CG Lab Manual

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 53

Ex.

No : 1
Date

BRESENHAMS LINE DRAWING

AIM:
To implement Bresenhams line drawing Algorithm for drawing lines.
ALGORITHM
Step1: Start the program.
Step2: Input the starting and end points and find the dx and dy values.
Step3: Set the p value as p=2*(dy-dx).
Step4: Check xa>xb then store (xb,yb)in (x,y) otherwise store (xa,xb)in (x,y).
Step5: Put a pixel and check p value to set the value by the formula p=p+2*dy-dx.
Step6: Repeat the step 5 until x<xend.
Step7: Display the line.
Step8: Stop the program.

PROGRAM:
#include<stdio.h>
#include<graphics.h>
void main()
{
intdx,dy,x,y,xend,p,gd=DETECT,gm,xa,xb,ya,yb;
printf("Line drawing using Bresenham's Algorithm:\n");
printf("Enter the starting point and ending point:");
scanf("%d%d%d%d",&xa,&ya,&xb,&yb);
initgraph(&gd,&gm,"c:/tc/bgi");
dx=abs(xa-xb);
dy=abs(ya-yb);
p=2*(dy-dx);
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
1

}
putpixel(x,y,12);
do
{
if(p<0)
p+=2*dy;
else
{
y++;
x++;
p+=2*dy;
}
putpixel(x,y,12);
}
while(x<xend);
getch();
closegraph();
}
OUTPUT:
Enter the two left endpoints (Xa, Ya): 234

124

Enter the two right endpoints (Xa, Ya): 578 321

RESULT:
Thus the program is executed and verified.

Ex.No: 1(b)
Date :

CIRCLE DRAWING

AIM:
To write a program to draw a circle using Bresenhams circle drawing
Algorithm.
ALGORITHM:
Step 1: Start
Step 2: Get the center point as (xc,yc),get the radius as r.
Step 3: Assign y=r,x=0
Step 4: Calculate p=3-2r
Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and y=y-1
Step 6: Increment x by 1
Step 7: Do steps (9) to (16)
Step 8: Repeat steps (5) to (9) until x<=y
Step 9: Put a pixel on (xc+x,yc+y,15);
Step 10: Put a pixel on (xc+x,yc-y,15);
Step 11: Put a pixel on (xc-x,yc+y,15);
Step 12: Put a pixel on (xc-x, yc-y, 15);
Step 13: Put a pixel on (xc+y,yc+x,15);
Step14: Put a pixel on (xc+y, yc-x, 15);
Step 15: Put a pixel on (xc-y, yc+x, 15);
Step 16: Put a pixel on (xc-y, yc-x, 15);
Step 17: Stop

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<math.h>
void main()
{
float d;
intgd,gm,x,y;
int r;
clrscr();
printf("Enter the radius of a circle:");
scanf("%d",&r);
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:/tc/bgi");
x=0;
y=r;
d=3-2*r;
do
{
putpixel(200+x,200+y,15);
putpixel(200+y,200+x,15);
putpixel(200+y,200-x,15);
putpixel(200+x,200-y,15);
putpixel(200-x,200-y,15);
putpixel(200-y,200-x,15);
putpixel(200-y,200+x,15);
putpixel(200-x,200+y,15);
if(d<=0)
{
d=d+4*x+6;
}
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
delay(1000);
}
while(x<y);
getch();
closegraph();
}

OUTPUT:
Enter the radius value

: 80

Enter the xcenter and ycenter values: 230

260

RESULT:
Thus the program is executed and verified

Ex.No :1(c)
Date

ELLIPSE DRAWING

