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!
==Method== [[File:Bresenham.svg|right|300px|thumb|Illustration of the result of Bresenham's line algorithm. (0,0) is at the top left corner of the grid, (1,1) is at the top left end of the line and (11, 5) is at the bottom right end of the line.]] The following conventions will be applied: * the top-left is (0,0) such that pixel coordinates increase in the right and down directions (e.g. that the pixel at (7,4) is directly above the pixel at (7,5)), and * the pixel centers have integer coordinates. The endpoints of the line are the pixels at <math>(x_0,y_0)</math> and <math>(x_1,y_1)</math>, where the first coordinate of the pair is the column and the second is the row. The algorithm will be initially presented only for the [[octant (plane geometry)|octant]] in which the segment goes down and to the right (<math>x_0 \leq x_1</math> and <math>y_0 \leq y_1</math>), and its horizontal projection <math>x_1-x_0</math> is longer than the vertical projection <math>y_1-y_0</math> (the line has a positive [[slope]] less than 1). In this octant, for each column ''x'' between <math>x_0</math> and <math>x_1</math>, there is exactly one row ''y'' (computed by the algorithm) containing a pixel of the line, while each row between <math>y_0</math> and <math>y_1</math> may contain multiple rasterized pixels. Bresenham's algorithm chooses the integer ''y'' corresponding to the [[pixel center]] that is closest to the ideal (fractional) ''y'' for the same ''x''; on successive columns ''y'' can remain the same or increase by 1. The general equation of the line through the endpoints is given by: :<math>\frac{y - y_0}{y_1-y_0} = \frac{x-x_0}{x_1-x_0}</math>. Since we know the column, ''x'', the pixel's row, ''y'', is given by rounding this quantity to the nearest integer: :<math>y = \frac{y_1-y_0}{x_1-x_0} (x-x_0) + y_0</math>. The slope <math>(y_1-y_0)/(x_1-x_0)</math> depends on the endpoint coordinates only and can be precomputed, and the ideal ''y'' for successive integer values of ''x'' can be computed starting from <math>y_0</math> and repeatedly adding the slope. In practice, the algorithm does not keep track of the y coordinate, which increases by ''m'' = ''βy/βx'' each time the ''x'' increases by one; it keeps an error bound at each stage, which represents the negative of the distance from (a) the point where the line exits the pixel to (b) the top edge of the pixel. This value is first set to <math>y_0-0.5</math> (due to using the pixel's center coordinates), and is incremented by ''m'' each time the ''x'' coordinate is incremented by one. If the error becomes greater than ''0.5'', we know that the line has moved upwards one pixel, and that we must increment our ''y'' coordinate and readjust the error to represent the distance from the top of the new pixel β which is done by subtracting one from error.<ref>{{Cite web|url=https://www.ercankoclar.com/wp-content/uploads/2016/12/Bresenhams-Algorithm.pdf|title=Bresenham's Algorithm|last=Joy|first=Kenneth|publisher=Visualization and Graphics Research Group, Department of Computer Science, University of California, Davis|access-date=20 December 2016}}</ref>
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)