You are on page 1of 64

COMPUTER GRAPHICS LAB

CS2405
Lab Report

2014-2015
VII Semester

Submitted By,
NAME
311011104XXX




DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
K.C.G COLLEGE OF TECHNOLOGY
KARAPAKKAM
CHENNAI 600 097




KCG COLLEGE OF TECHNOLOGY
KARAPAKKAM

REG. NO. ___________________
LABORATORY RECORD
Name of the Lab ____COMPUTER GRAPHICS LAB - (CS2405)_____
Department of ___COMPUTER SCIENCE AND ENGINEERING___
Certified that this is a bonafide record of the work done by
____________________________ of ____CSE VII ___ Class
in the __COMPUTER ______ Laboratory during the year 2014-2015.

____________________ ____________________
Staff Member in charge H.O.D

____________________ _______________________
INT. Examiner EXT. Examiner

Date of the Examination _______________
3110111040XX
XXXXXXX

1

EX NO :1A DRAWING A LINE USING DDA
DATE :
AIM:

To write a C Program for Line Drawing using Digital Differential Analyzer (DDA)
Method.

ALGORITHM:

1. Start
2. Read the two end points of a line (x1, y1) & (x2, y2).
3. x = x2-x1 &y = y2-y1
4. Initialize (x, y) with (x1,y1)
x = x1
y = y1
5. if abs( x) > abs(y) then
steps =x
else
steps=y
6. xincrement =x / steps.
7. yincrement =y /steps
8. Plot the rounded coordinate (x, y)
9. Initialize counter k=1
10. Start the loop
x = x + xincrement
y = y+ yincrement
11. Plot the rounded coordinate(x, y)
12. Continue the loop till the counter k= steps
13. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
3110111040XX
XXXXXXX

2

int x1, y1, x2, y2,dx,dy,steps,k;
float xincrement,yincrement,x,y;
initgraph(&gd, &gm, "..\\bgi");
printf("Enter the Starting Point of x axis : ");
scanf("%d", &x1);
printf("Enter the Starting of y axis : ");
scanf("%d", &y1);
printf("Enter the End Point of x axis : ");
scanf("%d", &x2);
printf("Enter the End Point of y axis : ");
scanf("%d", &y2);
clrscr();
dx = x2 x1;
dy = y2 y1;
x=x1;
y=y1;
if(abs(dx) > abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xincrement=dx/(float)steps;
yincrement=dy/(float)steps;
putpixel(ceil(x), ceil(y), RED);
for(k=1;k<=steps;k++)
{
x=x+xincrement;
y=y+yincrement;
putpixel(ceil(x),ceil(y), RED);
}
getch();
closegraph();
}


















3110111040XX
XXXXXXX

3


INPUT:

Enter the Starting Point of x axis : 100
Enter the Starting Point of y axis : 100
Enter the End Point of x axis : 200
Enter the End Point of y axis : 200

OUTPUT:







RESULT:
Thus the program was successfully created and executed.



3110111040XX
XXXXXXX

4


EX NO :1B BRESENHAM'S LINE DRAWING METHOD
DATE:


AIM:

To write a C Program for Line Drawing using Bresenhams Line drawing Method.

ALGORITHM:

1. Start
2. Read the two end points of a line (x1, y1) & (x2, y2).
3. Determine the Points which is used as start and end then store it in x and y
4. Plot the co-ordinate (x,y)
5. Calculate the constants x, y, 2y, 2y-2x and also obtain the starting value for the
decision parameter as p=2y-x
6. At each x
k
along the line, starting at k=0, Perform the following :
a. if p
k
<0, the next point to plot is (x
k+1,
y
k
) and p
k+1
=p
k
+2y
otherwise
b. the next point to plot is (x
k+1
,y
k+1
) and p
k+1
=p
k
+2y-2x
7. Repeat the Step 6 for x times
8. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd = DETECT, gm;
int x,y,x1,y1,x2,y2,p,dx,dy,twody,twodydx,xend;
initgraph(&gd,&gm,"..\\BGI:");
printf("\nEnter the x-coordinate of the starting point :");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the starting point :");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the Ending point :");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the ending point :");
scanf("%d",&y2);
3110111040XX
XXXXXXX

5

clrscr();
dx=x2-x1;
dy=y2-y1;
p=2*dy-dx;
twody=2*dy;
twodydx=2*(dy-dx);
if (x1>x2)
{
x=x2;
y=y2;
xend=x1;
}
else
{
x=x1;
y=y1;
xend=x2;
}
putpixel(x,y,RED);
while(x<xend)
{
x++;
if (p<0)
p=p+twody;
else
{
y=y+1;
p=p+twodydx;
}
putpixel(x,y,RED);
}
getch();
closegraph();
}
















3110111040XX
XXXXXXX

6

INPUT:

Enter the x-coordinate of the starting point : 100
Enter the y-coordinate of the starting point : 100
Enter the x-coordinate of the Ending point : 200
Enter the y-coordinate of the ending point : 200



OUTPUT:








RESULT:
Thus the program was successfully created and executed.






3110111040XX
XXXXXXX

7

EX NO :2A MIDPOINT ELLIPSE DRAWING ALGORITHM
DATE:

AIM:

To write a C Program for Ellipse Drawing using Midpoint Ellipse drawing Method.

ALGORITHM:

