Download as pdf or txt
Download as pdf or txt
You are on page 1of 38

SCHOOL OF COMPUTING SCIENCE

AN ENGINEERING
PRACTICAL FILE

• COURSE NAME: COMPUTER GRAPHICS LAB

• COURSE CODE: BCAC3113

• PROGRAM: BACHELOR OF COMPUTER APPLICATION (BCA)

• YEAR: 3rd

• SEMESTER: 5th

• SESSION:

Name: Mayank raj Section: BCA- 3

Admission No. : 19SCSE1110002 Submitted to:


Enrollment No. : 19041110002 Kuldeep kaswan
TITLE DATE OF SUBMISSIONDATE
S.NO EXECUTION SIGNATURE

1. Study of basic graphics functions defined in “graphics.h”

2. To implement DDA(Digital Differential Algorithm) for line


drawing .

3. To implement Bresenham’s algorithm for line drawing

4. To implement Bresenham's algorithm for circle drawing

5. To implement Midpoint algorithm for circle drawing

6. To implement Midpoint algorithm for ellipse drawing

7. To perform 2D Rotation Transformation

8. To perform 2-D Translation Transformation

9 To perform 2-D Scaling Transformation

10 To perform 2-D Reflection Transformation

11 To perform a composite Transformation using 2D


Transformation

12 To implement Cohen-Sutherland 2D Line clipping

13 To implement Sutherland Hodgeman Polygon clipping


algorithm

14 To implement Weiler Amerton polygon clipping

15 Value added Experiment:- Designing simple animation using


transformations
Experiment No. 1
Aim :-

Study of basic graphics functions defined in “graphics.h”

BASIC GRAPHICS FUNCTIONS DEFINED IN “GRAPHICS.H”:-

1. Putpixel:- Putpixel function is to draw the pixel on the screen. Pixel is small dot on the screen.
Syntax:-putpixel(x co-orinate, y co-ordinate,COLOR); Example: – putpixel(100,100,BLUE);

2. SetbkColor:- Setbkcolor function is used to set background color of the screen.


Syntax:setbkcolor(COLOR); Example:-setbkcolor(RED);

3. Setlinestyle:- setlinestyle function is used to set the current line style, width and pattern. Syntax:-
setlinestyle(linestyle, pattern, thickness); Example:-setlinestyle(SOLID_LINE,1,2);

4. Setcolor - setcolor is to set color of the objects which is to be drawn after this setcolor line.
Syntax:-setcolor(COLOR); Example:-setcolor(RED);

5. Rectange:- Rectangle function is used to draw the rectangle on the screen. X1,y1 are the lower
left co-ordinates of the rectangle and the x2,y2 are the upper right co-ordinates of the rectangle.
Syntax:– rectangle(x1,,y1,x2,y2); Example:– rectangle(100,100,200,200);

6. Textheight:- textheight returns the height of a string in pixels. Syntax:-textheight(STRING);


Example:-i=textheight(“HELLO”);

7. Textwidth:- textwidth returns the width of a string in pixels. Syntax:-textwidth(STRING);


Example:-i=textwidth(“HELLO”);

8. Getx:- getx returns the current position’s of x o-ordinate. Syntax:-getx(); Example:-x=getx();

9. Gety:- gety returns the current position’s of y co-ordinate. Syntax:-gety(); Example:-y=gety();

10. Getmaxx:- Purpose:-getmaxxreturns the maximum x co-ordinate on the screen. Syntax:getmaxx();


Example:-maxx=getmaxx();

11. Getmaxy:- getmaxy returns the maximum y co-ordinate on the screen. Syntax:-getmaxy();
Example:-maxy=getmaxy();

12. Line:- Line function is used to draw the line on the screen. Syntax: line(x1,y1,x2,y2);
Example:line(100,100,200,100);
13. Closegraph:- closegraph function shut down the graphic system. Syntax:-closegraph(); Example:-
closegraph();

14. Moveto:- moveto function moves current cursor position on the screen. Syntax:-moveto(x
coordinate, y co-ordinate); Example:-moveto(getmaxx/2, getmaxy/2);
15. Settextstyle:- settextstyle sets the current text characteristics like font, direction and size. Syntax:-
settextstyle(font, direction size); Example:-settextstyle(1,1,10);

16. Circle:- Circle function is used to draw the circle on the screen. Syntax:– circle(x,y,radius);
Example:circle(100,100,50);

17. Cleardevice:- cleardevice function is used to clear the contents or graphic images on the screen in
graphics mode. Syntax:cleardevice(); Example:cleardevice();

18. Outtextxy:- outtextxy function is used to print the text on the screen in graphics mode.
Syntax:outtext(x,y,text); Example:-outtextxy(100,100,”HELLO”);

19. Sector: sector function draws and fills an elliptical pie slice. Syntax:sector(x, y, starting angle,
ending angle, xradius, yradius); Example:sector(100,100,45 135 100 50);

