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
Bresenham's line 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!
===All cases=== However, as mentioned above this only works for [[octant (plane geometry)|octant]] zero, that is lines starting at the origin with a slope between 0 and 1 where x increases by exactly 1 per iteration and y increases by 0 or 1. The algorithm can be extended to cover slopes between 0 and -1 by checking whether y needs to increase or decrease (i.e. dy < 0) plotLineLow(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 yi = 1 '''if''' dy < 0 yi = -1 dy = -dy '''end if''' D = (2 * dy) - dx y = y0 '''for''' x from x0 to x1 plot(x, y) '''if''' D > 0 y = y + yi D = D + (2 * (dy - dx)) '''else''' D = D + 2*dy '''end if''' By switching the x and y axis an implementation for positive or negative steep slopes can be written as plotLineHigh(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 xi = 1 '''if''' dx < 0 xi = -1 dx = -dx '''end if''' D = (2 * dx) - dy x = x0 '''for''' y from y0 to y1 plot(x, y) '''if''' D > 0 x = x + xi D = D + (2 * (dx - dy)) '''else''' D = D + 2*dx '''end if''' A complete solution would need to detect whether x1 > x0 or y1 > y0 and reverse the input coordinates before drawing, thus plotLine(x0, y0, x1, y1) '''if''' abs(y1 - y0) < abs(x1 - x0) '''if''' x0 > x1 plotLineLow(x1, y1, x0, y0) '''else''' plotLineLow(x0, y0, x1, y1) '''end if''' '''else''' '''if''' y0 > y1 plotLineHigh(x1, y1, x0, y0) '''else''' plotLineHigh(x0, y0, x1, y1) '''end if''' '''end if''' In low level implementations which access the video memory directly, it would be typical for the special cases of vertical and horizontal lines to be handled separately as they can be highly optimized. Some versions use Bresenham's principles of integer incremental error to perform all octant line draws, balancing the positive and negative error between the x and y coordinates.<ref name=Zingl/> plotLine(x0, y0, x1, y1) dx = abs(x1 - x0) sx = x0 < x1 ? 1 : -1 dy = -abs(y1 - y0) sy = y0 < y1 ? 1 : -1 error = dx + dy '''while''' true plot(x0, y0) e2 = 2 * error '''if''' e2 >= dy '''if''' x0 == x1 '''break''' error = error + dy x0 = x0 + sx '''end if''' '''if''' e2 <= dx '''if''' y0 == y1 '''break''' error = error + dx y0 = y0 + sy '''end if''' '''end while'''
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)