Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Bresenham's line algorithm
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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'''
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Bresenham's line algorithm
(section)
Add topic