# line slope formula

This topic is 4988 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634117
• Total Posts
3015601
×