Step 1 : Start
Step 2 : Input rx ,ry, and ellipse center (xc, yc,) and obtain the first point on an ellipse
centered on the origin as (x,y)=(0,ry)
Step 3: Calculate the initial value of the decision parameter in region 1
p=ry
2
-(rx
2
*ry)+(0.25*rx
2
);
Step 4: At each position x
k
position in region1 perform the following
a) if p
k
<0 then p
k+1
= p
k
+2ry
2
x
k+1+
ry
2
else
b) p
k+1
= p
k
+2ry
2
x
k+1
2rx
2
y
k+1+
ry
2
c) continue until 2ry
2
x >=2rx
2
y.
Step 5: Calculate the initial value of the decision parameter in region 2
p= ry
2
*(x+0.5)*(x+0.5)+rx
2
*(y-1)*(y-1)-rx
2
*ry
2
Step 6: At each position y
k
position in region2 perform the following
a) if p
k
<0 then p
k+1
= p
k
-2rx
2
y
k+1
+rx
2
else
b) p
k+1
= p
k
+2ry
2
x
k+1
2rx
2
y
k+1+
ry
2
c) continue until 2rx
2y
>=2ry
2
x.
Step 7: Move each calculated pixel position onto the elliptical path and plot the co-ordinates.
Step 8: Stop
PROGRAM:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define round(a) ((int)(a+0.5))
void ellipseplot(int,int,int,int);
int xcenter,yc,rx,ry;
void main()
{
3110111040XX
XXXXXXX

8

int gd=DETECT,gm;
int xcenter,ycenter,rx,ry;
long rx2,ry2,tworx2,twory2,p,x,y,px,py;
initgraph(&gd,&gm,"..\\BGI:");
printf("\nEnter the x-coordinate of the centre point :");
scanf("%d",&xcenter);
printf("\nEnter the y-coordinate of the centre point :");
scanf("%d",&ycenter);
printf("\nEnter the x-coordinate of the radius :");
scanf("%d",&rx);
printf("\nEnter the y-coordinate of the radius :");
scanf("%d",&ry);
rx2=rx*rx;
ry2=ry*ry;
twory2=2*ry2;
tworx2=2*rx2;
x=0;
y=ry;
py=tworx2*y;
px=0;
ellipseplot(xcenter,ycenter,x,y);
p=round(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px=px+twory2;
if(p<0)
p=p+ry2+px;
else
{
y--;
py=py-tworx2;
p=p+ry2+px-py;
}
ellipseplot(xcenter,ycenter,x,y);
}
p=round(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y--;
py=py-tworx2;
if(p>0)
p=p+rx2-py;
else
{
x++;
px=px+twory2;
p=p+rx2-py+px;
}
ellipseplot(xcenter,ycenter,x,y);
3110111040XX
XXXXXXX

9

}
getch();
closegraph();
}
void ellipseplot(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter +x,ycenter +y,20);
putpixel(xcenter -x,ycenter +y,20);
putpixel(xcenter +x,ycenter -y,20);
putpixel(xcenter -x,ycenter -y,20);
}

INPUT:

Enter the x-coordinate of the centre point : 100
Enter the y-coordinate of the centre point : 200
Enter the x-coordinate of the radius : 50
Enter the y-coordinate of the radius : 60

OUTPUT:











RESULT:
Thus the program was successfully created and executed.
3110111040XX
XXXXXXX

10

EX NO :2B MIDPOINT CIRCLE DRAWING ALGORITHM
DATE:

AIM:

To write a C Program for Circle Drawing using Midpoint Circle Method.

ALGORITHM:

1. Start
2. Get the center point (xcenter, ycenter) and radius(r) of a circle.
3. Initialize the variables
i. x=0; y=r; p=1-r;
4. If (p<0) then
i. p=p+(2*x)+1
else
i. y--;
ii. p=p+2* (x-y)+1
5. Repeat step 4 until x<y and Increment x by 1 each time.
6. Plot the pixel to display the circle.
7. Display the circle
8. Stop.

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x,y,r,p,xcenter, ycenter;
void circleplot(int,int,int,int);
void main()
{
int gd = DETECT, gm;
initgraph(&gd,&gm,"..\\BGI:");
printf("\nEnter the x-coordinate of the centre point :");
scanf("%d",&xcenter);
printf("\nEnter the y-coordinate of the centre point :");
scanf("%d",&ycenter);
printf("\nEnter the radius :");
scanf("%d",&r);
x=0;
y=r;
p=1-r;
while (x<y)
{
x++;
if (p<0)
p=p+2*x+1;

3110111040XX
XXXXXXX

11

else
{
y--;
p=p+2*(x-y)+1;
}
circleplot(xcenter,ycenter,x,y);
}
getch();
closegraph();
}
void circleplot(int xcenter, int ycenter,int x, int y)
{
putpixel(xcenter+x,ycenter+y,10);
putpixel(xcenter-x,ycenter+y,10);
putpixel(xcenter+x,ycenter-y,10);
putpixel(xcenter-x,ycenter-y,10);
putpixel(xcenter+y,ycenter+x,10);
putpixel(xcenter-y,ycenter+x,10);
putpixel(xcenter+y,ycenter-x,10);
putpixel(xcenter-y,ycenter-x,10);
}

OUTPUT:
Enter the x-coordinate of the centre point :200
Enter the y-coordinate of the centre point :200
Enter the radius :50







RESULT:
Thus the program was successfully created and executed
3110111040XX
XXXXXXX

12

EX NO :3A LINE ATTRIBUTES
DATE:

AIM:
To study and Implement line attributes

ALGORITHM:

