Drawing Algorithms
Computer Graphics
P. Healy
CS1-08Computer Science Bldg.
tel: [email protected]
Spring 2018–2019
P. Healy (University of Limerick) CS4815 Spring 2018–2019 1 / 13
Drawing Algorithms
Outline
1 Drawing AlgorithmsLine Drawing Algorithms: §6
P. Healy (University of Limerick) CS4815 Spring 2018–2019 2 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Outline
1 Drawing AlgorithmsLine Drawing Algorithms: §6
P. Healy (University of Limerick) CS4815 Spring 2018–2019 3 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Which pixels?
Which pixels do we “light up” when drawing the line segment(0,4) to (13,0)? (Note pixel “surrounds” the (x , y) value.)
A line-drawing demo
P. Healy (University of Limerick) CS4815 Spring 2018–2019 4 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Equation of a line
The equation of a line is y = mx + c, where m = ∆y∆x is its
slope and c is its y -intercept (when x = 0)The line containing p = (xp, yp) and q = (xq, yq) has slopem =
yq−ypxq−xp
For the same line we can figure out c since every (x , y) onmust satisfy
(y − yp)
(x − xp)= m
(y − yp) = m(x − xp)
y = mx + (yp −mxp)
Now use y = mx + c to find appropriate y -value for eachxp ≤ x ≤ xq
P. Healy (University of Limerick) CS4815 Spring 2018–2019 5 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Equation of a line
The equation of a line is y = mx + c, where m = ∆y∆x is its
slope and c is its y -intercept (when x = 0)The line containing p = (xp, yp) and q = (xq, yq) has slopem =
yq−ypxq−xp
For the same line we can figure out c since every (x , y) onmust satisfy
(y − yp)
(x − xp)= m
(y − yp) = m(x − xp)
y = mx + (yp −mxp)
Now use y = mx + c to find appropriate y -value for eachxp ≤ x ≤ xq
P. Healy (University of Limerick) CS4815 Spring 2018–2019 5 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Equation of a line
The equation of a line is y = mx + c, where m = ∆y∆x is its
slope and c is its y -intercept (when x = 0)The line containing p = (xp, yp) and q = (xq, yq) has slopem =
yq−ypxq−xp
For the same line we can figure out c since every (x , y) onmust satisfy
(y − yp)
(x − xp)= m
(y − yp) = m(x − xp)
y = mx + (yp −mxp)
Another way to look on this is
c = y −mx |(xq ,yq) = yq −mxq
Now use y = mx + c to find appropriate y -value for eachxp ≤ x ≤ xq
P. Healy (University of Limerick) CS4815 Spring 2018–2019 5 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Equation of a line
The equation of a line is y = mx + c, where m = ∆y∆x is its
slope and c is its y -intercept (when x = 0)The line containing p = (xp, yp) and q = (xq, yq) has slopem =
yq−ypxq−xp
For the same line we can figure out c since every (x , y) onmust satisfy
(y − yp)
(x − xp)= m
(y − yp) = m(x − xp)
y = mx + (yp −mxp)
Now use y = mx + c to find appropriate y -value for eachxp ≤ x ≤ xq
P. Healy (University of Limerick) CS4815 Spring 2018–2019 5 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
If slope is m = ∆y∆x and we’re “standing” on line then
advancing ∆x units in x-direction and ∆y units iny -direction brings us back on to lineIf we advance pixel in x-direction then we must advance
pixels in y -direction to compensateTo draw a line segment between two points we start atone end point and move towards the other as follows:
If line has then we “sample” at (stepalong) successive values of and
yk+1 = yk + m
If the line has then 1m is small and so
we sample successively on using
xk+1 = xk +1m
This is much faster than solving y = mx + c repeatedlyP. Healy (University of Limerick) CS4815 Spring 2018–2019 6 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
void lineDDA (int x0, int y0, int xEnd, int yEnd){int dx = xEnd - x0, dy = yEnd - y0, steps, k;float xIncrement, yIncrement, x = x0, y = y0;
if (fabs(dx) > fabs(dy)) steps = fabs (dx);else steps = fabs (dy);xIncrement = float (dx) / float (steps);yIncrement = float (dy) / float (steps);
setPixel (round (x), round (y)); // round(x) = intfor (k = 0; k < steps; k++) {x += xIncrement;y += yIncrement;setPixel (round (x), round (y));
}}P. Healy (University of Limerick) CS4815 Spring 2018–2019 7 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
void lineDDA (int x0, int y0, int xEnd, int yEnd){int dx = xEnd - x0, dy = yEnd - y0, steps, k;float xIncrement, yIncrement, x = x0, y = y0;
if (fabs(dx) > fabs(dy)) steps = fabs (dx);else steps = fabs (dy);xIncrement = float (dx) / float (steps);yIncrement = float (dy) / float (steps);
setPixel (round (x), round (y)); // round(x) = intfor (k = 0; k < steps; k++) {x += xIncrement;y += yIncrement;setPixel (round (x), round (y));
}}P. Healy (University of Limerick) CS4815 Spring 2018–2019 7 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Digital Differential Analyzer
void lineDDA (int x0, int y0, int xEnd, int yEnd){int dx = xEnd - x0, dy = yEnd - y0, steps, k;float xIncrement, yIncrement, x = x0, y = y0;
if (fabs(dx) > fabs(dy)) steps = fabs (dx);else steps = fabs (dy);xIncrement = float (dx) / float (steps);yIncrement = float (dy) / float (steps);
setPixel (round (x), round (y)); // round(x) = intfor (k = 0; k < steps; k++) {x += xIncrement;y += yIncrement;setPixel (round (x), round (y));
}}P. Healy (University of Limerick) CS4815 Spring 2018–2019 7 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm
DDA requires floating point arithmeticBresenham’s algorithm requires only (much faster) integercalculationsThe general idea also works for circles and other curvesIdea: as with prev. alg., from current pixel (xk , yk ), we needto decide where to go to nextAssuming (w.l.o.g.) that |m| < 1, and so we will sample atsuccessive values of x again but increasing xThat is with xk+1 = xk + 1; find the best choice for yk+1
?
?yk
xk
P. Healy (University of Limerick) CS4815 Spring 2018–2019 8 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm (contd.)
Given x co-ordinate of xk + 1, what is best y value,yk+1 = yk or yk+1 = yk + 1 (for the previous line)?The exact value is y = m(xk + 1) + cSo we should choose the value closest to this as bestThe two differences or errors are:
dl = y − yk
= m(xk + 1) + c − yk , and
du = yk + 1− y= yk + 1−m(xk + 1)− c
dl > du → dl − du > 0 so sign tells us which is larger; thencombining these
dl − du = 2m(xk + 1)− 2yk + 2c − 1
P. Healy (University of Limerick) CS4815 Spring 2018–2019 9 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm (contd.)
In dl − du = 2m(xk + 1)− 2yk + 2c − 1 we still havem = ∆y
∆x which is a real no.; try fix thisIf we assume that ∆x > 0 (always scan from left to right inx) then multiply through by ∆x . Let be thepredicate:
pk = ∆x(dl − du)
= 2∆yxk − 2∆xyk + K
has the same sign as before, whereK = 2∆y + 2c∆x −∆x is a constant throughoutSo pk > 0⇒ dl > du and the upper y , yk+1, is closestHaving decided on yk , compute pk to find the better yk+1
Likewise, computing pk+1 = 2∆yxk+1 − 2∆xyk+1 + K tellsus what yk+2 to choose at x = xk+2
P. Healy (University of Limerick) CS4815 Spring 2018–2019 10 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm (contd.)
Can we squeeze any more juice from this?Subtracting successive values of p:
pk+1 − pk = 2∆yxk+1 − 2∆xyk+1 + K − (2∆yxk − 2∆xyk + K )
= 2∆y(xk+1 − xk )− 2∆x(yk+1 − yk )
= 2∆y − 2∆x(yk+1 − yk )
andpk+1 = pk + 2∆y − 2∆x(yk+1 − yk )
The initial condition is (see pk on prev. slide)
p0 = 2∆yx0 − 2∆xy0 + 2∆y + 2c∆x −∆x= 2∆y −∆x
since y0 = ∆y∆x x0 + c
P. Healy (University of Limerick) CS4815 Spring 2018–2019 11 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm (contd.)
pk+1 = pk + 2∆y − 2∆x(yk+1 − yk )
p0 = 2∆y −∆xpk < 0⇒ stay with same y -value: yk+1 = yk
Note that 2∆y and 2∆x are constantsProcedure:
Compute p0 and use this to tell whether y1 stays with samevalue as y0 or to use y0 + 1 at x1 = x0 + 1We use this y -information in computing p1:
p1 = p0 + 2∆y − 2∆x(y1 − y0)
Repeatedly use y -information from pk to calc pk+1Note: each predicate pk says what to at next sample, xk+1
P. Healy (University of Limerick) CS4815 Spring 2018–2019 12 / 13
Drawing Algorithms Line Drawing Algorithms: §6
Bresenham’s Line-Drawing Algorithm (concl.)
The previous treatment was for “gently increasing lines” –lines of the form 0 ≤ m ≤ 1What about “gently decreasing lines”?A similar derivation to the previous one can be done forwhen y decreases as x increases:
For consistency with previous case we base predicate pkon “predicate less than 0 means we stay with current y ”To do this we must set
pk = ∆x(du − dl )
The exact same formulae as before are yielded with theexception that (yk+1 − yk ) = −1or0 – opposite to previousTo fix for this always – in either case – use absolute value of∆y
As with DDA if 1 < |m| (slope is steep either positive ornegative) we sample with increasing y to improve coverage
P. Healy (University of Limerick) CS4815 Spring 2018–2019 13 / 13