20. Arc:- arc draws the arc on the screen, arc is a part of the circle. Syntax:arc(x, y, starting angle,
ending angle, radius); Example:arc( 100,100,90,180,50);

21. Setfillstyle:- setfillstyle is used to set the color and style to be filled in the object using the flood
fill method. Syntax:stefillstyle(STYLE, COLOR); Example:setfillstyle(1,RED)

22. Floodfill:- floodfill function is used to fill the color in the object, object may be circle, rectangle
or any other closed image. Syntax:floodfill(x,y,boundary color); Example:
floodfill(100,100,BLUE);

23. Ellipse:- ellipse function is used to draw the ellipse on the screen. Syntax:ellipse(x, y, starting
angle, ending angle, xradius, yradius); Example:ellipse(100,100,90,200,20,20);

24. Outtext:- outtext function is used to display the text on the screen, using this function text is
display in the current position. Syntax:outtext(STRING); Example:outtex(“HELLO”);

25. Getcolor:- getcolor returns the current drawing color. Syntax:getcolor(); Example:intclr =
getcolor();

26. Getpixel:- getpixel gets the color of a specified pixel. Syntax:getpixel(x,y); Example:
color=getpixel(100,100);

Experiment No. 2
Aim :-

To implement DDA(Digital Differential Algorithm) for line drawing

Algorithm:-

Step1: Start Algorithm

Step2: Declare x1,y1,x2,y2,dx,dy,x,y as integer variables.

Step3: Enter value of x1,y1,x2,y2.


Step4: Calculate dx = x2-x1

Step5: Calculate dy = y2-y1

Step6: If ABS (dx) > ABS (dy)


Then step = abs (dx)
Else

Step7: xinc=dx/step
yinc=dy/step
assign x = x1
assign y = y1

Step8: Set pixel (x, y)

Step9: x = x + xinc
y = y + yinc
Set pixels (Round (x), Round (y))

Step10: Repeat step 9 until x = x2

Step11: End Algorithm

Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h> #include
<dos.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,step;
int gd=DETECT,gm;
int i;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("mansi gupta 19SCSE1040163\n");
printf("Enter the value of x1 and y1:");
scanf("%f%f",&x1,&y2); printf("Enter the value
of x2 and y2:"); scanf("%f%f",&x2,&y2);
dx=abs(x2-x1); dy=abs(y2-y1); if(dx>=dy)
step=dx; else step=dy; dx=dx/step;
dy=dy/step; x=x1; y=y1; i=1;
while(i<=step)
{
putpixel(x,y,WHITE);
x=x+dx; y=y+dy;
i=i+1; delay(40);
}
getch();
closegraph();
}

Output:-

Experiment No. 3
Aim :-

To implement Bresenham’s algorithm for line drawing

Algorithm:-

Step 1: Enter the 2 end points for a line and store the left end point in (X0,Y0).

Step 2: Plot the first point by loading (X0,Y0) in the frame buffer.

Setp 3: determine the initial value of the decision parameter by calculating the constants dx, dy, 2dy and
2dy-2dx as P0 = 2dy –dx.
Step 4: for each Xk, conduct the following test, starting from k=0. If Pk <0, then the next point to be
plotted is at (Xk+1, Yk) and Pk+1 = Pk + 2dy

Else, the next point is (Xk+1, Yk+1) and Pk+1 = Pk + 2dy –2dx (step 3)

