Jump to content
  • Advertisement
Sign in to follow this  
mark-w

drawing a 2D line

This topic is 4330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a 2D point I want to draw a line from. I know the length I need to draw, and the slope. I'm not sure how to go about doing this. I could keep drawing line segments at the given slope, and keep checking if I've overstepped the terminating length, but that doesn't seem like an efficient way to do it. Any ideas? Thanks, Mark

Share this post


Link to post
Share on other sites
Advertisement
Given that the slope is b = dy/dx, you should be able to calculate a target point as follows (you didn't mention a language, but I'll assume C/C++):
float dx = 1.0f;
float dy = b;
float length = std::sqrt(1.0f+dy*dy);
dy /= length;
dx /= length;
float target_x = start_x + dx * line_length;
float target_y = start_y + dy * line_length;
DrawLine(start_x, start_y, target_x, target_y); // You supply this function
I may have made a mistake somewhere, but something like that should work. That said, I would suggest not using a slope representation for the line if you can at all avoid it, since it doesn't handle lines with arbitrary orientation in a uniform manner.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Surely your graphics library does this for you? DX can draw 3D lines (just use a non-transforming shader), GDI+ has DrawLine, and I don't know about OpenGL, but I'm sure there's something.

Share this post


Link to post
Share on other sites
Hi jyk,

Thanks for a fast reply. What would you recommend instead of a slope to represent the line?

I'm trying to perform a rotation of a 2D line around its center point. I'm figuring if the user grabs an end point of the line, then moves the mouse to some arbitrary location,

line center = 5,5
line length = 20;

user moves mouse to screen point

34,28

then I can draw both halves of the line appearing as if it rotated by drawing a line from the center point at the slope:

29,23

so I draw a line from the center point at the above slope with a length of (20/2).

Sound good, bad? What do you think?

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Surely your graphics library does this for you? DX can draw 3D lines (just use a non-transforming shader), GDI+ has DrawLine, and I don't know about OpenGL, but I'm sure there's something.
Well, he wants to draw a line using a start point and a slope, and at least some of the above APIs don't have any direct support for this.

Share this post


Link to post
Share on other sites
Quote:
Original post by mark-w
Hi jyk,

Thanks for a fast reply. What would you recommend instead of a slope to represent the line?

I'm trying to perform a rotation of a 2D line around its center point. I'm figuring if the user grabs an end point of the line, then moves the mouse to some arbitrary location,

line center = 5,5
line length = 20;

user moves mouse to screen point

34,28

then I can draw both halves of the line appearing as if it rotated by drawing a line from the center point at the slope:

29,23

so I draw a line from the center point at the above slope with a length of (20/2).

Sound good, bad? What do you think?

Thanks
I didn't quite follow all that, but one way or another you'll want to look into vectors (if you're not already familiar with them). There are some articles here on gdnet you could check out if you're new to vector math.

Also, what API are you using for drawing?

Share this post


Link to post
Share on other sites
When I did the exact same thing, drawing a line that needed to have its ends dragged, I just kept track of the two endpoints. Then, DrawLine(x1, y1, x2, y2) (if you have a funciton like that). What language / line-drawing routines do you have available to you?

EDIT: Oh, around it's center point... I would still keep track of the endpoint you're dragging, and subtract (2*x_offset) and (2*y_offset) from the x and y coordinate of it, respectively, to get the other tip of the line. (Where (x_offset) = (x coord of mouse - x coord of center) and (y_offset) = (y coord of mouse - y coord of center)).

Share this post


Link to post
Share on other sites
As has been said already, you should probably let your API do this. However, if you really want to draw lines yourself, have a look at this: Bresenham's algormithm

Edit: I guess I missed the part about rotating around the center too.

Share this post


Link to post
Share on other sites
Yeah sorry this is a little funky. If I know the user clicked on the end point of my line, then they drag the mouse to some other location on the screen - how could I rotate all the points in the line?

The method I came up with just uses the slope from the center point of the line to the point the user has their mouse at - and keeps filling those points in up until the length of the line is reached.

I'm using GDI, there's no rotate line function. If anyone knows how to rotate a line using only the two end points that would be great. I have to make sure the center point remains in place during the rotation and that the length of the line doesn't change during rotation,

Thanks

Share this post


Link to post
Share on other sites
Quote:
Original post by mark-w
The method I came up with just uses the slope from the center point of the line to the point the user has their mouse at - and keeps filling those points in up until the length of the line is reached.


Yeh, I guess that's pretty much what I said in my EDIT above (minus the "filling in each point"... you shouldn't have to do that much work in GDI).

Quote:
Original post by mark-w
I'm using GDI, there's no rotate line function.


But is there a line drawing routine? Or are you calculating + drawing one dot at a time? If the latter, you should go with Bresenham's, as mentioned above. But that's kinda low-level for GDI.

EDIT: Drawing a line in GDI.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!