1. Start the program for line type
2. Include the necessary package
3. Declare the line type
4. Using setline style and line function to draw the different line function
5. Terminate the function

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{
printf("******Line Styles*******\n");
printf("\n1.Solid Line");
printf("\n2.Dotted Line");
printf("\n3.Center Line");
printf("\n4.Dashed Line");
printf("\n5.Userbit Line");
printf("\n6.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(0,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
3110111040XX
XXXXXXX

13

case 2:
clrscr();
initgraph(&gd,&gm," ");
clrscr();
setlinestyle(1,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(2,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(3,1,3);
line(100,30,250,250);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setlinestyle(4,1,3);
line(100,30,250,250);
getch();
cleardevice();closegraph();
break;
case 6:
exit(0);
}
}
}


3110111040XX
XXXXXXX

14

OUTPUT:

******Line Styles*******
1.Solid Line
2.Dotted Line
3.Center Line
4.Dashed Line
5.Userbit Line
6.Exit

Enter your choice:4










RESULT:
Thus the program was successfully created and executed.
3110111040XX
XXXXXXX

15


EX NO :3B CIRCLE ATTRIBUTES
DATE:

AIM:
To study and Implement Circle attributes

ALGORITHM:

1.Initialize the graphics mode
2.For circle,set the fill pattern using setfillstyle() command.
3.Draw the circle using pieslice() command.
4.Perform the clean up procedure.

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{
printf("******Circle Attributes*******\n");
printf("\n1.Empty Fill");
printf("\n2.Soild Fill");
printf("\n3.Line Fill");
printf("\n4.LTSlash Fill");
printf("\n5.Slash Fill");
printf("\n6.BKSlash Fill");
printf("\n7.LTBKSlash Fill");
printf("\n8.Hatch Fill");
printf("\n9.Xhatch Fill");
printf("\n10.Interleave Fill");
printf("\n11.Wide dot Fill");
printf("\n12.close dot Fill");
printf("\n13.User Fill");
printf("\n14.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);


switch(ch)
{
3110111040XX
XXXXXXX

16

case 1:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 2:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
3110111040XX
XXXXXXX

17

case 6:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(BKSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 7:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTBKSLASH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 8:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(HATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 9:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(XHATCH_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 10:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(INTERLEAVE_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 11:
3110111040XX
XXXXXXX

18

clrscr();
initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 12:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 13:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
circle(100, 100, 50);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 14:
exit(0);
}
}
}

3110111040XX
XXXXXXX

19

Output
******Circle Attributes*******

1.Empty Fill
2.Soild Fill
3.Line Fill
4.LTSlash Fill
5.Slash Fill
6.BKSlash Fill
7.LTBKSlash Fill
8.Hatch Fill
9.Xhatch Fill
10.Interleave Fill
11.Wide dot Fill
12.close dot Fill
13.User Fill
14.Exit

Enter your choice : 5




RESULT:
Thus the program was successfully created and executed.




3110111040XX
XXXXXXX

20

EX NO :3C ELLIPSE ATTRIBUTES
DATE:

AIM:
To study and Implement ellipse attributes

ALGORITHM:

1.Initialize the graphics mode
2.For ellipse,set the fill pattern using setfillstyle() command.
3.Draw the ellipse using pieslice() command.
4.Perform the clean up procedure.

PROGRAM:

#include <graphics.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
int ch;
clrscr();
while(1)
{
printf("******Ellipse Attributes*******\n");
printf("\n1.Empty Fill");
printf("\n2.Soild Fill");
printf("\n3.Line Fill");
printf("\n4.LTSlash Fill");
printf("\n5.Slash Fill");
printf("\n6.BKSlash Fill");
printf("\n7.LTBKSlash Fill");
printf("\n8.Hatch Fill");
printf("\n9.Xhatch Fill");
printf("\n10.Interleave Fill");
printf("\n11.Wide dot Fill");
printf("\n12.close dot Fill");
printf("\n13.User Fill");
printf("\n14.Exit");
printf("\n\nEnter your choice:\n");
scanf("%d",&ch);


switch(ch)
{
case 1:
clrscr();
3110111040XX
XXXXXXX

21

initgraph(&gd,&gm," ");
setfillstyle(EMPTY_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 2:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SOLID_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 3:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LINE_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 4:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 5:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(SLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 6:
clrscr();
3110111040XX
XXXXXXX

22

initgraph(&gd,&gm," ");
setfillstyle(BKSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 7:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(LTBKSLASH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 8:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(HATCH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 9:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(XHATCH_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 10:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(INTERLEAVE_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 11:
clrscr();
3110111040XX
XXXXXXX

23

initgraph(&gd,&gm," ");
setfillstyle(WIDE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 12:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(CLOSE_DOT_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();
closegraph();
break;
case 13:
clrscr();
initgraph(&gd,&gm," ");
setfillstyle(USER_FILL, RED);
ellipse(100, 100,0,360,50,25);
floodfill(100, 100, WHITE);
getch();
cleardevice();closegraph();
break;
case 14:
exit(0);
}
}
}


















3110111040XX
XXXXXXX

24

OUTPUT:

******Ellipse Attributes*******
1.Empty Fill
2.Soild Fill
3.Line Fill
4.LTSlash Fill
5.Slash Fill
6.BKSlash Fill
7.LTBKSlash Fill
8.Hatch Fill
9.Xhatch Fill
10.Interleave Fill
11.Wide dot Fill
12.close dot Fill
13.User Fill
14.Exit
Enter your choice : 9






RESULT:
Thus the program was successfully created and executed .
3110111040XX
XXXXXXX

25


EX NO :4 2D TRANSFORMATIONS-TRANSLATION,SCALING,ROTATION,SHEAR
DATE : AND SHEAR
AIM:
To study and Implement 2D Transformation Translation, Rotation, Scaling, Shear
and Reflection

ALGORITHM:
Step 1: Declare the variables xa,ya,xa1,ya1 of array type.
Step 2:Declare the variables gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step 3: Initialize the graphics function.
Step 4: Input the number of points.
Step 5: Input the value of co-ordinate according to number of points.
Step 6 : Using switch statement selects the option to perform translation, rotation, scaling,
reflection and shearing.
Step 7: Translation:
a).input the translation vector
b).add the translation vectors with the coordinates
xa1 [i] =xa[i] =tx, ya1[i] =ya[i] =ty,
c).using the function line, display the object before and after translation.
Step 8: Rotation
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the value of reference point
d). calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the object before and after rotation.
Step 9: Scaling:
a).input the scaling factor and reference point
b).calculate new coordinate point using formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the object before and after scaling.
3110111040XX
XXXXXXX

26

Step 10: Shearing:
a).input the shearing value and reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display the object before and after shearing.
Step 11: Reflection:
a).display the object before reflection using the function line
b). display the object after reflection using the function line
Step 12: Stop.

PROGRAM:

a) Translation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>

void RectAngle(int x,int y,int Height,int Width);

void main()
{
int gd=DETECT,gm;
int x,y,Height,Width,a,b,Newx,Newy;
initgraph(&gd,&gm," ");
printf("Enter the First point for the Rectangle:");
scanf("%d%d",&x,&y);
printf("Enter the Height & Width for the Rectangle:");
scanf("%d%d",&Height,&Width);
printf("Enter the Translation coordinates");
scanf("%d%d",&Newx,&Newy);
cleardevice();
printf("\nBefore Translation");
RectAngle(x,y,Height,Width);
a=x+Newx;
b=y+Newy;
printf("\n\nAfter Translation");
RectAngle(a,b,Height,Width);
getch();
3110111040XX
XXXXXXX

27

}

void RectAngle(int x,int y,int Height,int Width)
{

line(x,y,x+Width,y);
line(x,y,x,y+Height);
line(x+Width,y,x+Width,y+Height);
line(x,y+Height,x+Width,y+Height);
}


b) Scaling

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
int x1,y1,x2,y2,x3,y3,mx,my;
void draw();
void scale();
void main()
{
int gd=DETECT,gm;
int c;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
draw();
scale();
}
void draw()
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void scale()
{
int x,y,a1,a2,a3,b1,b2,b3;
int mx,my;
printf("Enter the scaling factors");
scanf("%d%d",&x,&y);
mx=(x1+x2+x3)/3;
my=(y1+y2+y3)/3;
3110111040XX
XXXXXXX

28

cleardevice();
a1=mx+(x1-mx)*x;
b1=my+(y1-my)*y;
a2=mx+(x2-mx)*x;
b2=my+(y2-my)*y;
a3=mx+(x3-mx)*x;
b3=my+(y3-my)*y;
line(a1,b1,a2,b2);
line(a2,b2,a3,b3);
line(a3,b3,a1,b1);
draw();
getch();
}

c) Rotation

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3);
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3);
void main()
{
int gd=DETECT,gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd,&gm," ");
printf("Enter the 1st point for the triangle:");
scanf("%d%d",&x1,&y1);
printf("Enter the 2nd point for the triangle:");
scanf("%d%d",&x2,&y2);
printf("Enter the 3rd point for the triangle:");
scanf("%d%d",&x3,&y3);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
cleardevice();
Rotate(x1,y1,x2,y2,x3,y3);
setcolor(1);
TriAngle(x1,y1,x2,y2,x3,y3);
getch();
}
void TriAngle(int x1,int y1,int x2,int y2,int x3,int y3)
{
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
void Rotate(int x1,int y1,int x2,int y2,int x3,int y3)
{
3110111040XX
XXXXXXX

29

int x,y,a1,b1,a2,b2,a3,b3,p=x2,q=y2;
float Angle;
printf("Enter the angle for rotation:");
scanf("%f",&Angle);
cleardevice();
Angle=(Angle*3.14)/180;
a1=p+(x1-p)*cos(Angle)-(y1-q)*sin(Angle);
b1=q+(x1-p)*sin(Angle)+(y1-q)*cos(Angle);
a2=p+(x2-p)*cos(Angle)-(y2-q)*sin(Angle);
b2=q+(x2-p)*sin(Angle)+(y2-q)*cos(Angle);
a3=p+(x3-p)*cos(Angle)-(y3-q)*sin(Angle);
b3=q+(x3-p)*sin(Angle)+(y3-q)*cos(Angle);
printf("Rotate");
TriAngle(a1,b1,a2,b2,a3,b3);
}

d) Shearing

