Messing around with Microsoft XNA

The last couple of days I’ve been messing around with XNA 4.0. I have never tried it before – but a lot of things are real easy to do, but one thing baffled me – how hard is was to draw a line.

After searching a bit on the topic, I decided to make a method for drawing lines – just thought I would share it.

private void DrawLine(SpriteBatch batch, float width, Color color, Vector2 p1, Vector2 p2)
	Texture2D blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
	blank.SetData(new[] { Color.White });
	float angle = (float)Math.Atan2(p2.Y - p1.Y, p2.X - p1.X);
	float length = Vector2.Distance(p1, p2);
	Vector2 scale = new Vector2(length, width);
	batch.Draw(blank, p1, null, color, angle, Vector2.Zero, scale, SpriteEffects.None, 0);

Can it really be true that this is the smartest way to draw a line?

Leave a Reply

Your email address will not be published. Required fields are marked *