Step 5: iterate through step (4) dx times.

Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h> #include
<dos.h>
void main()
{
float x,y,x1,y1,x2,y2,dx,dy,pk;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("mansi gupta 19SCSE1040163\n"); printf("Enter the
value of x1 and y1:"); scanf("%f%f",&x1,&y2);
printf("Enter the value of x2 and y2:");
scanf("%f%f",&x2,&y2);
dx=x2-x1;
dy=y2-y1;
x=x1; y=x1;
pk=2*dy-dx;
while(x<=x2)
{
if(pk<=0)
{
pk+=2*dy;
x=x+1;
}
else
{
pk+=2*dy-2*dx;
x=x+1;
y=y+1;
}
putpixel(x,y,WHITE);
}
getch();
closegraph();
}
Output:-

Program No. 4
Aim :-

To implement Bresenham's algorithm for circle drawing

Algorithm:-

Step1: Start Algorithm

Step2: Declare p, q, x, y, r, d variables , p, q are coordinates of the center of the circle r is the radius of the
circle

Step3: Enter the value of r

Step4: Calculate d = 3 - 2r

Step5: Initialize x=0


&nbsy= r

Step6: Check if the whole circle is scan converted


If x > = y
Stop

Step7: Plot eight points by using concepts of eight-way symmetry. The center is at (p, q). Current active
pixel is (x, y).
Step8: Find location of next pixels to be scanned If d < 0
then d = d + 4x + 6
increment x = x + 1
If d ≥ 0
then d = d + 4 (x - y) + 10
increment x = x + 1
decrement y = y - 1

Step9: Go to step 6

Step10: Stop Algorithm

Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <dos.h>
void main(){
float p,q,x,y,r,d; int gd=DETECT,gm;
printf("Mansi gupta 19 scse1040163n");
printf("Enter the coordinate of the centre of a circle:");
scanf("%f%f",&p,&q);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Enter the value of radius:");
scanf("%f",&r); y=r; x=0; d=3-2*r;
while(x<=y)
{
putpixel(x+p,y+q,WHITE);
putpixel(y+p,x+q,WHITE); putpixel(-
y+p,x+q,WHITE); putpixel(-x+p,y+q,WHITE);
putpixel(-x+p,-y+q,WHITE); putpixel(-y+p,-
x+q,WHITE); putpixel(y+p,-x+q,WHITE);
putpixel(x+p,-y+q,WHITE);
delay(40);
if(d<0)
{
d=d+(4*x)+6;
x=x+1;
} else
{
d=d+(4*(x-y))+6;
x=x+1;
y=y-1;
}

}
getch();}
Output:

Program No. 5
Aim :-

To implement Midpoint algorithm for circle drawing.

Algorithm:-

Step 1:- Input radius r and circle center (xc, yc ). set the first point (x0 , y0 ) = (0, r ).

Step 2:- Calculate the initial value of the decision parameter as p0 = 1 – r.

Step 3:- At each xk position, starting at k = 0, perform the following test:


If pk< 0, plot (xk + 1, yk ) and pk+1 = pk + 2xk + 1 + 1,
Otherwise,
plot(xk+ 1, yk – 1 ) and pk+1 = pk + 2xk+1 + 1 – 2yk+1, where
2xk + 1 = 2xk + 2 and 2yk + 1 = 2yk – 2.
Step 4:- Determine symmetry points on the other seven octants.

Step 5:- Move each calculated pixel position (x, y) onto the circular path centeWHITE on (xc, yc) and
plot the coordinate values: x = x + xc , y = y + yc

Step 6:- Repeat steps 3 though 5 until x >= y.

Step 7:- For all points, add the center point (xc, yc )
Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h> #include
<dos.h>
void main()
{
float p,q,x,y,r,d;
int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi"); printf("Mansi
gupta 19SCSE1040163\n");
printf("Enter the coordinate of the centre of a circle:");
scanf("%f%f",&p,&q);
printf("Enter the value of radius:");

scanf("%f",&r);
y=r; x=0;
d=1-r;
while(x<=y)
{
putpixel(x+p,y+q,WHITE);
putpixel(y+p,x+q,WHITE); putpixel(-
y+p,x+q,WHITE); putpixel(-x+p,y+q,WHITE);
putpixel(-x+p,-y+q,WHITE); putpixel(-y+p,-
x+q,WHITE); putpixel(y+p,-x+q,WHITE);
putpixel(x+p,-y+q,WHITE);
delay(40);
if(d<0)
{ d=d+(2*x)+1;
x=x+1;
} else {
d=d+(2*(x-y))+1;
x=x+1;
y=y-1; }

}
getch();
}
Output:

Program No. 6
Aim :-

To implement Midpoint algorithm for ellipse drawing

Algorithm:-

Step 1:- Take input radius along x axis and y axis and obtain center of ellipse.

Step 2:- Initially, we assume ellipse to be centeWHITE at origin and the first point as : (x, y0)= (0, ry).

Step 3:- Obtain the initial decision parameter for region 1 as: p10=ry2+1/4rx2-rx 2ry

Step 4:- For every xk position in region 1 :


If p1k<0 then the next point along the is (xk+1 , yk) and p1k+1=p1k+2ry2xk+1+ry2
Else, the next point is (xk+1, yk-1 )
And p1k+1=p1k+2ry2xk+1 – 2rx2yk+1+ry2

Step 5:- Obtain the initial value in region 2 using the last point (x0, y0) of region 1 as:
p20=ry2(x0+1/2)2+rx2 (y0-1)2-rx2ry2

Step 6:- At each yk in region 2 starting at k =0 perform the following task.


If p2k>0 the next point is (xk, yk-1) and p2k+1=p2k-2rx2yk+1+rx2
Step 7:- Else, the next point is (xk+1, yk -1) and p2k+1=p2k+2ry2xk+1 -2rx2yk+1+rx2

Step 8:- Now obtain the symmetric points in the three quadrants and plot the coordinate value as:
x=x+xc, y=y+yc

Step 9:- Repeat the steps for region 1 until 2ry2x>=2rx2y

Source Code:-

#include <graphics.h>
#include <stdio.h>
#include <conio.h>
#include <math.h> #include
<dos.h>
void main()
{
float rx,x,y,ry,d1,d2,a,b,p,q;
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("mansi gupta 19SCSE1040163\n");
printf("Enter the coordinate of the centre of a ellipse:");
scanf("%f%f",&p,&q);
printf("Enter the value of major and minor axis:");
scanf("%f%f",&rx,&ry);
y=ry;
x=0;
d1=(ry*ry)+(0.25*rx*rx)-
(rx*rx*ry); a=2*ry*ry*x;
b=2*rx*rx*y;

while(a<b)
{
putpixel(x+p,y+q,WHITE); putpixel(-
x+p,y+q,WHITE); putpixel(x+p,-y+q,WHITE);
putpixel(-x+p,-y+q,WHITE);
delay(40);
if(d1<0)
{
x=x+1;
a=a+(2*ry*ry);
d1=d1+(2*ry*ry*x)+(ry*ry);
} else {
a=a+2*ry*ry;
b=b-2*rx*rx;
d1=d1+(2*ry*ry*x)-2*rx*rx*y+(ry*ry);
x=x+1;
y=y-1;
}

}
d2=((ry*ry)*((x+0.5)*(x + 0.5)))+((rx * rx)*((y - 1)*(y-1)))-(rx*rx*ry*ry);
while(y>=0)
{
putpixel(x+p,y+q,WHITE);
putpixel(-x+p,y+q,WHITE);
putpixel(x+p,-y+q,WHITE);
putpixel(-x+p,-y+q,WHITE);
delay(40);
if(d2>0)
{
b=b-2*rx*rx;
d2=d2+(rx*rx)-b;
y=y-1;
}
else
{
x=x+1;y=y-1;
a=a+2*ry*ry; b=b-
2*rx*rx;
d2=d2+a-b+(rx*rx);
}
}
getch();
}

Output:
Program
No. 7
Aim :-

To perform 2D Rotation Transformation

Algorithm:-

x = rcosB, y = rsinB x’ = rcos(A+B) = r(cosAcosB – sinAsinB) = rcosBcosA –


rsinBsinA = xcosA – ysinA y’ = rsin(A+B) = r(sinAcosB + cosAsinB) =
rcosBsinA + rsinBcosA = xsinA + ycosA

Source Code:-

#include<stdio.h>
#include<graphics.h> #include<math.h>
void main()
{
int gd=0,gm,x1,y1,x2,y2;
double s,c, angle;
initgraph(&gd, &gm, "C:\\TURBOC3\\BGI"); printf("Mansi
gupta 19SCSE1040163\n"); printf("Enter coordinates of line:
"); scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
printf("Enter rotation angle: ");
scanf("%lf", &angle); c =
cos(angle *3.14/180); s =
sin(angle *3.14/180); x1 =
floor(x1 * c + y1 * s); y1 =
floor(-x1 * s + y1 * c); x2 =
floor(x2 * c + y2 * s); y2 =
floor(-x2 * s + y2 * c); line(x1,
y1 ,x2, y2); getch();
closegraph();
}

Output:

Program No. 8
Aim :-
To perform 2-D Translation Transformation

Algorithm:-

Suppose, If point (X, Y) is to be translated by amount Dx and Dy to a new location (X’, Y’) then new
coordinates can be obtained by adding Dx to X and Dy to Y as: X' = Dx + X Y' = Dy + Y or P' = T + P
where
P' = (X', Y'),
T = (Dx, Dy ),
P = (X, Y)
Here, P(X, Y) is the original point. T(Dx, Dy) is the translation factor, i.e. the amount by which the point
will be translated. P'(X’, Y’) is the coordinates of point P after translation.

Source Code:-

#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdio.h>
void triangletranslate()
{ int
x,y;
line(75,85,90,120);
line(90,120,110,85);
line(110,85,75,85);
printf("Enter translation vector in x direction and y direction:");
scanf("%d%d",&x,&y); line(75+x,85+y,90+x,120+y);
line(90+x,120+y,110+x,85+y);
line(110+x,85+y,75+x,85+y);
getch();;
closegraph();
}
void rectangletranslate()
{ int
x,y;
line(75,8
5,75,120
);
line(75,1
20,100,1
20);
line(100,
120,100,
85);
line(100,
85,75,85
);
printf("Enter translation vector in x direction and y direction:");
scanf("%d%d",&x,&y); line(75+x,85+y,75+x,120+y);
line(75+x,120+y,100+x,120+y);
line(100+x,120+y,100+x,85+y);
line(100+x,85+y,75+x,85+y);
getch();;
closegraph();
}
void circletranslate()
{
int x,y,radius; printf("Enter
the radius:");
scanf("%d",&radius);
circle(90,120,radius);
printf("Enter translation vector in x direction and y direction:");
scanf("%d%d",&x,&y); circle(90+x,120+y,radius);
getch();
closegraph();
}
void main()
{
int ch; int
gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
printf("mansi gupta 19SCSE1040163\n");
printf("1.translate a triangle\n2.translate a Circle\n3.translate a rectangle\n4.exit"); printf("\nEnter
choice:");
scanf("%d",&ch);
clrscr();
switch(ch)
{
case 1:triangletranslate();break; case
2:circletranslate();break; case
3:rectangletranslate();break; case
4:printf("THANK YOU");break;
default:printf("INCORRECT INPUT");
}
getch();}

Output:
Program No. 9
Aim :-

To perform 2-D Scaling Transformation


Algorithm:-

Step 1:- Input x, y, z.


Step 2:- Input scaling factor sx and sy.
Step 3:- Draw a triangle using line().
Step 4:- Scale the point of the triangle and redraw it.

Scaling operation can be achieved by multiplying each vertex coordinate (x, y) of the polygon by scaling
factor sx and sy to produce the transformed coordinates as (x’, y’).

So, x’ = x * sx and y’ = y * sy.

Source Code:-

#include <conio.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gm,gd=DETECT,x=100;
int y=200;
int z=150;
int sx,sy;
initgraph(&gd,&gm,"C:\\TURBOC3\\BGI");
initgraph(&gd,&gm, "");
printf("mansi gupta 19SCSE1040163\n");

line(x,x,y,x);
line(x,x,z,z);
line(y,x,z,z);

printf("Enter the scaling factor:");


scanf("%d%d",&sx,&sy);

line(sx*x,sy*x,sx*y,sy*x);
line(sx*x,sy*x,sx*z,sy*z);
line(sx*y,sy*x,sx*z,sy*z);

getch();
closegraph();
}

Output:
Program No. 10
Aim :-

To perform 2-D Reflection Transformation

Algorithm:-

Step 1:- Create an object in the 2nd graph quadrant by providing the coordinates.

Step 2:- For reflection along X-axis:


1. Y-axis coordinates will remain the same.
2. Obtain laterally inverted X-axis coordinates distance by calculating the distance between the X
coordinate of the source object and its nearest surface along the X-axis.
3. Draw another object using the laterally inverted X coordinates obtained from the above step while
keeping the Y coordinate the same.
4. The above steps will generate a mirror image or reflection of the source object.
5. In this scenario, the reflected object will be formed at the 1st quadrant.

Step 3:- For reflection along Y-axis:


1. X-axis coordinates will remain the same.
2. Obtain laterally inverted Y-axis coordinates’ distance by calculating the distance between the Y
coordinate of the source object and its nearest surface along the Y-axis.
3. Draw another object using the laterally inverted Y coordinates obtained from the above step while
keeping the X coordinate the same.
4. The above steps will generate a mirror image or reflection of the source object.
5. In this scenario, the reflected object will be formed at the 3rd quadrant.

Step 4:- For reflection along the origin:


1. X-axis and Y-axis coordinates will change.
2. Obtain laterally inverted Y-axis coordinate distance by calculating the distance between the Y
coordinate of the source object and its nearest surface along the Y-axis.
3. Obtain laterally inverted X-axis coordinate distance by calculating the distance between the X
coordinate of the source object and its nearest surface along the X-axis.
4. Draw another object using the laterally inverted X and Y coordinates obtained from the above
steps.
5. The above steps will generate a mirror image or reflection of the source object.
6. This time, the reflection is about origin as both X & Y coordinates have changed, pushing the
object inside out. In this scenario, the reflected object will be formed at the 4th quadrant.

Source Code:-

#include <conio.h>
#include <graphics.h>
#include <stdio.h>
void main()
{
int gm,gd=DETECT,ax,x1=100;
int x2 = 100, x3 = 200, y1 = 100;
int y2 = 200, y3 = 100; initgraph(&gd,
&gm,"C:\\TURBOC3\\BGI");
initgraph(&gd, &gm, "");
printf("mansi gupta 19SCSE1040163\n")
line(getmaxx()/2,0,getmaxx()/2,getmaxy()); line(0,
getmaxy()/2,getmaxx(),getmaxy()/2);
printf("Before Reflection Object in 2nd Quadrant");
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();

printf("\nAfter Reflection");

// along origin

line(getmaxx()-x1,getmaxy()-y1,getmaxx()-x2,getmaxy()-
y2); line(getmaxx()-x2,getmaxy()-y2,getmaxx()-
x3,getmaxy()-y3); line(getmaxx()-x3,getmaxy()-
y3,getmaxx()-x1,getmaxy()-y1);

// Reflection along x-axis i.e.,

line(getmaxx()-x1,y1,getmaxx()-x2,y2); line(getmaxx()-x2,y2,getmaxx()-
x3,y3); line(getmaxx()-x3,y3,getmaxx()-x1,y1);

// Reflection along y-axis i.e.,


line(x1, getmaxy() - y1, x2,getmaxy() - y2);
line(x2, getmaxy() - y2, x3,getmaxy() - y3);
line(x3, getmaxy() - y3, x1,getmaxy() - y1);
getch();
closegraph();
}
Output:

Program No. 11
Aim :-

ToperformacompositeTransformationusing2DTransformation

Algorithm/Theory:-

A number of transformations or sequence of transformations can be combined into single one called as
composition. The resulting matrix is called as composite matrix. The process of combining is called as
concatenation.

Suppose we want to perform rotation about an arbitrary point, then we can perform it by the sequence of
three transformations

1. Translation
2. Rotation
3. Reverse Translation

The ordering sequence of these numbers of transformations must not be changed. If a matrix is represented
in column form, then the composite transformation is performed by multiplying matrix in order from right
to left side. The output obtained from the previous matrix is multiplied with the new coming matrix.

Source Code:-
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
int xa, xb, xc, ya, yb, yc, y1a, y1b, y1c, x1a, x1b, x1c, x2a, x2b, x2c, y2a, y2b, y2c;
int x3a, x3b, x3c, y3a, y3b, y3c, x4a, x4b, x4c, y4a, y4b, y4c, x5a, x5b, x5c, y5a, y5b, y5c;
int tx, shx, t, ch, shy;
float ang, theta, sx, sy;
int main(void) {
int gdriver = DETECT, gmode, errorcode;
initgraph( &gdriver, &gmode, "C:\\turboc3\\BGI");
printf("mansi gupta 19SCSE1040163");
printf("\n\t\t\t 2D Composite Transformations");
printf("\n\n Enter all coordinates values :");
scanf("%d %d %d %d %d %d", &xa, &ya, &xb, &yb, & xc, &yc);
printf("\n\n The original Image");
line(xa, ya, xb, yb);
line(xb, yb, xc, yc);
line(xc, yc, xa, ya);
printf("\n\n Enter the value tranlsation factor :");
scanf("%d", &tx);
printf("\n\n After Translation ");
x1a = xa + tx;
x1b = xb + tx;
x1c = xc + tx;
y1a = ya;
y1b = yb;
y1c = yc;
delay(1);
printf("\n\n Next Operation is Rotation");
printf("\n\n Enter the rotation angle :");
scanf("%f", & ang);
theta = ((ang * 3.14) / 180);
x2a = x1a * cos(theta) - y1a * sin(theta);
y2a = x1a * sin(theta) + y1a * cos(theta);
x2b = x1b * cos(theta) - y1b * sin(theta);
y2b = x1b * sin(theta) + y1b * cos(theta);
x2c = x1c * cos(theta) - y1c * sin(theta);
y2c = x1c * sin(theta) + y1c * cos(theta);
delay(1);
printf("\n\n Next Operation is Scaling");
printf("\n\n Enter the Scale factor :");
scanf("%f %f", &sx, &sy);
x3a = x2a + sx;
y3a = y2a + sy;
x3b = x2b + sx;
y3b = y2b + sy;
x3c = x2c + sx;
y3c = y2c + sy;
clrscr();
printf("\nABHIMANYU KUMAR 19SCSE1040145\n Final Shape after all the transformations: ");
line(x3a, y3a, x3b, y3b);
line(x3b, y3b, x3c, y3c);
line(x3c, y3c, x3a, y3a);
getch();
closegraph();
return 0;
}
Output:

Program No. 12
Aim :-

To implement Cohen-Sutherland 2D Line clipping

Algorithm:-

Step1:Calculate positions of both endpoints of the line

Step2:Perform OR operation on both of these end-points

Step3:If the OR operation gives 0000


Then
line is considered to be visible
else
Perform AND operation on both endpoints
If And ≠ 0000
then the line is invisible
else
And=0000
Line is considered the clipped case.

Step4:If a line is clipped case, find an intersection with boundaries of the window
m=(y2-y1 )(x2-x1)
(a) If bit 1 is "1" line intersects with left boundary of rectangle window
y3=y1+m(x-X1)
where X = Xwmin
where Xwminis the minimum value of X co-ordinate of window

(b) If bit 2 is "1" line intersect with right boundary


y3=y1+m(X-X1)
where X = Xwmax
where X more is maximum value of X co-ordinate of the window

(c) If bit 3 is "1" line intersects with bottom boundary


X3=X1+(y-y1)/m
where y = ywmin
ywmin is the minimum value of Y co-ordinate of the window

(d) If bit 4 is "1" line intersects with the top boundary


X3=X1+(y-y1)/m
where y = ywmax
ywmax is the maximum value of Y co-ordinate of the window

Source Code:-

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>

typedef struct coordinate {


int x, y;
char code[4];
}
PT;

void drawwindow();
void drawline(PT p1, PT p2);
PT setcode(PT p);
int visibility(PT p1, PT p2);
PT resetendpt(PT p1, PT p2);

void main() {
int gd = DETECT, v, gm;
PT p1, p2, p3, p4, ptemp;
printf("\mansi gupta 19SCSE1040163\n");
printf("\nEnter x1 and y1\n");
scanf("%d %d", & p1.x, & p1.y);
printf("\nEnter x2 and y2\n");
scanf("%d %d", & p2.x, & p2.y);
initgraph( &gd, & gm, "c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1, p2);
delay(500);
cleardevice();
delay(500);
p1 = setcode(p1);
p2 = setcode(p2);
v = visibility(p1, p2);
delay(500);
switch (v) {
case 0:
drawwindow();
delay(500);
drawline(p1, p2);
break;
case 1:
drawwindow();
delay(500);
break;
case 2:
p3 = resetendpt(p1, p2);
p4 = resetendpt(p2, p1);
drawwindow();
delay(500);
drawline(p3, p4);
break;
}
delay(5000);
closegraph();
}

void drawwindow() {
line(150, 100, 450, 100);
line(450, 100, 450, 350);
line(450, 350, 150, 350);
line(150, 350, 150, 100);
}

void drawline(PT p1, PT p2) {


line(p1.x, p1.y, p2.x, p2.y);
}

PT setcode(PT p) //for setting the 4 bit code


{
PT ptemp;
if (p.y< 100)
ptemp.code[0] = '1'; //Top
else
ptemp.code[0] = '0';
if (p.y> 350)
ptemp.code[1] = '1'; //Bottom
else
ptemp.code[1] = '0';
if (p.x> 450)
ptemp.code[2] = '1'; //Right
else
ptemp.code[2] = '0';
if (p.x< 150)
ptemp.code[3] = '1'; //Left
else
ptemp.code[3] = '0';
ptemp.x = p.x;
ptemp.y = p.y;
return (ptemp);
}

int visibility(PT p1, PT p2) {


int i, flag = 0;
for (i = 0; i< 4; i++) {
if ((p1.code[i] != '0') || (p2.code[i] != '0'))
flag = 1;
}
if (flag == 0)
return (0);
for (i = 0; i< 4; i++) {
if ((p1.code[i] == p2.code[i]) && (p1.code[i] == '1'))
flag = '0';
}
if (flag == 0)
return (1);
return (2);
}

PT resetendpt(PT p1, PT p2) {


PT temp;
int x, y, i;
float m, k;
if (p1.code[3] == '1')
x = 150;
if (p1.code[2] == '1')
x = 450;
if ((p1.code[3] == '1') || (p1.code[2] == '1')) {
m = (float)(p2.y - p1.y) / (p2.x - p1.x);
k = (p1.y + (m * (x - p1.x)));
temp.y = k;
temp.x = x;
for (i = 0; i< 4; i++)
temp.code[i] = p1.code[i];
if (temp.y<= 350 &&temp.y>= 100)
return (temp);
}
if (p1.code[0] == '1')
y = 100;
if (p1.code[1] == '1')
y = 350;
if ((p1.code[0] == '1') || (p1.code[1] == '1')) {
m = (float)(p2.y - p1.y) / (p2.x - p1.x);
k = (float) p1.x + (float)(y - p1.y) / m;
temp.x = k;
temp.y = y;
for (i = 0; i< 4; i++)
temp.code[i] = p1.code[i];
return (temp);
} else
return (p1);
}

Output:

Program No. 13
Aim :-
To implement Sutherland Hodgeman Polygon clippingalgorithm.

Algorithm:-

The polygon clipping algorithm deals with four different clipping cases. The output of each case is input for the
next case.

Case1) Left clip: In the left side polygon clipping, we only remove the left part of the polygon, which is outside the
window. We only save the portion which is inside the window.

Case2) Right clip: In the right-side polygon clipping, we only remove the right part of the polygon, which is outside
the window. We only save the portion which is inside the window.

Case3) Top clip: On the top side polygon clipping, we only remove the top part of the polygon, which is outside
the window. We only save the portion which is inside the window.

Case4) Bottom clip: In the bottom side polygon clipping, we only remove the bottom part of the polygon, which is
outside the window. We only save the portion which is inside the window.

