gpt4 book ai didi

c# - 如何绘制正方形的边框?

转载 作者:太空狗 更新时间:2023-10-29 17:35:04 24 4
gpt4 key购买 nike

我正在使用 monogame(它使用 XNA API 接口(interface))来编写我的游戏。到目前为止一切都很好,但我在本应简单的事情上遇到了障碍。

我需要画一个二维正方形。但我只想要边框(无填充)。

我见过很多展示如何进行填充的示例。但是没有一个只会显示边框。

我想我可以制作一个图像并使用它。但我怀疑它会很好地调整大小。

最佳答案

也许您可以使用 1x1 像素纹理通过 Sprite 批处理绘制各个边缘。在 XNA 中,您可以这样做:

class RectangleSprite
{
static Texture2D _pointTexture;
public static void DrawRectangle(SpriteBatch spriteBatch, Rectangle rectangle, Color color, int lineWidth)
{
if (_pointTexture == null)
{
_pointTexture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1);
_pointTexture.SetData<Color>(new Color[]{Color.White});
}

spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y, rectangle.Width + lineWidth, lineWidth), color);
spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X + rectangle.Width, rectangle.Y, lineWidth, rectangle.Height + lineWidth), color);
spriteBatch.Draw(_pointTexture, new Rectangle(rectangle.X, rectangle.Y + rectangle.Height, rectangle.Width + lineWidth, lineWidth), color);
}
}

生成的绘图将比传递的矩形宽(和高)lineWidth 像素。

enter image description here

上面是用这段代码绘制的:

sb.Begin(); //spritebatch
RectangleSprite.DrawRectangle(sb, new Rectangle(10, 10, 50, 100), Color.Red, 1);
RectangleSprite.DrawRectangle(sb, new Rectangle(30, 20, 80, 15), Color.Green, 4);
RectangleSprite.DrawRectangle(sb, new Rectangle(70, 40, 40, 70), Color.Blue, 6);
sb.End();

关于c# - 如何绘制正方形的边框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13893959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com