#include<stdio.h>
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
3110111040XX
XXXXXXX

30

for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void shearing(int n,float c[][3])
{
float b[10][3],sh,a[10][3];
int i=0,ch,j;
while(1)
{
clrscr();
cleardevice();
printf("\n* * * MENU * * *");
printf("\n 1) X SHEARING");
printf("\n 2) Y SHEARING");
printf("\n 3) EXIT ");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
for(i=0;i<3;i++)
b[i][i]=1;
switch(ch)
{
case 1:
printf("\n ENTER THE VALUE for SHEARING: ");
scanf("%f",&sh);
b[1][0]=sh;
break;
case 2:
printf("\n ENTER THE VALUE for SHEARING: ");
scanf("%f",&sh);
b[0][1]=sh;
break;
case 3:
exit(0);
break;
default:
printf("\nINVALID CHOICE ! ");
break;
}
disp(n,c);
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
getch();
3110111040XX
XXXXXXX

31

}
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm," ");
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the co-ordinates of the %d vertex :",i+1);
scanf("%f%f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
shearing(n,c);
closegraph();
}

e) Reflection

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
3110111040XX
XXXXXXX

32

for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void reflection(int n,float c[][3])
{
float b[10][3],a[10][3];
int i=0,ch,j;
while(1)
{
clrscr();
cleardevice();
printf("\n* * MENU * *");
printf("\n 1) ABOUT X-AXIS");
printf("\n 2) ABOUT Y-AXIS");
printf("\n 3) ABOUT ORIGIN");
printf("\n 4) ABOUT X=Y");
printf("\n 5) ABOUT -X=Y");
printf("\n 6) EXIT");
printf("\n ENTER YOUR CHOICE : ");
scanf("%d",&ch);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
b[i][j]=0;
if(i==j)
b[i][j]=1;
}
switch(ch)
{
case 1:
b[1][1]=-1;
break;
case 2:
b[0][0]=-1;
break;
case 3:
b[0][0]=-1;
b[1][1]=-1;
break;
case 4:
b[0][0]=0;
b[1][1]=0;
b[0][1]=1;
b[1][0]=1;
3110111040XX
XXXXXXX

33

break;
case 5:
b[0][0]=0;
b[1][1]=0;
b[0][1]=-1;
b[1][0]=-1;
break;
case 6:
exit(0);
break;
default:
printf("\nINVALID CHOICE ! ");
break;
}
mul(n,b,c,a);
clrscr();cleardevice();
disp(n,c);
setcolor(RED);
disp(n,a);
getch();
}
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm," ");
printf("\n Enter the number of vertices : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the co-ordinates of the %d vertex :",i+1);
scanf("%f%f",&c[i][0],&c[i][1]);
c[i][2]=1;
}
reflection(n,c);
closegraph();
}

OUTPUT:
a) Translation
Enter the first point of the rectangle : 100 100
Enter the height and width of the rectangle : 50 100
Enter the translation coordinates : 200 200
3110111040XX
XXXXXXX

34




b) Scaling
Enter the 1
st
point for the triangle : 150 150
Enter the 2
nd
point for the triangle : 200 200
Enter the 3
rd
point for the triangle : 200 150
Enter the scaling factors : 3 5



c) Rotation
Enter the 1
st
point for the triangle: 150 150
Enter the 2
nd
point for the triangle: 200 200
Enter the 3
rd
point for the triangle: 200 150
3110111040XX
XXXXXXX

35

Enter the angle for rotation: 60






