Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to be able to draw arbitary lines in my sprite based 2D XNA game. How can I draw a simple line on screen in XNA without dealing with vertex arrays or shaders.

share|improve this question
Hey Ken, take a look at my little drawinghelper library: It uses vertex arrays in the background, so no 1x1 white texture. – Luis Estrada Nov 19 '12 at 14:05
up vote 11 down vote accepted

You can draw a line using sprites. SpriteBatch.Draw(...) helpfully allows us to strech and rotate a sprite(texture).

In this code, we take a 1x1 pixel texture, strech it (by defining a rectangle of the correct shape and rotate it so it looks like a line.

    Texture2D t; //base for the line texture

    protected override void LoadContent()
        spriteBatch = new SpriteBatch(GraphicsDevice);
        // create 1x1 texture for line drawing
        t = new Texture2D(GraphicsDevice, 1, 1);
            new Color[] { Color.White });// fill the texture with white

    protected override void Draw(GameTime gameTime)


        DrawLine(spriteBatch, //draw line
            new Vector2(200, 200), //start of line
            new Vector2(100, 50) //end of line


    void DrawLine(SpriteBatch sb, Vector2 start, Vector2 end)
        Vector2 edge = end - start;
        // calculate angle to rotate line
        float angle =
            (float)Math.Atan2(edge.Y , edge.X);

            new Rectangle(// rectangle defines shape of line and position of start of line
                (int)edge.Length(), //sb will strech the texture to fill this rectangle
                1), //width of line, change this to make thicker line
            Color.Red, //colour of line
            angle,     //angle of line (calulated above)
            new Vector2(0, 0), // point in line about which to rotate

share|improve this answer
Something I want to note about this sort of line code is that it can need a little adjustment if you use a width greater than one. Because we're defining the height relative to the starting coordinates, using a length of more than 1 will make the line extend down, and if you have, for example, two separate 3-pixel lines with one rotated 180 degrees, one will extend up and one down. To fix this you can adjust the starting X coordinate based on the width of the line, like start.X - width / 2 – ssb Nov 19 '12 at 13:03

This library: has worked well for me in the past.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.