Sign in to follow this  
emprog

line slope formula

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this