d) Shearing
Enter the number of vertices : 4
Enter the coordinates of the 1 vertex: 150 150
Enter the coordinates of the 2 vertex: 100 150
Enter the coordinates of the 3 vertex: 100 100
Enter the coordinates of the 4 vertex: 150 100




3110111040XX
XXXXXXX

36

e) Reflection
Enter the number of vertices : 3
Enter the coordinates of 1 vertex : 150 150
Enter the coordinates of 2 vertex : 100 150
Enter the coordinates of 3 vertex : 150 100
**MENU**
1.ABOUT X-AXIS
2.ABOUT Y-AXIS
3.ABOUT ORIGIN
4.ABOUT X=Y
5.ABOUT -X=Y
6.EXIT
Enter your choice : 3





RESULT:
Thus the program was successfully created and executed.
3110111040XX
XXXXXXX

37

EX NO :5 COMPOSITE 2D TRANSFORMATIONS
DATE :
AIM:
To study and implement composite 2D transformation

ALGORITHM:

Step 1: Initialise the matrix to perform computations
Step 2: Get the values and store it in the matrix
Step 3: Perform rotation , scaling and transform the 2D image using matrix
Step 4: Display the input and output shape of the object
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
struct point
{
int x;
int y;
};
typedef float matrix[3][3];
matrix tm;
void Identity( matrix m)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=(i==j);
}
void multiply(matrix a, matrix b)
{
int r,c;
matrix tmp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
tmp[r][c]=a[r][0]*b[0][c]+a[r][1]*b[1][c]+a[r][2]*b[2][c];
for(r=0;r<3;r++)
for(c=0;c<3;c++)
b[r][c]=tmp[r][c];
}

void translate(int tx,int ty)
{
matrix m;
Identity(m);
m[0][2]=tx;
m[1][2]=ty;
3110111040XX
XXXXXXX

38

multiply(m,tm);
}

void scale(float sx,float sy, struct point refpt)
{
matrix m;
Identity(m);
m[0][0]=sx;
m[0][2]=(1-sx)*refpt.x;
m[1][1]=sy;
m[1][2]=(1-sy)*refpt.y;
multiply(m,tm);
}
void rotate(float a , struct point refpt)
{
matrix m;
Identity(m);
a=(a*3.14)/180.0;
m[0][0]=cos(a);
m[0][1]=-sin(a);
m[0][2]=refpt.x*(1-cos(a))+refpt.y*sin(a);
m[1][0]=sin(a);
m[1][1]=-cos(a);
m[1][2]=refpt.y*(1-cos(a))+refpt.x*sin(a);
multiply(m,tm);
}
void transform(int npts, struct point *pts)
{
int k;
float tmp;
for(k=0;k<npts;k++)
{
tmp=tm[0][0]*pts[k].x+tm[0][1]*pts[k].y+tm[0][2];
pts[k].y=tm[1][0]*pts[k].x+tm[1][1]*pts[k].y+tm[1][2];
pts[k].x=tmp;
}
}
void main()
{
struct point pts[4]={220.0, 50.0, 320.0, 200.0, 150.0, 200.0, 220.0, 50.0};
struct point refpt={50.0,50.0};
int g=DETECT,d;
initgraph(&g,&d,"c:\tc\bgi");
setbkcolor(GREEN);
setcolor(RED);
drawpoly(4,pts);
getch();
Identity(tm);
scale(0.5,0.5,refpt);
rotate(90.0,refpt);
3110111040XX
XXXXXXX

39

translate(100,100);
transform(4,pts);
setcolor(WHITE);
drawpoly(4,pts);
getch();
closegraph();
}

OUTPUT:







RESULT:
Thus the program was successfully created and executed
3110111040XX
XXXXXXX

40

EX NO :6 3D TRANSFORMATIONS-TRANSLATION,ROTATION AND SCALING
DATE :
AIM:
To perform 3D transformations such as Translation, Rotation and Scaling on 3D
object
ALGORITHM:
1. 3 dimensional transformation it has three axis x,y,z.
2. Depending upon the coordinate it will perform the Translation, Rotation, Scaling,
3. The translation can be performed by changing the sign of the translation
components Tx, Ty, and Tz.
4. Perform the scaling of 3D object with scaling parameter
5. Increase of rotation, object can be rotated about x or y or z axis.
6. Display the transmitted object in the screen
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int maxx,maxy,midx,midy;
void axis()
{
getch();
cleardevice();
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
}
void main()
{
int gd,gm,x,y,z,ang,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:/TC/BGI");
setfillstyle(3,25);
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
3110111040XX
XXXXXXX

41

outtextxy(100,100,"ORIGINAL OBJECT");
line(midx,0,midx,maxy);
line(0,midy,maxx,midy);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
axis();
outtextxy(100,20,"TRANSLATION");
printf("\n\n Enter the Translation vector: ");
scanf("%d%d",&x,&y);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x+100),midy-(y+20),midx+(x+60),midy-(y+90),20,5);
axis();
outtextxy(100,20,"SCALING");
printf("\n Enter the Scaling Factor: ");
scanf("%d%d%d",&x,&y,&z);
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+(x*100),midy-(y*20),midx+(x*60),midy-(y*90),20*z,5);
axis();
outtextxy(100,20,"ROTATION");
printf("\n Enter the Rotation angle: ");
scanf("%d",&ang);
x1=100*cos(ang*3.14/180)-20*sin(ang*3.14/180);
y1=100*sin(ang*3.14/180)+20*sin(ang*3.14/180);
x2=60*cos(ang*3.14/180)-90*sin(ang*3.14/180);
y2=60*sin(ang*3.14/180)+90*sin(ang*3.14/180);
axis();
printf("\n After rotating about z-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,20,5);
axis();
printf("\n After rotating about x-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+100,midy-x1,midx+60,midy-x2,20,5);
axis();
printf("\n After rotating about y-axis\n");
bar3d(midx+100,midy-20,midx+60,midy-90,20,5);
bar3d(midx+x1,midy-20,midx+x2,midy-90,20,5);
axis();
closegraph();
}


3110111040XX
XXXXXXX

42

INPUT:
Enter the translation vector : 65 70
Enter the scaling factor : 2 2 2
Enter the Rotation Angle : 100
Output

3110111040XX
XXXXXXX

43



3110111040XX
XXXXXXX

44




RESULT:
Thus the program was successfully created and executed

3110111040XX
XXXXXXX

45

EX NO :7 COMPOSITE 3D TRANSFORMATIONS
DATE :
AIM:
To write a C program to perform composite (sequence) of transformations on a 3D
object.
DESCRPTION:
1) Reflection:
Reflection is a transformation that produces a mirror image of an object.
Transformation matrix for reflection about the line y=0, is