AIM:
To write a program to draw a ellipse using Bresenhams ellipse drawing
Algorithm.
ALGORITHM:
Step 1: Start
Step 2: Get the center point as(x1, y1)
Step 3: Get the length of semi-major, semi-minor axes as r1 & r2
Step 4: Calculate t=pi/180
Step 5: Initialise i=0;
Step 6: Compute d=i*t
Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).
Step 8: Put a pixel on(x,y)
Step 9: Increment I by 1
Step 10: Repeat steps(6) to (9) until i<360
Step 11: Stop

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
int xc=0,yc=0,rx,ry,p,px,py,x,y;
int ry2,rx2,try2,trx2;
plotpoints(intxc,intyc,intx,int y)
{
putpixel(xc+x,yc+y,3);
putpixel(xc-x,yc+y,3);
putpixel(xc+x,yc-y,3);
putpixel(xc-x,yc-y,3);
return 1;
}
void main()
{
intgd=DETECT,gm;
6

int xc=200,yc=200,rx,ry;
initgraph(&gd,&gm,"c:/tc/bgi");
printf("\nEnter the center point and x & y radius:");
scanf("%d%d",&rx,&ry);
ry2=ry*ry;
rx2=rx*rx;
try2=2*ry2;
trx2=2*rx2;
x=0;
y=ry;
plotpoints(xc,yc,x,y);
p=(int)(ry2-rx2*ry+(0.25*rx2));
px=0;
py=trx2*y;
while(px<py)
{
x=x+1;
px=px+try2;
if(p>=0)
{
y=y-1;
py=py-trx2;
}
if(p<0)
p=p+ry2+px;
else
p=p+ry2+px-py;
plotpoints(xc,yc,x,y);
}
p=(int)(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y=y-1;
py=py-trx2;
if(p<=0)
{
x=x+1;
px=px+try2;
}
if(p>0)
p=p+rx2-py;
else
p=p+rx2-py+px;
plotpoints(xc,yc,x,y);
}
getch();
closegraph();
}

OUTPUT:
Enter the radius value(Rx.Ry)

10

30

Enter the xcenter and ycenter values

300

150

RESULT:
Thus the program is executed and verified.

Ex.No :2
Date :

LINE, CIRCLE AND ELLIPSE ATTRIBUTES

AIM:
To write a C Program to display the line, circle and ellipse attributes.
ALGORITHM:
Step 1: Start the program .
Step 2: Initialize the variables.
Step 3: Call the initgraph() function
Step 4: Set color for the output primitives.
Step 5: Using Outtextxy() display the choosen particular primitives.
Step 6: Using switch case mention the various primitives and their attributes.
Step 7: The various primitives are line ,circle and ellipse.
Step 8: Close the graph and run the program.
Step 9: Stop the program.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
charch='y';
intgd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm," c:/tc/bgi ");
while(ch=='y')
{
cleardevice();
setbkcolor(9);
outtextxy(100,150,"Enter 1 to get line");
outtextxy(100,170,"2.Circle");
outtextxy(100,230,"3.Ellipse");
outtextxy(100,270,"4.Exit");
ch=getch();
cleardevice();
switch(ch)
9

{
case '1':
line(100,200,300,400);
break;
case '2':
circle(200,200,100);
break;
case '3':
setfillstyle(5,4);
fillellipse(100,100,50,100);
break;
case '4':
closegraph();
return;
}
ch='y';
getch();
}
}

OUTPUT:
Enter 1 to get line: 1
2 for circle
3 for ellipse
4 for exit

RESULT:
Thus the program is executed successful

10

Ex.No :3
Date

TWO DIMENSIONAL TRANSFORMATION

AIM:
To perform the 2D transformation
ALGORITHM:
Step1: Enter the choice of the program
Step2: Perform the translation,rotation , scaling, reflection and shearing.
Step3:Get the needed parameters for the transformation from the user
Step4:Incase of rotation, object can be rotated about X or Y axis.
Step5:display the transmitted object in screen

