我如何在单体组中绘制线条?

发布于 2025-02-14 01:11:45 字数 155 浏览 0 评论 0原文

几天前,我刚刚开始学习单体组,我想知道如何从start vector2和具有特定行厚度的End vector2绘制一条线?

我是否应该使用一个像素图像将其绘制到屏幕上,然后使用Bresenham的线算法来查找位置,还是使用函数内置的单game来进行更优化且更复杂的方法来执行此操作?

I just started learning monogame a couple of days ago and I was wondering how I can draw a line from a Start Vector2 and an End Vector2 with a specific line thickness?

Should I use a one pixel image to draw it onto the screen and then use a Bresenham's line algorithm to find there positions, or is there a more optimized and less complicated method to do this using monogames built in functions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

携余温的黄昏 2025-02-21 01:11:45

一种方法是创建一个texture2d,其宽度是两个vector2 s与所需宽度的高度之间的距离。然后,当您绘制纹理时,将旋转旋转应用于纹理。

这是一个示例(作为SpriteBatch扩展方法):

public static void DrawLineBetween(
    this SpriteBatch spriteBatch,
    Vector2 startPos,
    Vector2 endPos,
    int thickness,
    Color color)
{
    // Create a texture as wide as the distance between two points and as high as
    // the desired thickness of the line.
    var distance = (int)Vector2.Distance(startPos, endPos);
    var texture = new Texture2D(spriteBatch.GraphicsDevice, distance, thickness);

    // Fill texture with given color.
    var data = new Color[distance * thickness];
    for (int i = 0; i < data.Length; i++)
    {
        data[i] = color;
    }
    texture.SetData(data);

    // Rotate about the beginning middle of the line.
    var rotation = (float)Math.Atan2(endPos.Y - startPos.Y, endPos.X - startPos.X);
    var origin = new Vector2(0, thickness / 2);

    spriteBatch.Draw(
        texture,
        startPos,
        null,
        Color.White,
        rotation,
        origin,
        1.0f,
        SpriteEffects.None,
        1.0f);
}

使用示例:

var startPos = new Vector2(0, 0);
var endPos = new Vector2(800, 480);
_spriteBatch.DrawLineBetween(startPos, endPos, 12, Color.White);

<

a href =“ https://i.sstatic.net/3ccccc9.png” rel =“ nofollow noreferrer“> ”输入图像描述在这里”

这不是一个完美的解决方案。如果您想以不同的角度绘制连接的线路,而没有任何可见的接缝,则需要对其进行修改。也不确定性能。

One way is to create a Texture2D with a width that is the distance between the two Vector2s and a height that is the desired width. Then you apply a rotation to the texture when you draw it.

Here is an example (as a SpriteBatch extension method):

public static void DrawLineBetween(
    this SpriteBatch spriteBatch,
    Vector2 startPos,
    Vector2 endPos,
    int thickness,
    Color color)
{
    // Create a texture as wide as the distance between two points and as high as
    // the desired thickness of the line.
    var distance = (int)Vector2.Distance(startPos, endPos);
    var texture = new Texture2D(spriteBatch.GraphicsDevice, distance, thickness);

    // Fill texture with given color.
    var data = new Color[distance * thickness];
    for (int i = 0; i < data.Length; i++)
    {
        data[i] = color;
    }
    texture.SetData(data);

    // Rotate about the beginning middle of the line.
    var rotation = (float)Math.Atan2(endPos.Y - startPos.Y, endPos.X - startPos.X);
    var origin = new Vector2(0, thickness / 2);

    spriteBatch.Draw(
        texture,
        startPos,
        null,
        Color.White,
        rotation,
        origin,
        1.0f,
        SpriteEffects.None,
        1.0f);
}

Example of use:

var startPos = new Vector2(0, 0);
var endPos = new Vector2(800, 480);
_spriteBatch.DrawLineBetween(startPos, endPos, 12, Color.White);

How it looks:

enter image description here

It's not a perfect solution. You'll want to modify it if you want to draw connected lines with different angles without any visible seams. Also not sure about the performance.

煮茶煮酒煮时光 2025-02-21 01:11:45

我使用我发现的库。它绘制了许多2D原语,例如行,框等。真的很容易使用。称为“ c3.mnogame.primitives2d”,可以在此处找到:
https://github.com/z2oh/c3.monogame.primitives.primitives2d

这是一个screenshot我使用许多方法写的演示:

这只是一个约500行的一个文件。如果您不喜欢git或使用库,则只需复制&amp;将其粘贴到您的项目中。

I use a library I found. It draws a number of 2D primitives, like lines, boxes, etc. Really easy to use. Called "C3.MonoGame.Primitives2D", it can be found here:
https://github.com/z2oh/C3.MonoGame.Primitives2D

Here's a screenshot of a demo I wrote using many of its methods:

C3.Monogame.Primitives2D

It's just one file of around 500 lines. If you don't like Git or using libraries, you can just copy & paste it into your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文