Jump to content
  • Advertisement
Sign in to follow this  
emprog

line slope formula

This topic is 4893 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

if i have a lines points x1 y1, x2 y2 which is a line that has slope what is the forula to calculate how much i would change y by during the pixal drawing? i basically want to draw a line one pixal at a time the formula i have so far is slope equals (y2-y1) /(x2-x1) am i on the right track? i am going to use a for loop for the drawing and use x1 x2 as the loop condition to know when i should stop drawing all i need to know now is how much to change y by during the drawing.

Share this post


Link to post
Share on other sites
Advertisement
Watch out for vertical lines though. If x1 and x2 are equal then the denominator will be zero. Make sure to test for that case when you draw lines between arbitrary points.

Btw, I love the word arbitrary. [grin]

Share this post


Link to post
Share on other sites
Quote:
Original post by emprog
i am going to use a for loop for the drawing and use x1 x2 as the loop condition to know when i should stop drawing all i need to know now is how much to change y by during the drawing.


One problem is that if you always increment X by 1 unit, then you will have gaps when the slope is > 1.
  m=4 m=2   m=1
| * * *
| *
| * *
| *
| * * *
| *
| * *
| *
|** *
| *
|**
|*
*-------------------------
Anyway, I also recommend you look at the Bresenham algorithm.

Share this post


Link to post
Share on other sites
Quote:
Original post by staaf
Watch out for vertical lines though. If x1 and x2 are equal then the denominator will be zero. Make sure to test for that case when you draw lines between arbitrary points.

Btw, I love the word arbitrary. [grin]


An arbitrary remark: To avoid this problem, go parametric on your line.

Common: y = m.x + b

Parametric: x = x(t), y = y(t)

It's trivial to figure out the specific form of the t-functions given the Common formulation.

Moreover, you gain a great deal of flexibility in the nature of the curves you can talk about.

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!