PROGRAM:
# include<graphics.h>
# include<stdio.h>
# include<conio.h>
# include<math.h>
void main()
{
intgd=DETECT,gm,i,j,k,ch;
float tx,ty,x,y,ang,n,temp;
float a[5][3],si,co,b[5][3],c[5][3];
initgraph(&gd,&gm," c:/tc/bgi ");
n=4;
a[0][0]=0;
a[0][1]=0;
a[1][0]=100;
a[1][1]=0;
a[2][0]=100;
a[2][1]=100;
a[3][0]=0;
a[3][1]=100;
a[4][0]=0;
a[4][1]=0;
while(1)
{
cleardevice();
gotoxy(1,8);
printf("\n\t******** Program to perform 2-D Transformations ********");
printf("\n\t\t\t 1. Accept the polygon");
printf("\n\t\t\t 2. Perform translation");
printf("\n\t\t\t 3. Perform scaling");
printf("\n\t\t\t 4. Perform rotation");
printf("\n\t\t\t 5. Perform reflection");
printf("\n\t\t\t 6. Perform shearing");
printf("\n\t\t\t 7. Exit");
printf("\n\t\t\t Enter your choice::");
11

scanf("%d",&ch);
switch(ch)
{
case 1:
cleardevice();
gotoxy(1,1);
printf("\n\tEnter no of points.:");
scanf("%f",&n);
for(i=0;i<n;i++)
{
printf("\n\t Enter x,y co-ordinates for %d::",i+1);
scanf("%f %f",&a[i][0],&a[i][1]);
}
a[i][0]=a[0][0];
a[i][1]=a[0][1];
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 2:
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
gotoxy(1,1);
printf("Enter translation vectors tx and ty\n\t");
scanf("%f %f",&x,&y);
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0]+x,240-(a[i][1]+y),320+a[i+1][0]+x,240-(a[i+1][1]+y));
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 3:
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
gotoxy(1,1);
printf("Enter scaling vectors tx and ty\n\t");
scanf("%f %f",&x,&y);
if(x==0)
x=1;
if(y==0)
12

y=1;
cleardevice();
for(i=0;i<n;i++)
line(320+(a[i][0]*x),240-(a[i][1]*y),320+(a[i+1][0]*x),240-(a[i+1][1]*y));
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 4:
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
gotoxy(1,1);
printf("Enter the angle of rotation\n\t");
scanf("%f",&ang);
ang=ang*0.01745;
gotoxy(1,3);
printf("Enter point of rotation\n\t");
scanf("%f %f",&x,&y);
gotoxy(1,5);
printf("1.clockwise 2.anticlockwise\n\t");
scanf("%d",&k);
si=sin(ang);
co=cos(ang);
for(i=0;i<n+1;i++)
{
c[i][0]=a[i][0];
c[i][1]=a[i][1];
c[i][2]=1;
}
b[0][0]=co;
b[0][1]=si;
b[0][2]=0;
b[1][0]=(-si);
b[1][1]=co;
b[1][2]=0;
b[2][0]=(-x*co)+(y*si)+x;
b[2][1]=(-x*si)-(y*co)+y;
b[2][2]=1;
if(k==1)
{
b[0][1]=(-si);
b[1][0]=(si);
b[2][0]=(-x*co)-(y*si)+x;
b[2][1]=(-x*si)+(y*co)+y;
}
for(i=0;i<n+1;i++)
{
13

for(j=0;j<3;j++)
{
a[i][j] = 0 ;
for(k=0;k<3;k++)
a[i][j] = a[i][j] + c[i][k] * b[k][j] ;
}
}
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 5:
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
gotoxy(1,1);
printf("\n1.Reflection about Y-axis");
printf("\n2.Reflection about X-axis");
printf("\n3.Reflection about origin");
printf("\n4.Reflection about line y=x");
printf("\n5.Reflection about line y=-x");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
for(i=0;i<n+1;i++)
a[i][0]=a[i][0]*(-1); /* Plot the polygon */
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 2:
for(i=0;i<n+1;i++)
a[i][1]=a[i][1]*(-1);
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
14

case 3:
for(i=0;i<n+1;i++)
{
a[i][1]=a[i][1]*(-1);
a[i][0]=a[i][0]*(-1);
}
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 4:
for(i=0;i<n+1;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
line(0,479,639,0);
getch();
break;
case 5:
for(i=0;i<n+1;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
for(i=0;i<n+1;i++)
{
a[i][1]=a[i][1]*(-1);
a[i][0]=a[i][0]*(-1);
}
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
line(0,0,639,479);
getch();
break;
default:
break;
15

}
break;
case 6:
cleardevice();
for(i=0;i<n;i++)
line(320+a[i][0],240-a[i][1],320+a[i+1][0],240-a[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
gotoxy(1,1);
printf("\n1.X shear with y reference line");
printf("\n2.Y shear with x reference line");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{ case 1: printf("\nEnter the x-shear parameter value:");
scanf("%f",&temp);
printf("\nEnter the yref line");
scanf("%f",&ty);
b[0][0]=1;
b[0][1]=0;
b[0][2]=0;
b[1][0]=temp;
b[1][1]=1;
b[1][2]=0;
b[2][0]=(-temp)*(ty);
b[2][1]=0;
b[2][2]=1;
for(i=0;i<n+1;i++)
a[i][2]=1;
for(i=0;i<n+1;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = 0 ;
for(k=0;k<3;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j] ;
}}
cleardevice();
for(i=0;i<n;i++)
line(320+c[i][0],240-c[i][1],320+c[i+1][0],240-c[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
case 2:
printf("\nEnter the y-shear parameter value:");
scanf("%f",&temp);
printf("\nEnter the xref line");
scanf("%f",&tx);
b[0][0]=1;
16

b[0][1]=temp;
b[0][2]=0;
b[1][0]=0;
b[1][1]=1;
b[1][2]=0;
b[2][0]=0;
b[2][1]=(-temp)*(tx);
b[2][2]=0;
for(i=0;i<n+1;i++)
a[i][2]=1;
for(i=0;i<n+1;i++)
{
for(j=0;j<3;j++)
{
c[i][j] = 0 ;
for(k=0;k<3;k++)
c[i][j] = c[i][j] + a[i][k] * b[k][j] ;
}}
cleardevice();
for(i=0;i<n;i++)
line(320+c[i][0],240-c[i][1],320+c[i+1][0],240-c[i+1][1]);
line(0,240,639,240);
line(320,0,320,479);
getch();
break;
default:
break;
}
break;
case 7:
exit(1);
closegraph();
restorecrtmode();
break;
default:
break; }}}

OUTPUT:
MENU
Translation
Rotation
Scaling
Shearing
Reflection
Exit

17

Translation
Enter the choice: 1 Enter the number of vertices: 3
Enter the coordinates: 30

150

10

200

Enter the coordinates: 10 200

60

200

Enter the coordinates: 60

30

150

200

RESULT:
Thus the program is executed and verified.

18

EXP.NO:4
DATE:

COMPOSITE 2D TRANSFORMATIONS

AIM:
To write a C program to implement 2D transformations.

ALGORITHM:
Step 1: Enter the choice for transformation.
Step 2: Perform the translation, rotation, scaling, reflection and shearing of 2D
object.

Step 3: Get the needed parameters for the transformation from the user.
Step 4: Incase of rotation, object can be rotated about x or y axis.
Step 5: Display the transmitted object in the screen.
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void input();
void output();
void translation();
void rotation();
void scaling();
int a[10][2],i,x,temp,y,n,fx,fy,opt,ch,j=1;
int sx,sy;
int tx,ty;
float k;
void input()
{
printf("\t\t\tTranslation");
printf("\nEnter the number of lines: " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the coordinates for the Line: ");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
19

{
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
printf("enter the tranformation vertex tx,ty:\n");
scanf("%d%d",&tx,&ty);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before translation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After translation");
outtextxy(520,460,"Computer Graphics");
delay(100);
}
void rotation()
{
printf("\nEnter the rotating angle: ");
scanf("%d",&y);
printf("\nEnter the pivot point:");
scanf("%d%d",&fx,&fy);
cleardevice();
output();
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
tx=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
ty=fy+(a[i][0]-fx)*sin(k)+(a[i][1]-fy)*cos(k);
a[i][0]=tx;
a[i][1]=ty;
}
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before rotation");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After rotation");
20

outtextxy(520,460,"GRAPHICS");
delay(100);
}
void scaling()
{
printf("\nEnter the scaling factor: ");
scanf("%d%d",&sx,&sy);
cleardevice();
output();
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx;
a[i][1]=a[i][1]*sy;
}
setlinestyle(2,1,1);
output();
setlinestyle(0,1,1);
line(75,460,125,460);
outtextxy(130,460,"Before scaling");
setlinestyle(2,1,1);
line(300,460,350,460);
outtextxy(355,460,"After scaling");
outtextxy(520,460,"GRAPHICS");
delay(100);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
input();
output();
do
{
printf("\nWhat Do you want to do with current object\n");
printf("\n1:Translation\n2:Rotation\n3:Scaling\n");
printf("\nEnter your Choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:translation();
break;
case 2:rotation();
break;
case 3:
scaling();
break;
}
delay(6000);
closegraph();
initgraph(&gd,&gm,"D:\\TC\\bgi");
setlinestyle(0,1,1);
printf("To continue press 1 & to EXIT press 0:");
21

scanf("%d",&j);
if(j==1)
{
printf("\nThe current object has the line coordinates:");
for(i=0;i<n;i++)
{
printf("\n");
printf("For line %d:",i+1);
printf("%d %d %d %d",a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
}while(j==1);
getch();}

OUTPUT:

22

23

24

RESULT:
Thus the program is executed and verified.

25

EXP.NO:5
DATE:

COHEN SUTHERLAND 2D LINE CLIPPING AND


WINDOWING

AIM:
To implement Cohen-Sutherland clipping Algorithm.
ALGORITHM:
Step 1: Create a class sulc with functions drawwindow, drawline, setcode,
visibility and reset endpoint.
Step 2: Using the function line set the parameters to draw window.
Step 3: Using the function defined in class sulc, setcode is used to save the line
inside the window and to the line outside the window.
Step 4: Using the function visibility
i).check the code to know the points inside or outside the window.
ii).if the code value is zero the point is inside the window.
Step 5: Using the function reset end point
i). if the code value for the line is outside the window.
ii).reset the endpoint to the boundary of the window.
Step 6: Initialize the graphics functions
Step 7: Declare the variables x1, x2, y1, y2 of array type.
Step 8: Get the value of two endpoints x1, y1 and x2, y2 to draw the line.
Step 9: Using the object c, display the window before clipping.
Step 10: Using the function setcode, visibility display the clipped window only
with lines inside the window class was displayed after clipping.

