### Sharply drawn lines

##### By Greg Goltsov

Just recently while writing a game in XNA I really needed a simple way to draw lines for debugging. However, after some searching nothing really looked nice and usable straight away. And now, studying various curves in maths, I was thinking whether it would be cool to be able to draw the curves in C# using XNA.

So, after ending up using cpt spleen's LineBatch code heavily and modifying it to work on XNA 4.0, I decided to just publish the code I used.

Grag the source for the LineBatch class from GitHub.

And here's a pretty trivial example of it in use. I tried keeping the code minimal, yet still working for this example:

using System;
using Microsoft.Xna.Framework;

namespace LineBatch
{
public class LineBatchTest : Game
{
GraphicsDeviceManager graphics;
LineBatch lineBatch;

public LineBatchTest()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
lineBatch = new LineBatch(graphics.GraphicsDevice, new Vector2(0, 300));

base.Initialize();
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

float scale = 100;
float step = 1;
graphics.GraphicsDevice.Clear(Color.DarkOrange);

lineBatch.Begin();

for (float x = 0; x < 800; x+=step)
{
float delta = x + step;
lineBatch.Batch(new Vector2(x, (float) Math.Sin(MathHelper.ToRadians(x)) * scale),
new Vector2(delta, (float) Math.Sin(MathHelper.ToRadians(delta)) * scale),
Color.DarkSlateGray,
0);
}

lineBatch.End();

base.Draw(gameTime);
}
}
}


Yields quite a nice looking sine curve (sorry about no anti-aliasing):

Having the power of XNA is pretty cool, since it's really easy to actually animate these graphs. And remember that the line is drawn not through single points, but through two-point segments (see the main for loop).