1

How can i draw line in XNA (C#) by using float values, not integer because an integer number eliminate the values after points

like: 20.12

  • Doesn't drawing usually involve using the `Point` or `Pointf` struct? Why use `int` or `float` then? – Nolonar Mar 12 '13 at 10:04
  • Maybe this will help: [How do I draw lines using XNA?](http://stackoverflow.com/questions/270138/how-do-i-draw-lines-using-xna) – Nolonar Mar 12 '13 at 11:06
  • @Nolonar when i have to draw a line i need the following steps Just create a quick texture, for example: Texture2D SimpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color); And then just draw a line using that texture: this.spriteBatch.Draw(SimpleTexture, new Rectangle(100, 100, 100, 1), Color.Blue); this is what i need rectangle object to draw a line and the rectangle constructor required int parameters not float. – Muhammad Hussain Mar 12 '13 at 11:30
  • I'm afraid you'll have to do it in 3D instead. I've been looking around, but couldn't find a `Rectangle` supporting `float` coordinates. Sorry. – Nolonar Mar 12 '13 at 11:43
  • ok, but do you have any trick to draw a line using float values? – Muhammad Hussain Mar 12 '13 at 11:55

1 Answers1

0

You can try using the SpriteBatch.Draw (Texture2D, Vector2, Nullable<Rectangle>, Color, Single, Vector2, Vector2, SpriteEffects, Single) method. You'll need to create a Texture2D that represents your line first.

Vector2 pos = new Vector2(20.12F, 20.12F);
Vector2 scale = new Vector2(150.23F, 1);
Color myColor = Color.Blue;

Texture2D simpleTexture = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);

// Draws a "line" with size (1, 1), at position (20.12, 20.12), with blue color, rotated by 45° (Pi/4), with origin at (0, 0) (the line will begin at 'pos'), scaled by (150.23, 1) (gives the line a size of (150.23, 1) )
this.spriteBatch.Draw(simpleTexture, pos, null, myColor, MathHelper.PiOver4, Vector2.Zero, scale, SpriteEffects.None, 0)

It's been long since I've been working with XNA, so I can't guarantee that this will work.
I hope it helps anyways.

Nolonar
  • 5,962
  • 3
  • 36
  • 55