There should be the following conditions while we clip a polygon.

Condition 1(Out-In): If the first vertex of the polygon is outside and the second vertex is inside the window, then
the output will be the intersection point and second vertex.

Condition 2(In-Out): If the first vertex of the polygon is inside and the second vertex is outside the window, then
the output will be the intersection point.

Condition 3(In-In): If both vertices of the polygon are inside the window, then the output will be the second vertex.

Condition 4(Out-Out): If both vertices of the polygon are outside the window, then the output will be nothing. It
means we found a clipped polygon.

Source Code:-

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int gd,gm,n,*x,i,k=0;
int w[]={220,140,420,140,420,340,220,340,220,140};
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("Window:-");
setcolor(RED);
drawpoly(5,w);
printf("mansi gupta 19SCSE1040163\n");
printf("Enter the no. of vertices of polygon: ");
scanf("%d",&n);
x = malloc(n*2+1);
printf("Enter the coordinates of points:\n");
k=0;
for(i=0;i<n*2;i+=2){
printf("(x%d,y%d): ",k,k);
scanf("%d,%d",&x[i],&x[i+1]);
k++;
}
x[n*2]=x[0];
x[n*2+1]=x[1];
setcolor(WHITE);
drawpoly(n+1,x);
printf("\nPress a button to clip a polygon..");
getch();
setcolor(RED);
drawpoly(5,w);
setfillstyle(SOLID_FILL,BLACK);
floodfill(2,2,RED);
gotoxy(1,1);
printf("\nThis is the clipped polygon..");
getch();
cleardevice();
closegraph();
return 0;
}
Output:
Program No. 14
Aim :-

