Because we are given two points we can calculate the slope and then we can use the slope and either point and use the point-slope formula to find the equation for the line.
The slope can be found by using the formula:#m = (color(red)(y_2) - color(blue)(y_1))/(color(red)(x_2) - color(blue)(x_1))#
Where#m#is the slope and (#color(blue)(x_1, y_1)#) and (#color(red)(x_2, y_2)#) are the two points on the line.
Substituting the two points from the problem gives:
#m = (color(red)(-9) - color(blue)(-3))/(color(red)(-4) - color(blue)(6))#
#m = (color(red)(-9) + color(blue)(3))/(color(red)(-4) - color(blue)(6))#
#m = (-6)/(-10)#
#m = 6/10 = 3/5#
Now we have the slope and can use either point and the point-slope formula to find the equation for the line:
Bresenham's line algorithm is a line drawing algorithm that determines the points of an n-dimensional raster that should be selected in order to form a close approximation to a straight line between two points. It is commonly used to draw line primitives in a bitmap image (e.g. on a computer screen), as it uses only integer addition, subtraction and bit shifting, all of which are very cheap operations in commonly used computer instruction sets such as x86_64. It is an incremental error algorithm, and one of the earliest algorithms developed in the field of computer graphics. An extension to the original algorithm may be used for drawing circles.
While algorithms such as Wu's algorithm are also frequently used in modern computer graphics because they can support antialiasing, Bresenham's line algorithm is still important because of its speed and simplicity. The algorithm is used in hardware such as plotters and in the graphics chips of modern graphics cards. It can also be found in many software graphics libraries. Because the algorithm is very simple, it is often implemented in either the firmware or the graphics hardware of modern graphics cards.
The label "Bresenham" is used today for a family of algorithms extending or modifying Bresenham's original algorithm.
History[edit]
Bresenham's line algorithm is named after Jack Elton Bresenham who developed it in 1962 at IBM. In 2001 Bresenham wrote:[1]
I was working in the computation lab at IBM's San Jose development lab. A Calcomp plotter had been attached to an IBM 1401 via the 1407 typewriter console. [The algorithm] was in production use by summer 1962, possibly a month or so earlier. Programs in those days were freely exchanged among corporations so Calcomp (Jim Newland and Calvin Hefte) had copies. When I returned to Stanford in Fall 1962, I put a copy in the Stanford comp center library. A description of the line drawing routine was accepted for presentation at the 1963 ACM national convention in Denver, Colorado. It was a year in which no proceedings were published, only the agenda of speakers and topics in an issue of Communications of the ACM. A person from the IBM Systems Journal asked me after I made my presentation if they could publish the paper. I happily agreed, and they printed it in 1965.
Bresenham's algorithm has been extended to produce circles, ellipses, cubic and quadratic bezier curves, as well as native anti-aliased versions of those.[2]
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 used:
- 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 (x0,y0){\displaystyle (x_{0},y_{0})}
The algorithm will be initially presented only for the octant in which the segment goes down and to the right (x0≤x1{\displaystyle x_{0}\leq x_{1}}
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:
y−y0y1−y0=x−x0x1−x0{\displaystyle {\frac {y-y_{0}}{y_{1}-y_{0}}}={\frac {x-x_{0}}{x_{1}-x_{0}}}}.Since we know the column, x, the pixel's row, y, is given by rounding this quantity to the nearest integer:
y=y1−y0x1−x0(x−x0)+y0{\displaystyle y={\frac {y_{1}-y_{0}}{x_{1}-x_{0}}}(x-x_{0})+y_{0}}.The slope (y1−y0)/(x1−x0){\displaystyle (y_{1}-y_{0})/(x_{1}-x_{0})}
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 y0−0.5{\displaystyle y_{0}-0.5}
Derivation[edit]
To derive Bresenham's algorithm, two steps must be taken. The first step is transforming the equation of a line from the typical slope-intercept form into something different; and then using this new equation to draw a line based on the idea of accumulation of error.
Line equation[edit]
y=f(x)=.5x+1 or f(x,y)=x-2y+2
Positive and negative half-planes
The slope-intercept form of a line is written as
y=f(x)=mx+b{\displaystyle y=f(x)=mx+b}where m is the slope and b is the y-intercept. Because this is a function of only x{\displaystyle x}
Letting this last equation be a function of x{\displaystyle x} and y{\displaystyle y}, it can be written as
f(x,y):=Ax+By+C=0{\displaystyle f(x,y):=Ax+By+C=0}where the constants are
- A=Δy=y1−y0{\displaystyle A=\Delta y=y_{1}-y_{0}}
- B=−Δx=−(x1−x0){\displaystyle B=-\Delta x=-(x_{1}-x_{0})}
- C=(Δx)b=x1y0−x0y1{\displaystyle C=(\Delta x)b=x_{1}y_{0}-x_{0}y_{1}}
The line is then defined for some constants A, B, and C anywhere f(x,y)=0{\displaystyle f(x,y)=0}
As an example, the line y=12x+1{\displaystyle y={\frac {1}{2}}x+1}
and the point (2,3) is not on the line
f(2,3)=(2)−2(3)+2=2−6+2=−2{\displaystyle f(2,3)=(2)-2(3)+2=2-6+2=-2}and neither is the point (2,1)
f(2,1)=(2)−2(1)+2=2−2+2=2{\displaystyle f(2,1)=(2)-2(1)+2=2-2+2=2}Notice that the points (2,1) and (2,3) are on opposite sides of the line and f(x,y) evaluates to positive or negative. A line splits a plane into halves and the half-plane that has a negative f(x,y) can be called the negative half-plane, and the other half can be called the positive half-plane. This observation is very important in the remainder of the derivation.
Algorithm[edit]
Clearly, the starting point is on the line
f(x0,y0)=0{\displaystyle f(x_{0},y_{0})=0}only because the line is defined to start and end on integer coordinates (though it is entirely reasonable to want to draw a line with non-integer end points).
Candidate point (2,2) in blue and two candidate points in green (3,2) and (3,3)
Keeping in mind that the slope is at most 1{\displaystyle 1}
If the value of this is positive then the ideal line is below the midpoint and closer to the candidate point (x0+1,y0+1){\displaystyle (x_{0}+1,y_{0}+1)}; in effect the y coordinate has advanced. Otherwise, the ideal line passes through or above the midpoint, and the y coordinate has not advanced; in this case choose the point (x0+1,y0){\displaystyle (x_{0}+1,y_{0})}. The value of the line function at this midpoint is the sole determinant of which point should be chosen.
The adjacent image shows the blue point (2,2) chosen to be on the line with two candidate points in green (3,2) and (3,3). The black point (3, 2.5) is the midpoint between the two candidate points.
Algorithm for integer arithmetic[edit]
Alternatively, the difference between points can be used instead of evaluating f(x,y) at midpoints. This alternative method allows for integer-only arithmetic, which is generally faster than using floating-point arithmetic. To derive the alternative method, define the difference to be as follows:
D=f(x0+1,y0+12)−f(x0,y0){\displaystyle D=f(x_{0}+1,y_{0}+{\tfrac {1}{2}})-f(x_{0},y_{0})}For the first decision, this formulation is equivalent to the midpoint method since f(x0,y0)=0{\displaystyle f(x_{0},y_{0})=0} at the starting point. Simplifying this expression yields:
D=[A(x0+1)+B(y0+12)+C]−[Ax0+By0+C]=[Ax0+By0+C+A+12B]−[Ax0+By0+C]=A+12B=Δy−12Δx{\displaystyle {\begin{array}{rclcl}D&=&\left[A(x_{0}+1)+B\left(y_{0}+{\frac {1}{2}}\right)+C\right]&-&\left[Ax_{0}+By_{0}+C\right]\\&=&\left[Ax_{0}+By_{0}+C+A+{\frac {1}{2}}B\right]&-&\left[Ax_{0}+By_{0}+C\right]\\&=&A+{\frac {1}{2}}B=\Delta y-{\frac {1}{2}}\Delta x\end{array}}}Just as with the midpoint method, if D{\displaystyle D}
If (x0+1,y0){\displaystyle (x_{0}+1,y_{0})} is chosen, the change in D will be:
ΔD=f(x0+2,y0+12)−f(x0+1,y0+12)=A=Δy{\displaystyle {\begin{array}{lclcl}\Delta D&=&f(x_{0}+2,y_{0}+{\tfrac {1}{2}})-f(x_{0}+1,y_{0}+{\tfrac {1}{2}})&=&A&=&\Delta y\\\end{array}}}If (x0+1,y0+1){\displaystyle (x_{0}+1,y_{0}+1)} is chosen the change in D will be:
ΔD=f(x0+2,y0+32)−f(x0+1,y0+12)=A+B=Δy−Δx{\displaystyle {\begin{array}{lclcl}\Delta D&=&f(x_{0}+2,y_{0}+{\tfrac {3}{2}})-f(x_{0}+1,y_{0}+{\tfrac {1}{2}})&=&A+B&=&\Delta y-\Delta x\end{array}}}If the new D is positive then (x0+2,y0+1){\displaystyle (x_{0}+2,y_{0}+1)}
Plotting the line from (0,1) to (6,4) showing a plot of grid lines and pixels
All of the derivation for the algorithm is done. One performance issue is the 1/2 factor in the initial value of D. Since all of this is about the sign of the accumulated difference, then everything can be multiplied by 2 with no consequence.
This results in an algorithm that uses only integer arithmetic.
plotLine(x0, y0, x1, y1) dx = x1 - x0 dy = y1 - y0 D = 2*dy - dx y = y0 for x from x0 to x1 plot(x, y) if D > 0 y = y + 1 D = D - 2*dx end if D = D + 2*dyRunning this algorithm for f(x,y)=x−2y+2{\displaystyle f(x,y)=x-2y+2} from (0,1) to (6,4) yields the following differences with dx=6 and dy=3:
D=2*3-6=0 Loop from 0 to 6 * x=0: plot(0, 1), D≤0: D=0+6=6 * x=1: plot(1, 1), D>0: D=6-12=-6, y=1+1=2, D=-6+6=0 * x=2: plot(2, 2), D≤0: D=0+6=6 * x=3: plot(3, 2), D>0: D=6-12=-6, y=2+1=3, D=-6+6=0 * x=4: plot(4, 3), D≤0: D=0+6=6 * x=5: plot(5, 3), D>0: D=6-12=-6, y=3+1=4, D=-6+6=0 * x=6: plot(6, 4), D≤0: D=0+6=6The result of this plot is shown to the right. The plotting can be viewed by plotting at the intersection of lines (blue circles) or filling in pixel boxes (yellow squares). Regardless, the plotting is the same.
All cases[edit]
However, as mentioned above this is only works for 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 ifBy 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 ifA 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 ifIn 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.[2] Take note that the order is not necessarily guaranteed; in other words, the line may be drawn from (x0, y0) to (x1, y1) or from (x1, y1) to (x0, y0).
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) if x0 == x1 && y0 == y1 break 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 whileThe Bresenham algorithm can be interpreted as slightly modified digital differential analyzer (using 0.5 as error threshold instead of 0, which is required for non-overlapping polygon rasterizing).
The principle of using an incremental error in place of division operations has other applications in graphics. It is possible to use this technique to calculate the U,V co-ordinates during raster scan of texture mapped polygons.[4] The voxel heightmap software-rendering engines seen in some PC games also used this principle.
Bresenham also published a Run-Slice (as opposed to the Run-Length) computational algorithm. This method has been represented in a number of US patents:
5,815,163Method and apparatus to draw line slices during calculation5,740,345Method and apparatus for displaying computer graphics data stored in a compressed format with an efficient color indexing system5,657,435Run slice line draw engine with non-linear scaling capabilities5,627,957Run slice line draw engine with enhanced processing capabilities5,627,956Run slice line draw engine with stretching capabilities5,617,524Run slice line draw engine with shading capabilities5,611,029Run slice line draw engine with non-linear shading capabilities5,604,852Method and apparatus for displaying a parametric curve on a video display5,600,769Run slice line draw engine with enhanced clipping techniquesAn extension to the algorithm that handles thick lines was created by Alan Murphy at IBM.[5]
See also[edit]
References[edit]
Further reading[edit]
rather than [which] for circle extension use: Technical Report 1964 Jan-27 -11- Circle Algorithm TR-02-286 IBM San Jose Lab or A Linear Algorithm for Incremental Digital Display of Circular Arcs February 1977 Communications of the ACM 20(2):100-106 DOI:10.1145/359423.359432