26

PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
typedef struct coord
{
intx,y;
char code[4];
}pt;
classsulc
{
public:
void drawwindow();
void drawline(pt p1,pt p2,int c1);
ptsetcode(pt p);
int visibility(pt p1,pt p2);
ptresetendpt(pt p1,pt p2);
};
voidsulc::drawwindow()
{
setcolor(WHITE);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
voidsulc::drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
ptsulc::setcode(pt p)
{
ptptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.y>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.y<150)
ptemp.code[3]='1';
27

else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
intsulc::visibility(pt p1,pt p2)
{
inti,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);
}
ptsulc::resetendpt(pt p1,pt p2)
{
pt temp;
intx,y,i;
floatm,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'))
{
28

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];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
else
return(p1);
}
void main()
{
intgd=DETECT,gm,v;
sulc c1;
pt p1,p2,ptemp;
initgraph(&gd,&gm," c:/tc/bgi ");
int x1[10],y1[10],x2[10],y2[10];
cleardevice();
inti,n;
settextstyle(4,0,4);
outtext("cohensutherland line clipping");
cout<<"\n\n enter the no.of lines:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n enter end-point1(x1,y1):";
cin>>x1[i]>>y1[i];
cout<<"\n\n enter end-point2(x2,y2):";
cin>>x2[i]>>y2[i];
}
cleardevice();
settextstyle(0,0,3);
outtext("before clipping");
c1.drawwindow();
for(i=0;i<n;i++)
{
p1.x=x1[i];
p1.y=y1[i];
p2.x=x2[i];
p2.y=y2[i];
c1.drawline(p1,p2,15);
}
getch();
cleardevice();
settextstyle(0,0,3);
outtext("after clipping");
for(i=0;i<n;i++)
{
29

p2.x=x2[i];
p2.y=y2[i];
p1=c1.setcode(p1);
p2=c1.setcode(p2);
v=c1.visibility(p1,p2);
switch(v)
{
case 0:
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
case 1:
c1.drawwindow();
break;
case 2:
p1=c1.resetendpt(p1,p2);
p2=c1.resetendpt(p2,p1);
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
}
}
getch();
closegraph();
}