To implement Weiler Amerton polygon clipping

Algorithm:-

 First make a list of all intersection points namely i1, i2, i3, ...
 Classify those intersection points as entering or exiting.
 Now, make two lists, one for the clipping polygon, and the other for the clipped polygon.
 Fill both the lists up in such a way that the intersection points lie between the correct vertices of each of the
polygon. That is the clipping polygon list is filled up with all the vertices of the clipping polygonalong with
the intersecting points lying between the corresponding vertices.
 Now, start at the 'to be clipped' polygon's list.
 Choose the first intersection point which has been labelled as an entering point. Follow the points in the list
(looping back to the top of the list, in case the list ends) and keep on pushing them into a vector or something
similar of the sorts. Keep on following the list until an exiting intersection point is found.
 Now switch the list to the 'polygon that is clipping' list, and findthe exiting the intersection that was
previously encountered. Now keep on following the points in this list (similar to how we followed the
previous list) until the entering intersection point is found (theone that was found in the previous 'to be
clipped' polygon's list).
 This vector now formed by pushing all the encountered points in the two lists, is now the clippedpolygon
(one of the many clipped polygons if any of the clipping polygons is concave).
 Repeat this clipping procedure (i.e. from step 5) until all the entering intersection points have been visited
once.

Source Code:-

