Computer Graphics Lab Manual
Computer Graphics Lab Manual
Experiment No 01
OBJECT
ALGORITHM
1. Read the end points of a line (x1, y1) & (x2, y2).
2. If the points are same, plot the points & exit (Enter continue with the calculation).
3. x = | x2-x1 | & y = | y2-y1 |
4. if abs( x) ≥ abs(y) then
Steps =x
Else
Steps=y
5. x increment = x / steps.
6. y increment = y /steps
7. initialize (x, y) with (x1,y1)
x = x1
y = y1
8. plot the rounded coordinate (x, y)
9. initialize counter K=1
10. start the loop
x = x + x increment
y = y+ y increment
Plot the rounded coordinate(x, y)
11. Continue the loop till the counter = steps
12. Stop.
FLOW CHART
START
Read
(x1,y1),(y1,y2)
N
2
1
2
x = |X2 - X1|
Y = |Y2 - Y1|
If Y
Steps = x
abs(x) >=
abs(Y)
Steps = Y
xIncrement = x/steps
y Increment = y/steps
Initialize(x,y)
with (x1,y1)
Initialize counter
K=1
x = x + x Increment
y = y + y Increment
3 Plot [round(x) , round(y)]
K=k+1
1
3
1
3
Y If
K=1
STOP 2
4
5
EXPERIMENT No. 02
OBJECT
ALGORITHM
1. Read the line end points (x1,y1) & (x2,y2) such that they are not equal.
2. If the points are same, plot the points & exit (Else continue with the
calculation)
3. Calculate
x = |x2-x1|
y = |y2-y1|
4. [initialize the starting point]
x=x1
y=y1
5. e = 2 * y - x
[initialize value of decision variable or error to compensate for non zero
intercepts]
6. i = 1[initialize counter]
7. plot(x , y)
8. while (e ≥ 0)
{
y = y+1
e = e – 2 * x
}
x = x+1
e = e+2 * y
9. i = i+1
10. if ( i ≤ x ) then go to step 6.
11. Stop.
7
FLOW CHART
START
N 1
x = |X2 - X1|
Y = |Y2 - Y1|
Initialize(x , y)
with (x1,y1)
e= 2 * y - x
Initialize counter
i=1
Plot (x , y)
3
2
8
y = y+1 if
e = e-2 *x e≥0
Y
x = x+1
e = e+2 * y
3 i =i+1 2
If
i ≤ x
STOP 1
9
EXPERIMENT NO. 03
OBJECT
Thick Line Drawing Method
ALGORITHM
1. Inter the coordinates for the line .
p1 = (x1,y1) , p2 = (x2,y2)
2. enter the thickness.
3. draw line (x1,y1,x2,y2).
4. if ( (y2-y1) / (x2-x1) < 1)
{
wy = (thickness-1) * sqrt (pow ( (x2-x1),2)+ pow ((y2-y1),2))/(2*fabs(x2-x1));
while(i<wy)
{
i = i+1;
line(x1,y1-i,x2,y2-i);
line( x1,y1+i,x2,y2+i);
}
}
else
{
while(i<wx);
{
wx=(thickness-1)*sqrt(pow((x2-x1),2)+pow((y2-y1),2))/(2*fabs(y2-y1));
line(x1-i,y1,x2-i,y2);
line( x1+i,y1,x2+i,y2);
i= i+1;
}
}
START
Line (x1,y1,x2,y2)
i=i+1
wy=(thickness1)*sqrt(pow( Y
(x2-x1),2)+pow((y2- If (y2-y1)/(x2-
y1),2))/(2*fabs(x2-x1)); x1)
<1
N
line(x1,y1-i,x2,y2-i);
line( x1,y1+i,x2,y2+i);
i=i+1
wy = (thickness-1)*
sqrt(pow((x2-x1),2) +
pow((y2y1),2)) / ((2*fabs(y2-y1));
Y If
(i<wy)
line(x1-i,y1,x2-i,y2);
line( x1+i,y1,x2+i,y2);
N If Y
STOP
(i<wx)
12
Experiment no 04
14
OBJECT:
DDA CIRCLE DRAWING METHOD
ALGORITHM
FLOW CHART
start
Calculate
E=2-n
1
15
X2=x1+Ey1
Y2=y1-Ex2
If
Y (y1 - start_y)<E
or
(start_x - x1)>E
if the current
Y point is the
starting
point
STOP
16
Experiment 05
OBJECT
To draw the 1/8th, of the circle of a given radius.
ALGORITHM
1. Read the radius of the circle.
2. Calculate initial decision variable.
d= 3 – 2r
3. Initialize start p
x = 0, y = r
4. do
plot (x,y)
{
if d < 0 then
d = d + 4x + 6
else
{
d = d + 4 (x-y) + 10
y=y–1
}
x = x +1
} while(x<y)
5. Plot (x, y)
6. End
18
19
FLOW CHART
Start
Input
Initialize
x = 0, y = r
d = 3 – 2r
Plot (x, y)
N
If d = d + 4 (x-y) + 10
d< y=y-1
0
Y
d = d+4x+6
Increment
X=x+1
N While
x<y
stop
20
Experiment No: 06
OBJECT
Midpoint Circle Drawing Algorithm
ALGORITHM
1. Read the radius r of the circle.
2. Initialize starting position as
x=0
y=r
3. Calculate initial value of decision parameter as
P=1.25-r
4. D0
{
plot(x , y)
if (d<0)
{
x=x+1
y=y
=d+2x+2
}
else
{
x=x+1
y=y-1
d=d+2x+2y+1
}
}While (x<y)
5. Determine symmetry point
6. Stop
22
FLOW CHART
Start
Read r
Initialize
x = 0, y = r
p= 1.25– r
Plot (x, y)
If Y x = x+1
d< y = y-1
0 d = d + 2x+2
N
x = x+1
y = y-1
d = d + 2x+2
if
x<y
stop
23
Experiment no :10
OBJECT
Mid point ellipse drawing algorithm
ALGORITHM
1. read radii rx and ry.
2. Initialize starting point as
x=0
y=ry
3. calculate the initial value of decision parameter in region
1 as d1=ry2-rx2*ry+(1/4)*rx2
4. initialize dx and dy as
dx=2*ry2*x
dy=2*rx2*y
5. do
{
plot (x , y)
if (d1<0)
{
x=x+1
y=y
dx=dx+2*ry2
d1=d1+dx+ ry2
[d1=d1+2*ry2*x+2*ry2]
}
else
{
x=x+1
y=y-1
dx = dx+2*ry2
dy = dy-2rx2
d1=d1+dx-dy+ry2
[d1=d1+2*ry2*x+2ry2-(2rx2*y-2*rx2 )+ry2]
} while(dx<dy)
6. calculate the initial value of decision parameter in region 2as
d2=ry2(x+1/2)2 + rx2(y-1)2-rx2*ry2
7. do
{
plot (x,y)
if(d2>0)
{
x=x
25
y=y-1
dy=dy-2*rx2
d2=d2-dy+rx2
[d2=d2-(2*rx2y-2rx2)+rx2]
}
else
{
x=x+1
y=y-1
dy=dy-2*rx2
dx=dx+2*ry2
d2=d2+dx-dy+rx2
d2=[d2+2*ry2*x+2*ry2-(2*rx2*y-2*rx2)+ry2]
}
}while(y>0)
8. determine symmetrical points in other three quadrants
9. stop.
FLOW CHART
START
READ
Radii rx & ry
Initialize dx and dy
dx = 2*ry2*x , dy = 2*rx2*y
1
26
Plot (x , y)
x=x+1 y=y
If dx=dx+2*ry2
Y ,d1=d1+dx+ry2
d1<0
[d1=d1+2*ry2x+2*ry2+ry2]
x=x+1,y=y-1,dx=dx+2*ry2,dy=dy-2rx2
d1=d1+dx-dy+ry2
[d1=d1+2ry2x+2ry2-(2rx2y-2rx2)+ry2]
Y
If
dx<d
y
Plot (x,y)
2
x=x y=y-1
Y dy=dy-2*rx2 ,d2=d2-dy+rx2
If [d2=d2-(2*rx2y-2*rx2+rx2]
d2>0
1
27
x=x+1,y=y-1,dx=dx+2*ry2,dy=dy-2rx2
d2=d2+dx-dy+rx2
[d2=d2+2ry2x+2ry2-(2rx2y-2rx2)+ry2]
Y If
2 y>0
Determine
Symmetrical points points in other three
qudrants
STOP
28
Slope = -1
rx Region 1
Region 2
ry
Ellipse
(-x , y) (x , y)
Experiment No. 07
OBJECT
Cohen and Sutherland Subdivision Line
ALGORITHM
5. Determine the intersecting edge of the clipping window by inspecting the region codes
of two end points.
a. If region codes for both end points are non-zero, find
intersection points P1 and P2 with boundary edges of clipping window
with respect to it.
b. If region code for any one end point is non zero then find
intersection points P1 or P2 with the boundary edge of the clipping window with
respect to it.
FLOW CHART
START
Read corners
Wx1,Wy1,Wx2,Wy2
Y
If Set Bit 1
x<wx
11
N
If Y
Set Bit 2
x>wx
2
N
If Y
Set Bit 3
y<wy
2
Y
If Set Bit 4
y>wy
1
N
33
If region
Line is
codes for both
completely
end
invisible ,reject
Points=!0
the line
and
Logicalend
= !0
Line is partially
If region visible
codes do not Divide the
satisfy partially visible
Both above line segment
condition inequal parts
3
34
find intersection
points P1| and P2|
If region with boundary
codes for both Y
edges of clipping
end window with
Points=!0 respect to point P1
and P2.
find intersection
points P1| and P2| If region
with boundary Y codes for any
edges of clipping one end
window with Points=!0
respect to point P1
and P2.
STOP 2
35
Experiment No 08
OBJECT
Midpoint Subdivision of line
ALGORITHM
6. Stop.
39
FLOW CHART
START
Y
If Set Bit 1
x<w
x1
N
2
If Y
x>wx Set Bit 2
2
1 2
40
If Y
Set Bit 3
y<wy
1
N 2
Y
If Set Bit 1
y>wy
2
N
Check for visibility of 2
line
N
3
If region
codes for Y Line is completely
both end invisible ,reject the
Points=!0 line
and
Logical
end=0
N 3
41
4
3
4
3
Line is partially
If region visible
codes do not Y Divide the
satisfy partially visible
Both above line segment
condition inequal parts
N 5
STOP Y 3
42
Experiment No:09
OBJECT
Bezier Curves
ALGORITHM
1. Get four control points say A(xA, yA), B(xB, yB), C(xC, yC), D(xD, yD).
2. Divide the curve represented by points say A, B, C and D in two sections.
xAB = (xA+xB)/2
yAB = (yA+yB)/2
xBC = (xB+xC)/2
yBC = (yB+yC)/2
xCD = (xC+xD)2
yCD = (yC+yD)/2
xABC = (xAB+yBC)/2
yABC = (yAB+yBC)/2
xBCD = (xBC+yCD)/2
yBCD = (yBC+yCD)/2
xABCD = (xABC+xBCD)/2
yABCD = (yABC+yBCD)/2
3. Repeat the step 2 for section A, AB, ABC and ABCD and section ABCD, BCD, CD
and D.
4. Repeat step 3 until we have section so short that they can be replaced by straight lines.
5. Replace small section by straight by lines.
6. Stop.
45
FLOW CHART
START
Get fourcontrolpoint(xA,yA),(xB,yB),(xC,yC)
STOP
46