30

OUTPUT:
Enter the no.of lines: 1
Enter end-point1(x1,y1):30 40
Enter end-point1(x2,y2):300 400

RESULT:
Thus the program is executed and verified.

31

EX.NO :6
Date :

SUTHERLAND-HODGEMAN POLYGON CLIPPING

AIM:
To write a C program to implement polygon clipping.
ALGORITHM
Step 1: Get the minimum and maximum coordinates of both window and view
port.
Step 2: Get the number of sides of a polygon and its corresponding
coordinates.
Step 3: If the polygon lies within region code window, display it.
Step 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.
Step 5: Display the polygon after clipping
PROGRAM:
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<stdio.h>
#define TRUE 1
#define FALSE 0
#define MAX 50
unsigned char a;
void interrupt (*ptr)();
void interrupt key()
{
a=0;
a=inportb(0x60);
ptr();
poke(0x0040,0x001a,peek(0x0040,0x001c));
}
enum bound{left,bottom,right,top};
struct vertex
{ floatx,y;};
typedefstruct win { float xmin,xmax,ymin,ymax;};
32

intnum_vertices =0;
struct vertex polyin[MAX];
intoutlen=0,i;
struct vertex polyout[MAX];
voiddraw_polygon(intnum_vertices,struct vertex *vertices,int color)
{
setcolor(color);
for(i=0;i<num_vertices-1;i++)
{
line(vertices[i].x,vertices[i].y,vertices[i+1].x,vertices[i+1].y);
}
line (vertices[0].x,vertices[0].y,vertices[num_vertices1].x,vertices[num_vertices-1].y);
}
int inside(struct vertex pt,struct win body,enum bound side)
{
int status =FALSE;
switch(side)
{
case top:
if(pt.y<=body.ymax) status=TRUE;
break;
case bottom:
if(pt.y>=body.ymin) status=TRUE;
break;
case right:
if(pt.x<=body.xmax) status =TRUE;
break;
case left:
if(pt.x>=body.xmin) status= TRUE;
break;
}
return status;
}
void out(struct vertex point)
{
polyout[outlen]=point;
outlen++;
}
struct vertex intersect(struct vertex v1,struct vertex v2,struct win body,enum
bound side)
{
struct vertex i;
float m=0;
if(v1.x!=v2.x) m=(v2.y-v1.y)/(v2.x-v1.x);
switch(side)
{
case top:
i.y=body.ymax;
if(v1.x==v2.x) i.x=v2.x;
33

elsei.x=v2.x+(body.ymax-v2.y)/m;
break;
case bottom:
i.y=body.ymin;
if(v1.x==v2.x)
i.x=v2.x;
elsei.x=v2.x+(body.ymin-v2.y)/m;
break;
case left:
i.x=body.xmin;
i.y=v2.y+m*(body.xmin-v2.x);
break;
case right:
i.x=body.xmax;
i.y=v2.y+m*(body.xmax-v2.x);
break;
}
return i;
}
voidsuth_hodg(intnum_ver,struct win body,enum bound edge)
{
struct vertex i,v1,v2;
int j;
v1=polyin[num_ver-1];
for(j=0;j<num_ver;j++)
{
v2=polyin[j];
if(inside(v2,body,edge))
{
if(inside(v1,body,edge))
out(v2);
else
{
i=intersect(v1,v2,body,edge);
out(i);
out(v2);
}
}
else if(inside(v1,body,edge))
{
i=intersect(v1,v2,body,edge);
out(i);
}v1=v2;
}
}
voidtransfer_points(void)
{int i;
num_vertices=outlen;
outlen=0;
for( i=0;i<num_vertices;i++)
34

polyin[i]=polyout[i];
}
void main()
int x=320,y=240,i,ix,iy,cur_x=320,cur_y=240;
int border=100;
struct win body;
intgd=VGA,gm=VGAHI;
initgraph(&gd,&gm," c:/tc/bgi ");
body.xmin=border;
body.xmax=getmaxx()-border;
body.ymin=border;
body.ymax=getmaxy()-border;
setcolor(GREEN);
rectangle(body.xmin,body.ymin,body.xmax,body.ymax);
setcolor(RED);
line(x-10,y,x+10,y);
line(x,y-10,x,y+10);
printf("\t use arrow keys to move the cursor,spacebar to select vertices");
printf("\t esc key to end polygon vertices selection\n");
ptr=getvect(0x9);
setvect(0x9,key);
do
{
switch(a)
{
case 75:
if(x>0) x-=7;
break;
case 77:
if(x<getmaxx()) x=x+7;
break;
case 72:
if(y>0) y-=7;
break;
case 80:
if(y<getmaxy()) y+=7;
break;
case 57:
if(ix!=x || iy!=y)
{
polyin[num_vertices].x=x;
polyin[num_vertices].y=y;
circle(x,y,4);
num_vertices++;
ix=x;iy=y;
}
break;
}
if(cur_x !=x||cur_y!=y)
{
35

clearviewport();
for(i=0;i<num_vertices;i++)
circle(polyin[i].x,polyin[i].y,4);
setcolor(RED);
line(x-10,y,x+10,y);
line(x,y-10,x,y+10);
setcolor(GREEN);
rectangle(body.xmin,body.ymin,body.xmax,body.ymax);
cur_x=x;cur_y=y;
}
}
while(a!=1);
setvect(0x9,ptr);
draw_polygon(num_vertices,polyin,YELLOW);
printf("%d",num_vertices);
suth_hodg(num_vertices,body,left);
transfer_points();
suth_hodg(num_vertices,body,bottom);
transfer_points();
suth_hodg(num_vertices,body,right);
transfer_points();
suth_hodg(num_vertices,body,top);
printf("\t press any key to clip the drawn polygonb");
getch();
cleardevice();
setcolor(GREEN);
rectangle(body.xmin,body.ymin,body.xmax,body.ymax);
draw_polygon(outlen,polyout,YELLOW);
getch();
restorecrtmode();
}

