Professional Documents
Culture Documents
shenbagamoorthy.a83@wipro.com moorthy2626@gmail.com
3 2011HW69932 SHENBAGAMOORTHY A
3
Digital devices display a straight line by plotting
discrete coordinate points along the line path which are
calculated from the equation of the line.
Screen locations are referenced with integer values, so plotted
positions may only approximate actual line positions between
two specific endpoints.
A computed line position of (10.48, 20.51) will be converted to
pixel position (10, 21). This rounding of coordinate values to
integers causes lines to be displayed with a stairstep appearance
(the “jaggies”).
Particularly noticeable on systems with low resolution.
To smooth raster lines, pixel intensities along the line paths must
be adjusted.
4
Line Drawing Algorithms
Cartesian equation:
y = mx + c y2
where y1
m – slope x1 x2
c – y-intercept
y2 y1 y
m
x2 x1 x
5
Slope
+ve -ve
if |m| = 1 45°
45°
= 45°
if |m| 1
-45° < < 45° °
°
if |m| 1
45° < < 90° or °
x y 8
0 0 7
1 1 6
2 2 5
3 3 4
4 4
3
5 5
2
6 6 1
7 7 0
8 8 x
0 1 2 3 4 5 6 7 8
7
|m| 1
y=½x+1
m=½
y
c=1
8
x y round(y)
7
0 1 1 6
1 1.5 2 5
4
2 2 2
3
3 2.5 3
2
4 3 3 1
5 3.5 4 0
0 1 2 3 4 5 6 7 8 x
6 4 4
8 7 4.5 5
|m| 1
y = 3x - 2
m=3
c = -2 y
8
x y round(y)
7
0 -2 -2 6
1 1 1 5
2 4 4 4
3 7 7 3
4 10 10 2
5 13 13 1
0
6 16 16 0 1 2 3 4 5 6 7 8 x
outside
9 7 19 19
Bresenham Line Algorithm
A more efficient approach
A
B
Start position
True line
ti
si
di = (si - ti)
If di 0, then closest pixel is below true line (si smaller)
If di 0, then closest pixel is above true line (ti smaller)
12
Example:
dy
Let gradient(slope) 0.5 (i.e. 0.5 or 2dy dx)
dx
Start pixel at (x0,y1)
y5 At x1 :
s1 = dy t1 = dx - dy
y3 d1 = (si - ti) = dy - (dx - dy) = 2dy - dx
but 2dy dx di 0 y stays the same
hence next pixel is at (x1,y1)
y2
3dy 4dy At x2 :
2dy s2 = 2dy t2 = dx - 2dy
y1 dy
d2 = (s2 – t2) = 2dy - (dx - 2dy) = 4dy - dx
Suppose d2 0 y is incremented
y0 hence next pixel is at (x2,y2)
x0 x1 x2 x3 x4 x5
At x3 :
s3 = 3dy - dx t2 = 2dx - 3dy
d3 = (s2 – t3) = 6dy - 3dx 0
so y stays the same
hence next pixel is at (x3,y2)
13
In General For a line with gradient ≤ 1
d0 = 2dy – dx
if di 0 then yi+1 = yi
di+1 = di + 2dy
if di ≥ 0 then yi+1 = yi + 1
di+1 = di + 2(dy – dx)
xi+1 = xi + 1
For a line with gradient 1
d0 = 2dx – dy
if di 0 then xi+1 = xi
di+1 = di + 2dx
if di ≥ 0 then xi+1 = xi + 1
di+1 = di + 2(dx – dy)
yi+1 = yi + 1
Note: For |m| ≤ 1 the constants 2dy and 2(dy-dx) can be calculated
14 once,
so the arithmetic will involve only integer addition and subtraction.
Example – Draw a line from (20,10) to (30,18)
(30,18)
dx = 10
dy = 8
initial decision d0 = 2dy – dx = 6
Also 2dy = 16, 2(dy – dx) = -4
(20,10)
i di (xi+1,yi+1)
19
0 6 (21,11)
1 2 (22,12) 18
2 -2 (23,12)
3 14 (24,13) 17
4 10 (25,14)
16
5 6 (26,15)
6 2 (27,16) 15
7 -2 (28,16)
8 14 (29,17) 14
9 10 (30,18)
13
15
12
void LineBres(int x0, int y0, int x1, int y1) // line for |m| < 1
{
int dx = abs(x1 – x0), dy = abs(y1 – y0);
int d = 2 * dy – dx, twoDy = 2 * dy, twoDyMinusDx = 2 * (dy – dx);
int x, y;
if (x0 > x1) { // determines which point to use as start, which as
end
x = x1;
y = y1;
x1 = x0;
}
else {
x = x0;
y = y0;
}
setPixel(x,y);
while (x < x1) {
x++;
if (d < 0) d += twoDy;
else {
y++;
d += twoDyMinusDx;
}
setPixel(x, y);
}
}
16
Special cases
Special cases can be handled separately
Horizontal lines (y = 0)
Vertical lines (x = 0)
Diagonal lines (|x| = |y|)
directly into the frame-buffer without processing
them through the line-plotting algorithms.
17
Circle Equations
Polar form
x = rCos
y = rSin (r = radius of circle)
P=(rCos, rSin)
r
rSin)
x
rCos)
18
Drawing a circle
= 0°
while ( < 360°)
x = rCos
y = rSin
setPixel(x,y)
= + 1°
end while
Disadvantages
To find a complete circle varies from 0° to 360°
The calculation of trigonometric functions is very
slow.
19
Cartesian form
Use Pythagoras theorem
x2 + y2 = r2 r
y
r
P x, r 2 x 2
y
x x
20
Circle algorithms
Step through x-axis to determine y-values
Disadvantages:
– Not all pixel filled in
21
– Square root function is very slow
Circle Algorithms
Use 8-fold symmetry and only compute pixel
positions for the 45° sector.
(-x, y) (x, y)
(-y, x) (y, x)
45°
General Principle
The circle function:
f circle ( x, y ) x 2 y 2 r 2
and
xi xi + 1
24
Bresenham’s Circle Algorithm
Define: D(si) = distance of p3 from circle
D(ti) = distance of p2 from circle
if pi < 0 then
yi+1 = yi
pi+1 = pi + 4xi + 6
xi+1 = xi + 1
else if pi ≥ 0 then
yi+1 = yi – 1
pi+1 = pi + 4(xi – yi) + 10
i pi xi, yi
9
8
0 -17 (0, 10)
7
6
1 -11 (1, 10) 5
4
3
2 -1 (2, 10) 2
1
3 13 (3, 10) 0
0 1 2 3 4 5 6 7 8 9 10
27
4 -5 (4, 9)
Midpoint Circle Algorithm
Midpoint
yi
yi-1 x2 + y2 – r2 = 0
xi xi+1 xi+2
Assuming that we have just plotted the pixels at (xi , yi).
Which is next? (xi+1, yi) OR (xi+1, yi – 1).
- The one that is closer to the circle.
28
Midpoint Circle Algorithm
The decision parameter is the circle at the midpoint
between the pixels yi and yi – 1.
pi f circle ( xi 1, yi 12 )
( xi 1)2 ( yi 12 )2 r 2
29
Decision Parameters
Decision Parameters are obtained using
incremental calculations
Note:
pi 1 f circle ( xi 1 1, yi 1 12 ) xi+1 = xi +1
( xi 2)2 ( yi 1 12 )2 r 2
OR
pi 1 pi 2( xi 1) 2 ( yi21 yi2 ) ( yi 1 yi ) 1
30
1. Initial values:- point(0,r) The Algorithm
x0 = 0
y0 = r move circle origin at (0,0) by
x = x – xc and y = y – yc
2. Initial decision parameter
p0 f circle (1, r 12 ) 1 (r 12 ) 2 r 2 54 r
3. At each xi position, starting at i = 0, perform the
following test: if pi < 0, the next point is (xi + 1, yi) and
pi+1 = pi + 2xi+1 + 1
If pi ≥ 0, the next point is (xi+1, yi-1) and
pi+1 = pi + 2xi+1 + 1 – 2yi+1
where 2xi+1 = 2xi + 2 and 2yi+1 = 2yi – 2
4. Determine symmetry points in the other octants
5. Move pixel positions (x,y) onto the circular path
centered on (xc, yc) and plot the coordinates: x = x + xc,
y = y + yc
31 6. Repeat 3 – 5 until x ≥ y
Example
r = 10
p0 = 1 – r = -9 (if r is integer round p0 = 5/4 – r to integer)
Initial point (x0, y0) = (0, 10)
i pi xi+1, 2xi+ 2yi+ 10
yi+1 1 1
9
0 -9 (1, 10) 2 20
8
1 -6 (2, 10) 4 20 7
6
2 -1 (3, 10) 6 20 5
4
3 6 (4, 9) 8 18
3
4 -3 (5, 9) 10 18 2
1
5 8 (6, 8) 12 16 0
0 1 2 3 4 5 6 7 8 9 10
32
6 5 (7, 7)
Midpoint function
void plotpoints(int x, int y)
{
setpixel(xcenter+x, ycenter+y);
setpixel(xcenter-x, ycenter+y);
setpixel(xcenter+x, ycenter-y);
setpixel(xcenter-x, ycenter-y);
setpixel(xcenter+y, ycenter+x);
setpixel(xcenter-y, ycenter+x);
setpixel(xcenter+y, ycenter-x);
setpixel(xcenter-y, ycenter-x);
}
void circle(int r)
{
int x = 0, y = r;
plotpoints(x,y);
int p = 1 – r;
while (x<y) {
x++;
if (p<0) p += 2*x + 1;
else {
y--;
p += 2*(x-y) + 1;
}
plotpoints(x,y);
33 }
}
Ellipse-Generating Algorithms
Ellipse – A modified circle whose radius varies from a
maximum value in one direction (major axis) to a minimum
value in the perpendicular direction (minor axis).
d1
F1 P=(x,y)
d2
F2
The sum of the two distances d1 and d2, between the fixed positions F1 and
F2 (called the foci of the ellipse) to any point P on the ellipse, is the same
value, i.e.
d1 + d2 = constant
34
Ellipse Properties
Expressing distances d1 and d2 in terms of the focal
coordinates F1 = (x1, x2) and F2 = (x2, y2), we have:
( x x1 )2 ( y y1 )2 ( x x2 )2 ( y y2 )2 constant
ry
rx
Cartesian coordinates: x x 2
2
y yc
1
c
rx ry
Polar coordinates:
x xc rx cos
35 y yc ry sin
Ellipse Algorithms
Symmetry between quadrants
Not symmetric between the two octants of a quadrant
Thus, we must calculate pixel positions along the
elliptical arc through one quadrant and then we obtain
positions in the remaining 3 quadrants by symmetry
(-x, y) (x, y)
ry
rx
36
Ellipse Algorithms
fellipse ( x, y) r x r y r r
2 2
y x
2 2 2 2
x y
Decision parameter:
0 if ( x, y ) is inside the ellipse
f ellipse ( x, y ) 0 if ( x, y ) is on the ellipse
0 if ( x, y ) is outside the ellipse
Slope = -1
1
ry 2 2ry2 x
dy
Slope 2
rx dx 2rx y
37
Slope = -1
Ellipse Algorithms 1
ry 2
rx
2ry2 x 2rx2 y
38
Midpoint Ellipse Algorithm
Midpoint
yi
yi-1
xi xi+1 xi+2
p1i 1 f ellipse ( xi 1 1, yi 1 12 )
ry2 ( xi 2) 2 rx2 ( yi 1 12 ) 2 rx2 ry2
OR
p1i 1 p1i 2ry2 ( xi 1) 2 ry2 rx2 ( yi 1 12 ) 2 ( yi 12 ) 2
where yi+1 = yi
or yi+1 = yi – 1
40
Decision Parameter (Region 1)
Decision parameters are incremented by:
2ry2 xi 1 ry2 if p1i 0
increment 2
y i 1 y
2 r x r 2
2 r 2
x yi 1 if p1i 0
Midpoint
yi
yi-1
xi xi+1 xi+2
Decision parameter:
p 2i f ellipse ( xi 12 , yi 1)
ry2 ( xi 12 ) 2 rx2 ( yi 1) 2 rx2 ry2
If p2i > 0 the midpoint is outside the ellipse xi is closer
42
If p2i ≤ 0 the midpoint is inside the ellipse xi + 1 is closer
Decision Parameter (Region 2)
p 2i 1 f ellipse ( xi 1 12 , yi 1 1)
ry2 ( xi 1 12 ) 2 rx2 ( yi 2) 2 rx2 ry2
OR
p 2i 1 p 2i 2rx2 ( yi 1) rx2 ry2 ( xi 1 12 ) 2 ( xi 12 ) 2
where xi+1 = xi
or xi+1 = xi + 1
43
Decision Parameter (Region 2)
Decision parameters are incremented by:
2rx2 yi 1 rx2 if p 2i 0
increment 2
y i 1
2 r x 2 r 2
y
x i 1 rx
2
if p 2i 0
p 20 f ellipse ( x0 12 , y0 1)
ry2 ( x0 12 ) 2 rx2 ( y0 1) 2 rx2 ry2
44
Midpoint Ellipse Algorithm
1. Input rx, ry, and ellipse center (xc, yc), and obtain the
first point on an ellipse centered on the origin as
(x0, y0) = (0, ry)
2. Calculate the initial parameter in region 1 as
6
5
4
3
2
1
0
48
0 1 2 3 4 5 6 7 8
Midpoint Ellipse Function
void ellipse(int Rx, int Ry)
{
int Rx2 = Rx * Rx, Ry2 = Ry * Ry;
int twoRx2 = 2 * Rx2, twoRy2 = Ry2 * Ry2;
int p, x = 0, y = Ry;
int px = 0, py = twoRx2 * y;
ellisePlotPoints(xcenter, ycenter, x, y);
// Region 1
p = round(Ry2 – (Rx2 * Ry) + (0.25 * Rx2));
while (px < py) {
x++;
px += twoRy2;
if (p < 0) p += Ry2 + px;
else {
y--;
py -= twoRx2;
p += Ry2 + px – py;
}
ellisePlotPoints(xcenter, ycenter, x, y);
}
// Region 2
p = round(Ry2 * (x+0.5) * (x+0.5) + Rx2 * (y-1)*(y-1) – Rx2 * Ry2;
while (y > 0) {
y--;
py -= twoRx2;
if (p > 0) p += Rx2 – py;
else {
x++;
px += twoRy2;
p += Rx2 – py + px;
}
ellisePlotPoints(xcenter, ycenter, x, y);
49 }
}
Thank You