Computer Graphics: Bresenham's Line Drawing Algorithm & Mid Point Circle Algorithm
Computer Graphics: Bresenham's Line Drawing Algorithm & Mid Point Circle Algorithm
Graphics
Bresenham's line drawing algorithm
& Mid Point Circle algorithm
Team Members
Group-I
S.No.
BITS ID
NAME
2011HW6989
8
2011HW6990
0
2011HW6993
SHENBAGAMOORTHY A
2
2011HW6991
3
2011HW6990
9
2011HW6956
9
2011HW6989
3
2011HW6990
4
2011HW6989
6
Personal Email ID
saikrishna.tanguturu@wipro.com
sai.tsk2008@gmail.com
RAYAPU MOSES
rayapu.moses@wipro.com
stalinkvd001@gmail.com
shenbagamoorthy.a83@wipro.com
moorthy2626@gmail.com
ANURUPA K C
anurupa.c85@wipro.com
anu.rupa30@gmail.com
ARUNJUNAISELVAM P
arunjunaiselvam.p95@wipro.com
arunjunai.carrer@gmail.com
pranob.kalita@wipro.com
pranob.kalita90@gmail.com
TINNALURI V N PRASANTH
prasanth.tinnaluri@wipro.com
naga.prasanth985@gmail.com
KONDALA SUMATHI
sumathi.kondala@wipro.com
sumathi.kondala@gmail.com
DASIKA KRISHNA
dasika.krishna@wipro.com
dasikakrishnas@gmail.com
Lines
Analog devises, such as a random-scan display
or a vector plotter, display a straight line
smoothly from one endpoint to another. Linearly
varying horizontal and vertical deflection
voltages are generated that are proportional to
the required changes in the x and y directions to
produce the smooth line.
y2
y1
m slope
c y-intercept
y 2 y1 y
m
x2 x1 x
5
x1
x2
Slope
+ve
if |m| = 1
45
45
= 45
if |m| 1
-45 < < 45
if |m| 1
45 < < 90 or
-90 < < -45
-ve
y=x
m=1
c=0
x
0
1
2
3
4
5
6
7
8
7
|m| =
1
y
y
0
1
2
3
4
5
6
7
8
4
3
1
0
|m|
y=x+ 1
1
m=
c = 1x
round(y)
0
1
1
1.5
1
2
2
3
2
2.5
2
3
4
5
3
3.5
3
4
6
7
4
4.5
4
5
0
0
|m|
y = 3x - 2 1
m=3
c = -2
round(y)
-2
-2
10
10
13
13
16
19
16
outside
19
6
5
0
0
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
dx
2dy dx)
y5
At x1 :
y3
lin
True
y2
y0
13
2dy
dy
y1
x0
x1
x2
x3
3dy
x4
4dy
x5
s1 = dy
t1 = dx - dy
d1 = (si - ti) = dy - (dx - dy) = 2dy
- dx
but 2dy dx di 0 y stays
the same
At
x2next
: pixel is at (x ,y )
hence
1
1
s2 = 2dy
t2 = dx - 2dy
d2 = (s2 t2) = 2dy - (dx - 2dy) =
4dy - dx
Suppose d2 0 y is
incremented
At
x3 next
:
hence
pixel is at (x2,y2)
s3 = 3dy - dx
t2 = 2dx - 3dy
d3 = (s2 t3) = 6dy - 3dx 0
so y stays the same
hence next pixel is at (x3,y2)
In General
xi+1 = xi
= di + 2dx
if di 0 then xi+1 = xi + 1
di+1 = di + 2(dx dy)
14
yi+1 = yi +2dy
1 and 2(dy-dx) can be
Note: For |m| 1 the constants
calculated once,
so the arithmetic will involve only integer addition and
Example
(30,18)
dx = 10
dy = 8
initial decision d0 = 2dy dx = 6
Also 2dy = 16, 2(dy dx) = -4
i
di
(xi+1,yi+1)
0
1
2
3
4
5
6
7
8
9
6
2
-2
14
10
6
2
-2
14
10
(21,11)
(22,12)
(23,12)
(24,13)
(25,14)
(26,15)
(27,16)
(28,16)
(29,17)
(30,18)
19
18
17
16
15
14
13
15
12
(20,10)
16
Special cases
Special cases can be handled separately
Horizontal lines (y = 0)
Vertical lines (x = 0)
Diagonal lines (|x| = |y|)
17
Circle Equations
Polar form
x = rCos
y = rSin
(r = radius of circle)
y
P=(rCos, rSin)
r
rCos )
18
rSin )
x
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
x
y
P x, r 2 x 2
r
y
x
20
Circle algorithms
Step through x-axis to determine y-values
Disadvantages:
21
Circle Algorithms
Use 8-fold symmetry and only compute
(-y, x)
(x, y)
45
(-y, -x)
(y, -x)
(-x, -y)
22
(y, x)
(x, -y)
General Principle
The circle function:
f circle ( x, y ) x 2 y 2 r 2
and
23
p1
p3
yi - 1
p2
D(si)
D(ti)
r
xi xi + 1
After point p1, do we choose p2 or p3?
24
The Algorithm
x0 = 0
y0 = r
p0 = [12 + r2 r2] + [12 + (r-1)2 r2] = 3
2r
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
26
Example
r = 10
p0 = 3 2r = -17
Initial point (x0, y0) = (0, 10)
10
i
0
pi
-17
xi, yi
(0, 10)
9
8
7
27
-11
(1, 10)
-1
(2, 10)
13
(3, 10)
-5
(4, 9)
Midpoin
t
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
pi f circle ( xi 1, yi 12 )
( xi 1) 2 ( yi 12 ) 2 r 2
If pi < 0, the midpoint is inside the circle and
Decision Parameters
Decision Parameters are obtained using
incremental calculations
pi 1 f circle ( xi 1 1, yi 1 12 )
Note:
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
where yi+1 is either yi or yi-1 depending on the sign of
pi
30
1.
2.
3.
The Algorithm
p0 f circle (1, r 12 ) 1 ( r 12 ) 2 r 2 54 r
31
6.
Example
i
0
r = 10
p0 = 1 r = -9 (if r is integer round p0 = 5/4 r to
integer)
Initial point (x0, y0) = (0, 10)
pi
-9
20
10
9
8
-6
(2, 10)
20
-1
(3, 10)
20
32
(4, 9)
18
-3
(5, 9)
10
18
(6, 8)
12
16
(7, 7)
Midpoint function
void plotpoints(int x,
{
setpixel(xcenter+x,
setpixel(xcenter-x,
setpixel(xcenter+x,
setpixel(xcenter-x,
setpixel(xcenter+y,
setpixel(xcenter-y,
setpixel(xcenter+y,
setpixel(xcenter-y,
}
33
int y)
ycenter+y);
ycenter+y);
ycenter-y);
ycenter-y);
ycenter+x);
ycenter+x);
ycenter-x);
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);
}
}
Ellipse-Generating Algorithms
Ellipse A modified circle whose radius varies from a
F1
d1
P=(x,y)
F2
d2
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
( x x1 ) 2 ( y y1 ) 2 ( x x2 ) 2 ( y y2 ) 2 constant
ry
rx
Polar coordinates:
35
y yc
1
rx
ry
Cartesian coordinates:
x xc
x xc rx cos
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
(-x, -y)
36
(x, -y)
Ellipse Algorithms
f ellipse ( x, y ) ry2 x 2 rx2 y 2 rx2 ry2
Decision parameter:
Slope = -1
ry
rx
37
2ry2 x
dy
Slope
2
dx
2rx y
Ellipse Algorithms
Slope = -1
1
ry 2
rx
dx
2ry x 2rx y
2ry x 2rx y
38
Midpoin
t
yi-1
xi
xi+1 xi+2
ellipse
OR
p1i 1 p1i 2ry2 ( xi 1) 2 ry2 rx2 ( yi 1 12 ) 2 ( yi 12 ) 2
where yi+1 = yi
or
40
yi+1 = yi 1
2
r
if p1i 0
y i 1 y
x yi 1
Use only addition and subtraction by
obtaining 2ry2 x and 2rx2 y
At 2initial
ry2 x 0 position (0, ry)
2rx2 y 2rx2 ry
41
Region 2
Over region 2, step in the negative y direction and
midpoint is taken between horizontal pixels at each
step.
yi
Midpoin
t
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
42
OR
p 2i 1 p 2i 2rx2 ( yi 1) rx2 ry2 ( xi 1 12 ) 2 ( xi 12 ) 2
where xi+1 = xi
or
43
xi+1 = xi + 1
2
r
y
r
y i 1
x i 1
x
if p 2i 0
if p 2i 0
p 20 f ellipse ( x0 12 , y0 1)
ry2 ( x0 12 ) 2 rx2 ( y0 1) 2 rx2 ry2
44
2ry2 x 2rx2 y
p 2the
p 2i point
2ry xi 1is (x
2rix +
yi 1,
rxi 1) and
otherwise,
i 1 next
1 y
2
46
Example
rx = 8 ,
2ry2x = 0
ry = 6
(with increment 2ry2 = 72)
47
pi
2rx2yi+1
-332
(1, 6)
72
768
-224
(2, 6)
144
768
-44
(3, 6)
216
768
208
(4, 5)
288
640
-108
(5, 5)
360
640
288
(6, 4)
432
512
244
(7, 3)
504
Example
Region 2
(x0, y0) = (7, 3)
p 20 f ellipse (7 12 , 2) 151
i
pi
-151
(8, 2)
576
256
233
(8, 1)
576
128
745
(8, 0)
6
5
4
3
48
2rx2yi+1
Stop at y = 0
49
}
Thank You