36

OUTPUT:
Enter the window size:
100 400 100 400
Enter the number of vertices:
80 200 200 70 450 160 150 420

RESULT:
Thus the program is executed and verified.

37

Ex.No :7

3D- TRANSFORMATION

Date :

AIM:
To perform 3D transformations such as translation, rotation and scaling.
ALGORITHM:
Step 1: Create a class cube with function draw cube.
Step 2: Use the function draw cube to draw a cube using eight points by means
of functions line.
Step 3: Declare the variables x1, y1, x2, y2, x3, y3, in array type which of data
type int.
Step 4:Declare the variables theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5: Initialize graphics functions.
Step 6: Input the first point in the cube.
Step 7: Input the size of the edge.
Step 8: Create an object to call the function.
Step9: Using switch operation selects the operation to perform translation,
rotation, scaling.
Step 10: Translation
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
c).using the function line, display the object before and after
translation.
Step11: Rotation:
a). input the rotation angle
b). using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d). if the direction is along x axis,
38

x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
if the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
e).calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f). using the function line,display the object before and after rotation.
Step12: Scaling:
a).input the scaling factor and reference point
b).calculate coordinates point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d). using the function line, display the object before and after scaling.
Step13: Stop.

39

PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
voiddrawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}
for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int
i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,t
z,sx,sy,sz,xf,yf,zf,x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode," c:/tc/bgi ");
cout<<"enter the points on the cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
40