#include<conio.h>
#include <graphics.h>
#include<dos.h>
void weiler_polygon_clipping();
void main()
{ int gd = DETECT, gm;
initgraph(&gd,&gm,"c:\\TURBOC3\\BGI");
outtextxy(50,90,"mansi gupta 19SCSE1040163");
clrscr();
outtextxy(50,80,"Weiler-Atherton Polygon Clipping");
cleardevice();
weiler_polygon_clipping();
cleardevice();
settextstyle(TRIPLEX_FONT, HORIZ_DIR, 5);
outtextxy(150,150,"THANK YOU");
getch();
closegraph();
}
void weiler_polygon_clipping()
{

outtextxy(50,90,"ABHIMANYU KUMAR 19SCSE1040145");


rectangle(70,240,180,360);
line(30,310,110,270);
line(110,270,100,295);
line(100,295,50,330);
line(50,330,110,340);
line(110,340,30,350);
line(30,310,30,350);
outtextxy(20,310,"v1");
outtextxy(110,270,"v2");
outtextxy(105,295,"v3");
outtextxy(45,330,"v4");
outtextxy(115,340,"v5");
outtextxy(20,350,"v6");
outtextxy(65,285,"v1'");
outtextxy(65,305,"v3'");
outtextxy(75,325,"v4'");
outtextxy(50,350,"v5'");
outtextxy(50,469,"Hit any key to continue...");
getch();
cleardevice();
outtextxy(50,90,"ABHIMANYU KUMAR 19SCSE1040145");
rectangle(70,240,180,360);
setcolor(11);
line(70,290,110,270);
line(110,270,100,295);
line(100,295,70,320);
line(70,290,70,320);
delay(2000);
line(70,330,110,340);
line(70,330,110,340);
line(110,340,70,350);
line(70,330,70,350);

outtextxy(50,469,"Hit any key to continue...");


getch();
}
Output:
Program No. 15
Aim :-

