Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Line drawing algorithm
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Simple Methods === The simplest method of drawing a line involves directly calculating pixel positions from a line equation. Given a starting point <math>(x_1, y_1)</math> and an end point <math>(x_2, y_2)</math>, points on the line fulfill the equation <math>y = m(x-x_1) + y_1</math>, with <math>\textstyle m = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1}</math> being the [[slope]] of the line. The line can then be drawn by evaluating this equation via a simple loop, as shown in the following pseudocode: dx = x2 β x1 dy = y2 β y1 m = dy/dx '''for''' x '''from''' x1 '''to''' x2 '''do''' y = m Γ (x β x1) + y1 plot(x, y) Here, the points have already been ordered so that <math>x_2 > x_1</math>. This algorithm is unnecessarily slow because the loop involves a multiplication, which is significantly slower than addition or subtraction on most devices. A faster method can be achieved by viewing the Difference between two consecutive steps: :{| |- | <math>y_{i+1} - y_i \!</math> | <math>= (m(x_{i+1}-x_1) + y_1) - (m(x_i-x_1) + y_1) \!</math> |- | || <math>= m(x_{i+1}-x_i) \!</math> |- | || <math>= m \!</math>. |} Therefore, it is enough to simply start at the point <math>(x_1, y_1)</math> and then increase <math> y </math> by <math> m </math> once on every iteration of the loop. This algorithm is known as a [[Digital differential analyzer (graphics algorithm) | Digital differential analyzer]]. Because rounding <math> y </math> to the nearest whole number is equivalent to rounding <math> y+0.5 </math> down, rounding can be avoided by using an additional control variable that is initialized with the value 0.5. <math> m </math> is added to this variable on every iteration. Then, if this variable exceeds 1.0, <math> y </math> is incremented by 1 and the control variable is decremented by 1. This allows the algorithm to avoid rounding and only use integer operations. However, for short lines, this faster loop does not make up for the expensive division <math>\textstyle m = \frac{\Delta y}{\Delta x} = \frac{y_2-y_1}{x_2-x_1}</math>, which is still necessary at the beginning. These algorithm works just fine when <math>dx \geq dy</math> (i.e., slope is less than or equal to 1), but if <math>dx < dy</math> (i.e., slope greater than 1), the line becomes quite sparse with many gaps, and in the limiting case of <math>dx = 0</math>, a division by zero exception will occur.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)