Transformation matrix for reflection about the line x=0, is

2) Shearing
Shearing is a transformation that distorts the shape of an object.
An X-direction shear relative to the x axis is produced by the transformation matrix



A y-direction shear relative to other referencelines is produced by the transformation
matrix
3110111040XX
XXXXXXX

46



3) Rotation
This is done by three transformation steps: translation of the arbitrary point (xc,yc) to
the origin, rotate about the origin, and then translate the center of rotation back to where it
belongs.
To tranform a point, we would multiply all the transformation matrices together to
form an overall transformation matrix.
1. The translation which moves (xc,yc) to the origin:

2. the rotation is
=
3. and the translation to move the center point back is


ALGORITHM:
1. Start the program.
2. Declare the variables.
3. Declare the following functions.
transform()
rotate()
scale()
4. Translate the object one co-ordinate to another using transform() function and rotate the
object using rotate function.
5. Translate the object one co-ordinate to another using transform() function and change the
size of object using scale() function.
6. Change the size of object using scale() function and translate the object one co-ordinate to
another using transform() function
7. Display the object.
8. Stop the program.

3110111040XX
XXXXXXX

47

PROGARM:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
float tx,ty,tz,sx,sy,sz,r;
float x[15][15],y[15][15],z[15][15];
int i,j=2,ch;
void transform()
{
x[i][j-1]=x[i][j-1]+tx;
x[i][j]=x[i][j]+tx;
y[i][j-1]=y[i][j-1]+ty;
y[i][j]=y[i][j]+ty;
z[i][j-1]=z[i][j-1]+tz;
z[i][j]=z[i][j]+tz;
}
void rotate()
{
if(i<=8)
{
x[i][j-1]=(x[i][j-1]*cos(r))-(y[i][j-1]*sin(r));
y[i][j-1]=(x[i][j-1]*sin(r))+(y[i][j-1]*cos(r));
x[i][j]=(x[i][j]*cos(r))-(y[i][j]*sin(r));
y[i][j]=(x[i][j]*sin(r))+(y[i][j]*cos(r));
}
else
{
z[i][j-1]=(z[i][j-1]*cos(r))-(y[i][j-1]*sin(r));
y[i][j-1]=(z[i][j-1]*sin(r))+(y[i][j-1]*cos(r));
z[i][j]=(z[i][j]*cos(r))-(y[i][j]*sin(r));
y[i][j]=(z[i][j]*sin(r))+(y[i][j]*cos(r));
}
}
void scale()
{
x[i][j-1]=x[i][j-1]*sx;
x[i][j]=x[i][j]*sx;
y[i][j-1]=y[i][j-1]*sy;
y[i][j]=y[i][j]*sy;
z[i][j-1]=z[i][j-1]*sz;
z[i][j]=z[i][j]*sz;
}
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
x[1][1]=200,x[1][2]=250,y[1][1]=200,y[1][2]=200;
x[2][1]=200,x[2][2]=200,y[2][1]=200,y[2][2]=150;
3110111040XX
XXXXXXX

48

x[3][1]=200,x[3][2]=250,y[3][1]=150,y[3][2]=150;
x[4][1]=250,x[4][2]=250,y[4][1]=150,y[4][2]=200;
x[5][1]=220,x[5][2]=270,y[5][1]=140,y[5][2]=140;
x[6][1]=270,x[6][2]=270,y[6][1]=140,y[6][2]=190;
x[7][1]=220,x[7][2]=220,y[7][1]=140,y[7][2]=190;
x[8][1]=220,x[8][2]=270,y[8][1]=190,y[8][2]=190;
z[9][1]=200,z[9][2]=220,y[9][1]=150,y[9][2]=140;
z[10][1]=250,z[10][2]=270,y[10][1]=150,y[10][2]=140;
z[11][1]=250,z[11][2]=270,y[11][1]=200,y[11][2]=190;
z[12][1]=220,z[12][2]=200,y[12][1]=190,y[12][2]=200;
textattr(0);
for(i=1;i<=12;i++)
{
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
}
while(ch!=4)
{
printf("\n 3D-COMPOSITE TRANSFORMATION");
printf("\n1.TRANSLATION & ROTATION");
printf("\n2.TRANSLATION & SCALING");
printf("\n3.SCALING & ROTATION");
printf("\n4.EXIT");
printf("\nENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nENTER X Y & Z VALUES:");
scanf("%f%f%f",&tx,&ty,&tz);
printf("\nENTER ROTATION ANGLE:");
scanf("%f",&r);
clrscr();
r=(r*3.14/180);
for(i=1;i<=12;i++)
{
transform();
rotate();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
}
break;
case 2:
printf("\nENTER X Y & Z VALUES:");
scanf("%f%f%f",&tx,&ty,&tz);
3110111040XX
XXXXXXX

49

printf("\nENTER X Y & Z VECTORS:");
scanf("%f%f%f",&sx,&sy,&sz);
clrscr();
for(i=1;i<=12;i++)
{
transform();
scale();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
}
break;
case 3:
printf("\nENTER X Y & Z VECTORS:");
scanf("%f%f%f",&sx,&sy,&sz);
printf("\nENTER ROTATION ANGLE:");
scanf("%f",&r);
clrscr();
r=(r*3.14/180);
for(i=1;i<=12;i++)
{
scale();
rotate();
if(i<=8)
line(x[i][j-1],y[i][j-1],x[i][j],y[i][j]);
else
line(z[i][j-1],y[i][j-1],z[i][j],y[i][j]);
delay(500);
}
break;
case 4:
printf("\nEND OF PROGRAM");
exit(0);
break;
}
}
getch();
return 0;
}









3110111040XX
XXXXXXX

50

