# line slope formula

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

## 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 on other sites
The slope formula is ok.

You are reinventing the Bresenham algorithm. [grin]

##### 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 on other sites
Quote:
 Original post by emprogi 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 on other sites
Quote:
 Original post by staafWatch 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 on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628667
• Total Posts
2984143

• 13
• 10
• 10
• 9
• 9