y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
do
{
cout<<"menu"<<endl;
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after translation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the direction"<<endl;

41

cout<<"1.rotation about x axis"<<endl<<"2.rotation about y


axis"<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
}
else
if(op==2)
{
for(i=0;i<8;i++)
{
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
else
if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after rotation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
42

case 3:
cout<<"enter the scaling factor:";
cin>>sx>>sy>>sz;
cout<<"enter the reference point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(op!=4);
getch();
}

43

OUTPUT:
Enter the point in the cube: 100 100 100
Enter the size of the edge: 50
Menu
1. translation
2. rotation
3. scaling
4. exit

Enter the choice:1


Enter the translation vector: 5,10,15
Before translation

After translation

RESULT:
Thus the program is executed and verified

44

EX.NO:8
DATE :

COMPOSITE 3D TRANSFORMATIONS

AIM
To write a C program to implement 3D transformations. 3 Dimensional
Transformations

ALGORITHM
Step 1: Enter the choice for transformation.
Step 2: Perform the translation, rotation, scaling of 3D object.
Step 3: Get the needed parameters for the transformation from the user.
Step 4: Increase of rotation, object can be rotated about x or y or z axis.
Step 5: Display the transmitted object in the screen
PROGRAM:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
intmaxx,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,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"d:\\tc\\bgi");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
45

