Line drawing

I implemented a feature in MonoGame.Extended recently that draws primitive shapes, including lines using a SpriteBatch. Here’s the relevant code that should give you everything you need.

private static Texture2D _texture;
private static Texture2D GetTexture(SpriteBatch spriteBatch)
{
    if (_texture == null)
    {
        _texture = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
        _texture.SetData(new[] {Color.White});
    }

    return _texture;
}

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point1, Vector2 point2, Color color, float thickness = 1f)
{
    var distance = Vector2.Distance(point1, point2);
    var angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
    DrawLine(spriteBatch, point1, distance, angle, color, thickness);
}

public static void DrawLine(this SpriteBatch spriteBatch, Vector2 point, float length, float angle, Color color, float thickness = 1f)
{
    var origin = new Vector2(0f, 0.5f);
    var scale = new Vector2(length, thickness);
    spriteBatch.Draw(GetTexture(spriteBatch), point, null, color, angle, origin, scale, SpriteEffects.None, 0);
}

I wouldn’t say it’s the most efficient way to draw line. But it’ll work just fine if you’re only drawing a few lines every now and then. I normally use this stuff for debugging but it’s also very effective for simple effects like this or prototyping.

An alternative method would be to use a BasicEffect. The Farseer Physics library has a nice implementation of PrimitiveBatch that does something like this. We’ve been thinking about adding this to MonoGame.Extended also.

3 Likes