Value added experiment:- Designing simple animation using transformations

Source Code:-

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>

int main()
{
int gd = DETECT, gm;
char temp;
int i, maxx, midy;
initgraph(&gd, &gm, "c:\\turboc3\\bgi");
printf("mansi gupta 19SCSE1040163");
printf("Press any key to start:");
scanf("%c",&temp);
maxx = getmaxx();
midy = getmaxy()/2;
for (i=0; i < maxx-150; i=i+5) {
cleardevice();
setcolor(WHITE);
line(0, midy + 37, maxx, midy + 37);
line(i, midy + 23, i, midy);
line(i, midy, 40 + i, midy - 20);
line(40 + i, midy - 20, 80 + i, midy - 20);
line(80 + i, midy - 20, 100 + i, midy);
line(100 + i, midy, 120 + i, midy);
line(120 + i, midy, 120 + i, midy + 23);
line(0 + i, midy + 23, 18 + i, midy + 23);
arc(30 + i, midy + 23, 0, 180, 12);
line(42 + i, midy + 23, 78 + i, midy + 23);
arc(90 + i, midy + 23, 0, 180, 12);
line(102 + i, midy + 23, 120 + i, midy + 23);
line(28 + i, midy, 43 + i, midy - 15);
line(43 + i, midy - 15, 57 + i, midy - 15);
line(57 + i, midy - 15, 57 + i, midy);
line(57 + i, midy, 28 + i, midy);
line(62 + i, midy - 15, 77 + i, midy - 15);
line(77 + i, midy - 15, 92 + i, midy);
line(92 + i, midy, 62 + i, midy);
line(62 + i, midy, 62 + i, midy - 15);
circle(30 + i, midy + 25, 9);
circle(90 + i, midy + 25, 9);
delay(100);
}
getch();
closegraph();
return 0;
}

Output:

You might also like