OUTPUT:
/* 3D-COMPOSITE TRANSFORMATION */
1. TRANSLATION & ROTATION
2.TRANSLATION & SCALING
3.SCALING & ROTATION
4.EXIT
ENTER YOUR CHOICE:
ENTER YOUR CHOICE:1
ENTER YOUR X,Y &Z VECTORS: 1 1 1
ENTER ROTATION ANGLE : 20
1. TRANSLATION & ROTATION
2.TRANSLATION & SCALING
3.SCALING & ROTATION
4.EXIT
ENTER YOUR CHOICE:2
ENTER YOUR X ,Y & Z VALUES: 2 2 2
ENTER YOUR X ,Y & Z VECTORS : 3 2
1. TRANSLATION & ROTATION
2.TRANSLATION & SCALING
3.SCALING & ROTATION
4.EXIT
ENTER YOUR CHOICE:3
ENTER YOUR X,Y & Z VECTORS :1.3 1.4 1.3
ENTER ROTATION ANGLE : 25
1. TRANSLATION & ROTATION
2.TRANSLATION & SCALING
3.SCALING & ROTATION
4.EXIT
ENTER YOUR CHOICE:4
END OF THE PROGRAM
















RESULT:
Thus the program has been executed and verified successfully.

3110111040XX
XXXXXXX

51

EX NO :8 COHEN SUTHERLAND 2D LINE CLIPPING AND WINDOWING
DATE :
AIM:

To study and Implement Cohen Sutherland 2D line clipping and Windowing

ALGORITHM:

1. Start
2.
Input two endpoints of the line say p
1
( x
1
, y
1
) and p
2
( x
2
, y
2
)