printf("\Enter the translation factor");


scanf("%d%d",&x,&y);
axis();
printf("After translation");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x+100,midy-(y+150),midx+x+60,midy-(y+100),10,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the scaling factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After scaling");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+(x*100),midy-(y*150),midx+(x*60),midy-(y*100),10*z,1);
axis();
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
printf("Enter the rotation angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*sin(o*3.14/180)+100*cos(o*3.14/180);
x2=60*cos(o*3.14/180)-90*sin(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("After rotating about Z-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,10,1);
axis(); printf("After rotating about x-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+100,midy-x1,midx+60,midy-x2,10,1);
axis(); printf("After rotating about Y-axis");
bar3d(midx+100,midy-150,midx+60,midy-100,10,1);
bar3d(midx+x1,midy-150,midx+x2,midy-100,10,1);
getch(); closegraph();
}

46

OUTPUT:

47

RESULT:
Thus the C program to implement the Composite 3D-Transformations
was written, executed and the output was verified successfully.

48

EX.NO :9
DATE :

DRAWING THREE DIMENSIONAL OBJECTS


AND SCENES

AIM
To draw 3D objects and scenes
ALGORITHM:
Step1: Open the 3ds max software
Step2: Then click the create button which is in right side of the window
Step3: Select system icon in that selects biped
Step4: Drag the cursor over the perspective window to draw the skeleton
Step5: Select the motion icon in that select footstep mode
Step6: Click multiple footstep dialog box fix number of steps to walk then
click ok
Step7: Click create footstep, then click create keys for inactive footstep.
Step8: In main menu select rendering ---- > render setup
Step9: In the dialog box enable active time segment select movie size as
720*486,select the output path and the format of video,In the file format
dialog box set key frame as 1 then click ok
Step10: Finally click render in the render setup dialog box.
Step11: Now the output video is present in the specified path

49

OUTPUT:

RESULT
Thus the program is executed and verified.

50

EX.NO : 10
DATE :

GENERATING FRACTAL IMAGES

AIM:
To generate fractal images.
ALGORITHM
Step 1: The sierpinski Triangle is created by infinite removals
Step 2: Each triangle is divided into 4 smaller upside down triangles
Step 3: The center of the 4 triangles is removed
Step 4: As the process is iterated infinite number of times, the total area of the
Step 5: Set goes to infinity as the size of the each new triangle goes to zero
Step 6: After closer examination magnification factor is 2.
Step 7:With each magnification there are 3 divisions of a triangleDimension
D=ln(3)/ln(2)D=1.5850

51

PROGRAM:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<process.h>
#include<alloc.h>
#include<dos.h>
void draw()
{
arc(500,100,0,90,20);
arc(540,100,90,180,200);
arc(250,80,0,90,20);
arc(290,80,90,180,20);
arc(450,90,0,90,20);
arc(490,90,90,180,20);
rectangle(80,300,200,460);
rectangle(120,360,160,460);
circle(155,410,3);
line(80,300,140,230);
line(140,230,200,300);
}
int main(void)
{
intx,y;
intgdriver=DETECT,gmode,errorcode;
intleft,top,right,bottom;
unsigned size;
unsigned size1;
void far *buf;
void far *buf1;
initgraph(&gdriver,&gmode,"..bgi");
errorcode=graphresult();
if (errorcode!=grOk)
{
printf("Graphics error:%s\n",grapherrormsg(errorcode));
printf("press any key to halt:");
getch();
exit(1);
}
circle(40,400,10);
line(40,410,40,450);
line(40,420,50,430);
line(40,420,30,430);
line(40,450,35,460);
line(40,450,45,460);
line(40,450,45,460);
draw();
size=imagesize(30,400,45,460);
buf=farmalloc(size);
getimage(30,380,50,460,buf);
for(x=30,y=400; x<=140; x=x+10)
{
putimage(x,y,buf,COPY_PUT);
draw();
52

delay(1000);
cleardevice();
}
getch();
closegraph();
return 0;
}

OUTPUT:

RESULT:
Thus the program is executed and verified.

53

You might also like