3. Input two corners (Let-top and right -bottom ) of the window , say(wx
1
,wy
1
and wx
2

, wy
2
)
4. Assign the region codes for two endpoints p
1
and p
2
using following steps :
Initialize code with bits 0000
Set Bit1 =0 if (x < wx
1
)
Set Bit2 =0 if (x < wx
2
)
Set Bit3 =0 if (y < wy
2
)
Set Bit4 =0 if (y < wy
1
)
5. Check for visibility of line
a. If region codes for both endpoints p
1
and p
2
are zero then the line is completely
visible. Hence draw the line and go to step 9
b. If region codes for both endpoints are not zero and the logical ANDing of them is also
nonzero then the line is completely invisible. So reject the ling and go to 9
c. If region codes for two endpoints do not satisfy the condition in (4a and 4b the line is
partially visible.
6. Determine the intersecting edge of the clipping window by inspecting the region codes
of two endpoints
a. If region codes for both endpoints are non- zero, find intersecting point p'
1
and p'
2
with boundary edges of clipping window with respect to point p
1
and point p
2
respectively
b. If region codes for any one endpoints are non-zero, find intersecting point p'
1
or p'
2
with boundary edges of clipping window with respect to it.
7. Divide the Line segments considering intersection points
3110111040XX
XXXXXXX

52

8. Reject the line segments if any one endpoint of it appears outsides the clipping
window
9. Draw the remaining line segments
10.Stop
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define LEFT_EDGE 0x1
#define RIGHT_EDGE 0x2
#define BOTTOM_EDGE 0x4
#define TOP_EDGE 0x8
#define INSIDE(a) (!a)
#define REJECT(a,b) (a&b)
#define ACCEPT(a,b) (!(a|b))
#define FALSE 0
#define TRUE 1
struct point
{
int x;
int y;
}p1,p2,tmp;
struct win
{
int x;
int y;
}winmin,winmax;
unsigned char encode(struct point pt,struct win winmin,struct win winmax)
{
unsigned char code=0x00;
if(pt.x<winmin.x)
code=code|LEFT_EDGE;
if(pt.x>winmax.x)
code=code|RIGHT_EDGE;
if(pt.y<winmin.y)
code=code|BOTTOM_EDGE;
if(pt.y>winmax.y)
code=code|TOP_EDGE;
return(code);
}
void swappts(struct point *p1,struct point *p2)
{
tmp=*p1;
*p1=*p2;
*p2=tmp;
}
void swapcodes(unsigned char *c1,unsigned char *c2)
3110111040XX
XXXXXXX

53

{
unsigned char tmp;
tmp=*c1;
*c1=*c2;
*c2=tmp;
}
void clipline(struct win winmin, struct win winmax,struct point p1,struct point p2)
{
unsigned char code1,code2;
int done=FALSE,draw=FALSE;
float m;
while(!done)
{
code1=encode(p1,winmin,winmax);
code2=encode(p2,winmin,winmax);
if(ACCEPT(code1,code2))
{
done=TRUE;
draw=TRUE;
}
else if(REJECT(code1,code2))
done=TRUE;
else
{
if(INSIDE(code1))
{
swappts(&p1,&p2);
swapcodes(&code1,&code2);
}
if(p2.x!=p1.x)
m=(p2.y-p1.y)/(p2.x-p1.x);
if(code1 &LEFT_EDGE)
{
p1.y+=(winmin.x-p1.x)*m;
p1.x=winmin.x;
}
else if(code1 &RIGHT_EDGE)
{
p1.y+=(winmax.x-p1.x)*m;
p1.x=winmax.x;
}
else if(code1 &BOTTOM_EDGE)
{
if(p2.x!=p1.x)
p1.x+=(winmin.y-p1.y)/m;
p1.y=winmin.y;
}
else if(code1 &TOP_EDGE)
{
if(p2.x!=p1.x)
3110111040XX
XXXXXXX

54

p1.x+=(winmax.y-p1.y)/m;
p1.y=winmax.y;
}
}
}
if(draw)
line(p1.x,p1.y,p2.x,p2.y);
}
void main()
{
int c,gm,gr;
clrscr();
printf("\nEnter the window minimum coordinates");
scanf("%d%d",&winmin.x,&winmin.y);
printf("\nEnter the window max coordinates");
scanf("%d%d",&winmax.x,&winmax.y);
printf("\nEnter the starting point");
scanf("%d%d",&p1.x,&p1.y);
printf("\nenter the end point");
scanf("%d%d",&p2.x,&p2.y);
detectgraph(&gm,&gr);
initgraph(&gm,&gr,"d:\\tc\\BGI");
printf("Before Clipping");
line(p1.x,p1.y,p2.x,p2.y);
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
getch();
clrscr();
cleardevice();
printf("After Clipping");
rectangle(winmin.x,winmax.y,winmax.x,winmin.y);
clipline(winmin,winmax,p1,p2);
getch();
}

OUTPUT:
Enter the window minimum coordinates: 100 100
Enter the window maximum coordinates: 200 200

Enter the starting point: 150 150
Enter the ending point: 200 200

3110111040XX
XXXXXXX

55







RESULT:
Thus the program was successfully created and executed
3110111040XX
XXXXXXX

56

EX NO :9 SUTHERLAND-HODGEMAN POLYGON CLIPPING ALGORITHM
DATE :
AIM:
To implement polygon clipping.
ALGORITHM:
1. Get the minimum and maximum coordinates of both window and view port.
2. Get the number of sides of a polygon and its corresponding coordinates.
3. If the polygon lies within region code window, display it.
4. If any one of the polygon side is neither inside nor outside the boundary, find point of
intersection and clip the regions that lies outside the boundary.
5. Display the polygon after clipping
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void clip(float,float,float);
int i,j=0,n;
int rx1,rx2,ry1,ry2;
float x1[8],y1[8];
void main()
{
int gd=DETECT,gm;
int i,n;
float x[8],y[8],m;
clrscr();
initgraph(&gd,&gm,"");
printf("coordinates for rectangle : ");
scanf("%d%d%d%d",&rx1,&ry1,&rx2,&ry2);
printf("no. of sides for polygon : ");
scanf("%d",&n);
printf("coordinates : ");

for(i=0;i<n;i++)
{
scanf("%f%f",&x[i],&y[i]);
}
3110111040XX
XXXXXXX

57

cleardevice();
outtextxy(10,10,"Before clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<n-1;i++)
line(x[i],y[i],x[i+1],y[i+1]);
line(x[i],y[i],x[0],y[0]);
getch();
cleardevice();
for(i=0;i<n-1;i++)
{
m=(y[i+1]-y[i])/(x[i+1]-x[i]);
clip(x[i],y[i],m);
clip(x[i+1],y[i+1],m);
}
m=(y[i]-y[0])/(x[i]-x[0]);
clip(x[i],y[i],m);
clip(x[0],y[0],m);
outtextxy(10,10,"After clipping");
outtextxy(10,470,"Press any key....");
rectangle(rx1,ry1,rx2,ry2);
for(i=0;i<j-1;i++)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
getch();
}
void clip(float e,float f,float m)
{
while(e<rx1 || e>rx2 || f<ry1 || f>ry2)
{
if(e<rx1)
{
f+=m*(rx1-e);
e=rx1;
}

else if(e>rx2)
{
f+=m*(rx2-e);
e=rx2;
}
if(f<ry1)
3110111040XX
XXXXXXX

58

{
e+=(ry1-f)/m;
f=ry1;
}
else if(f>ry2)
{
e+=(ry2-f)/m;
f=ry2;
}
}
x1[j]=e;
y1[j]=f;
j++;
}
OUTPUT:











RESULT:
Thus the program was successfully created and executed

3110111040XX
XXXXXXX

59

EX NO :10 DRAWING 3D OBJECTS AND SCENES USING OPEN GL
DATE :

AIM:
To draw three dimensional objects and scenes using OPENGL

PROCEDURE:

Steps to add opengl files in Microsoft visual studio 2010:

1. Open Microsoft Visual Studio 2010.
2. File -> New -> Project -> Other Languages -> Visual C++ -> Empty Project.
3. Enter project name and click OK.
4. Right click on the project in the Solution Explorer (in RHS) and select Add -> new item.
5. Select C++ File(.cpp), Enter file name and click Add.
6. To hide other folders click on Show All Files icon at the top of Solution Explorer (Second
icon from left).
7. Right Click the project in the Solution Explorer and select Open Folder in Windows
Explorer.
8. Copy the Folder freeglut\include\gl (gl in small letters) to the folder opened in step 7.
9. Copy the File freeglut\lib\freeglut.lib to the folder opened in Step 7.
10.Copy the File freeglut\bin\freeglut.dll to the folder opened in Step 7.
11. Refresh the Project in Solution Explorer.
12. Right Click all the newly added file and select Include in Project.
13. Write the code in the .cpp file created in step 5.

ALGORITHM:
Main:

1.Initialise glut and create window.
2.Register call backs.
3.Create a render function to render the desired shape and rotate it.
4.Call glutMainLoop().
RenderScene:
1.Clear color and depth buffers.
2.Reset transformations.
3.Set the camera position.
4.Rotate the object using glRotatef();
5.Switch between the 3D objects according to the choice.
6.End using glEnd().



3110111040XX
XXXXXXX

60

PROGRAM:

#include "conio.h"
#include "stdio.h"
#include "gl\glut.h"
#include "gl\freeglut.h"
using namespace std;

float angle = 0.0f;
int choice;

void changeSize(int w, int h) {

// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;

// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);

// Reset Matrix
glLoadIdentity();

// Set the viewport to be the entire window
glViewport(0, 0, w, h);

// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f);

// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void)
{
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
glLoadIdentity();

// Set the camera
3110111040XX
XXXXXXX

61

gluLookAt(0.0f, 0.0f, 10.0f,
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f);

glRotatef(angle, 1.0f, 0.0f, 0.0f);

switch(choice)
{
case 1: glutWireTeapot(1);
break;
case 2: glutWireTorus(1,3,20,30);
break;
case 3: glutWireCone(3,5,30,30);
break;
case 4: glutWireSphere(3,30,30);
break;
case 5: glutWireCube(5);
break;
case 6: exit(0);
}

glEnd();
angle+=0.1f;
glutSwapBuffers();
}

int main(int argc, char **argv)
{

printf("3D OBJECTS AND SCENES USING OPENGL\n\n");
printf("MENU\n");
printf("1. TEA POT\n2. TORUS\n3. CONE\n4. SPHERE\n5. CUBE\n6. EXIT");
printf("\nEnter Choice: ");
scanf("%d", &choice);


// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow(Objects Drawn here");

3110111040XX
XXXXXXX

62

// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);

// enter GLUT event processing cycle
glutMainLoop();

return 1;
}

OUTPUT:















RESULT:
Thus the program was successfully created and executed

You might also like