Professional Documents
Culture Documents
No: 1
Date:
DDA ALGORITHM FOR DRAWING LINE
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw(int x1,int y1,int x2,int y2);
int main(void)
{
int x1,y1,x2,y2;
int gdriver=DETECT,gmode,gerror;
printf("\n Enter the x and y value for starting point:\n");
scanf("%d%d",&x1,&y1);
printf("\n Enter the x and y value for ending point:\n");
scanf("%d%d",&x2,&y2);
clrscr();
initgraph(&gdriver,&gmode,"E:\\TC\\BGI\\");
draw(x1,y1,x2,y2);
getch();
return 0;
}
void draw(int x1,int y1,int x2,int y2)
{
float x,y,xinc,yinc,dx,dy;
int k,step;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
step=abs(dx);
else
step=abs(dy);
xinc=dx/step;
yinc=dy/step;
x=x1;
y=y1;
putpixel(abs(x),abs(y),111);
for(k=1;k<=step;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(abs(x),abs(y),111);
}
}
Output:
Enter the x and y value for starting point: 50 60
Enter the x and y value for ending point: 70 90
Ex.No: 1
Date:
BRESENHAMS ALGORITHM FOR LINE DRAWING
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int x,y,x1,y1,x2,y2,p,dx,dy;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\BGI:");
printf("\nEnter the x-coordinate of the first point ::");
scanf("%d",&x1);
printf("\nEnter the y-coordinate of the first point ::");
scanf("%d",&y1);
printf("\nEnter the x-coordinate of the second point ::");
scanf("%d",&x2);
printf("\nEnter the y-coordinate of the second point ::");
scanf("%d",&y2);
x=x1;
y=y1;
dx=x2-x1;
dy=y2-y1;
putpixel(x,y,2);
p=(2dy-dx);
while(x<=x2)
{
if(p<0)
{
x=x+1;
p=2*x-dx;
}
else
{
x=x+1;
y=y+1;
p=p+2*dy;
}
putpixel(x,y,7);
}
getch();
closegraph();
}
Output:
Enter the x-coordinate of the first point ::40
Enter the y-coordinate of the first point ::50
Enter the x-coordinate of the second point ::60
Enter the y-coordinate of the second point ::80
Ex.No:1
Date:
BRESENHAMS ALGORITHM TO DRAW A CIRCLE
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void circlepoints(int,int);
void main()
{
int x,y,p,r;
int gdriver=DETECT,gmode;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi:");
clrscr();
printf("Enter the radius");
scanf("%d",&r);
x=0;y=r;p=1-r;
while(x<y)
{
x++;
if(p>0)
{
p=p+2*(x-y)+1;
y--;
}
else
p=p+2*x+1;
circlepoints(x,y);
}
getch();
closegraph();
}
void circlepoints(int x,int y)
{
putpixel(x+300,y+300,8);
putpixel(x+300,-y+300,8);
putpixel(-x+300,y+300,8);
putpixel(-x+300,-y+300,8);
putpixel(y+300,x+300,8);
putpixel(y+300,-x+300,8);
putpixel(-y+300,x+300,8);
putpixel(-y+300,-x+300,8);
}
Output:
Enter the radius 50
Ex.No: 1
Date:
BRESENHAM'S ALGORITHM FOR ELLIPSE
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp();
float x,y;
int xc,yc;
void main()
{
int gd=DETECT,gm;
int a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"");
printf("\n Enter the value for xc:");
scanf("%d",&xc);
printf("\n Enter the value for yc:");
scanf("%d",&yc);
printf("\n Enter the value for a:");
scanf("%d",&a);
printf("\n Enter the value for b:");
scanf("%d",&b);
x=0;y=b;
disp();
p1=(b*b)-(a*a*b)+(a*a)/4;
while((2.0*b*b*x)<=(2.0*a*a*y))
{
x++;
if(p1<=0)
p1=p1+(2.0*b*b*x)+(b*b);
else
{
y--;
p1=p1+(2.0*b*b*x)+(b*b)-(2.0*a*a*y);
}
disp();
x=-x;
disp();
x=-x;
}
x=a;
y=0;
disp();
p2=(a*a)+2.0*(b*b*a)+(b*b)/4;
while((2.0*b*b*x)>(2.0*a*a*y))
{
y++;
if(p2>0)
p2=p2+(a*a)-(2.0*a*a*y);
else
{
x--;
p2=p2+(2.0*b*b*x)-(2.0*a*a*y)+(a*a);
}
disp();
y=-y;
disp();
y=-y;
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,10);
putpixel(xc-x,yc+y,10);
putpixel(xc+x,yc-y,10);
putpixel(xc+x,yc-y,10);
}
Output:
Enter the value for xc: 50
Enter the value for yc: 50
Enter the value for a: 125
Enter the value for b: 150
line(20,20+s*50,120,120+s*50);
outtextxy(125,120+s*50,lname[s]);
}
getch();
closegraph();
break;
}
break;
case 2:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
maxcolor=getmaxcolor(); /* draw circles of different colors*/
for(color=1;color<=maxcolor;color++)
{
cleardevice();
setcolor(color);
/* draw the circle */
circle(midx,midy,radius);
/* clean up */
getch();
}
closegraph();
break;
case 3:
printf("\n1.pattern 2.colour\n"); /* get choice for color or style of eclipse*/
printf("\nEnter your choice:\n");
scanf("%d",&ch2);
switch(ch2) {
case 1:
/* initialize graphics and local variables */
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
midx=getmaxx()/2;
midy=getmaxy()/2;
/* loop through the fill patterns */
for(i=EMPTY_FILL;i<USER_FILL;i++)
{
/* set fill pattern */
setfillstyle(i,getmaxcolor());
/* draw a filled ellipse */
fillellipse(midx, midy, xradius, yradius);
getch();
}
closegraph();
break;
case 2:
initgraph(&gdriver,&gmode,"c:\\tc\\bgi");
maxcolor=getmaxcolor();
for(color=1;color<=maxcolor;color++)
{
cleardevice();
setcolor(color);
ellipse(100,200,0,360,xradius,yradius);
getch();
}
/* clean up */
closegraph();
break;
}
default:
//exit(0);
break;
}
}
while(ch==3);
return 0;
}
OUTPUT:
Line Color
Circle colour
Ellipse Pattern
Ellipse Color
Ex.No: 3
TWO DIMENSIONAL TRANSFORMATION - TRANSLATION
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,i,a[100][100],tx,ty;
void input()
{
printf("enter the no of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the co-ordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void translation()
{
output();
printf("enter the transformation vertex tx,ty:");
scanf("%d%d",&tx,&ty);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]+tx;
a[i][1]=a[i][1]+ty;
}
output();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"Z:\\BGI\\");
input();
translation();
getch();
}
Output:
enter the no of vertices:3
enter the co-ordinates:50 50 50 100
enter the co-ordinates:50 100 75 75
enter the co-ordinates:75 75 50 50
Ex.No: 3
TWO DIMENSIONAL TRANSFORMATION - ROTATION
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int n,i,a[100][100],fx,fy,y,k,temp;
void input()
{
printf("enter the no of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the co-ordinates:");
scanf("%d%d",&a[i][0],&a[i][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n-1;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[i][0],a[i][1],a[0][0],a[0][1]);
}
void rotation()
{
output();
printf("enter the rotating angle:");
scanf("%d",&y);
printf("enter the pivot point:");
scanf("%d%d",&fx,&fy);
k=(y*3.14)/180;
for(i=0;i<=n;i++)
{
temp=a[i][0]-a[i][1]*sin(k);
a[i][1]=a[i][0]*sin(k)+a[i][1];
a[i][0]=temp;
}
output();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"Z:\\BGI\\");
input();
rotation();
getch();
}
Output:
enter the no of vertices:3
enter the co-ordinates:30 150 10 200
enter the co-ordinates:10 200 60 200
enter the co-ordinates:60 200 30 150
Ex.No: 3
TWO DIMENSIONAL TRANSFORMATION - SCALING
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
int n,i,a[100][100],fx,fy;float sx,sy;
void input()
{
printf("enter the no of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the co-ordinates:");
scanf("%d%d%d%d",&a[i][0],&a[i][1],&a[i+1][0],&a[i+1][1]);
}
}
void output()
{
cleardevice();
for(i=0;i<n;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
}
void scaling()
{
output();
printf("enter the scaling factor sx,sy:");
scanf("%f%f",&sx,&sy);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
for(i=0;i<=n;i++)
{
a[i][0]=a[i][0]*sx+fy*(1-sx);
a[i][1]=a[i][1]*sy+fy*(1-sy);
}
output();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"Z:\\BGI\\");
input();
scaling();
getch();
Output:
Ex.No: 3
TWO DIMENSIONAL TRANSFORMATION - REFLECTION
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int x1,y1,x2,y2,d,a[20][2],n,i;
void input();
void output();
void reflection();
void input()
{
x1=0,y1=getmaxy()/2,x2=getmaxx(),y2=getmaxy()/2;
printf("enter the no of vertices:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n enter the co-ordinates:");
scanf("%d%d",&a[i][0],&a[i][1]);
}
cleardevice();
printf("Before reflection");
output();
getch();
cleardevice();
reflection();
printf("\nAfter reflection about X-axis:");
output();
}
void reflection()
{
for(i=0;i<n;i++)
{
d=y1-a[i][1];
if(d<0)
{
d=-1*d;
a[i][1]=a[i][0]-2*d;
}
else
{
a[i][1]=a[i][1]+2*d;
}
}
}
void output()
{
line(x1,y1,x2,y2);
for(i=0;i<n-1;i++)
{
line(a[i][0],a[i][1],a[i+1][0],a[i+1][1]);
}
line(a[i][0],a[i][1],a[0][0],a[0][1]);
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"E:\\TC\\BGI\\");
input();
getch();
}
Output:
enter the no of vertices:3
enter the co-ordinates:50 50 50 100
enter the co-ordinates:50 100 75 75
enter the co-ordinates:75 75 50 50
Before Reflection:
Ex.No:4
IMPLEMENTATION OF TWO DIMENSIONAL COMPOSITE
TRANSFORMATIONS
CODING:
#include <graphics.h> /* include the necessary header files*/
#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:\\TC\\BGI"); /* request for auto detection*/
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"); /* get the coordinates for the original image*/
line(xa,ya,xb,yb); /* draw the original image*/
line(xb,yb,xc,yc);
line(xc,yc,xa,ya);
printf("\n\n Enter the value tranlsation factor :"); /* get the translation factor*/
scanf("%d",&tx);
printf("\n\n After Translation ");
x1a=xa+tx;
x1b=xb+tx;
x1c=xc+tx;
y1a=ya;
y1b=yb;
y1c=yc;
line(x1a,y1a,x1b,y1b); /* image after translation*/
line(x1b,y1b,x1c,y1c);
line(x1c,y1c,x1a,y1a);
delay(1);
printf("\n\n Next Operation is Rotation");
printf("\n\n Enter the rotation angle :"); /* get the angle of rotation*/
scanf("%f",&ang);
theta=((ang*3.14)/180); /* convert the angle*/
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);
printf("\n\n After Rotation "); /* the rotated object*/
line(x2a,y2a,x2b,y2b);
line(x2b,y2b,x2c,y2c);
line(x2c,y2c,x2a,y2a);
delay(1);
printf("\n\n Next Operation is Scaling"); /* get the scale factor*/
printf("\n\n Enter the Scale factor :");
scanf("%f %f",&sx,&sy);
x3a=x2a+sx; /* modify the objects coordinates based on the scale factor*/
y3a=y2a+sy;
x3b=x2b+sx; y3b=y2b+sy;
x3c=x2c+sx;
y3c=y2c+sy;
printf("\n\n After Scaling ");
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
delay(1);
printf("\n\n Next Operation is Shearing");
printf("\n\n Enter 1 for x-axis \n 2 for y-axis: "); /* get the choice of shearing in the x or y
axis*/
scanf("%d",&ch);
if(ch==1) /* get the shear value*/
{
printf("\n\n Enter the x-SHEAR (^.^) Value: ");
scanf("%d",&shx);
}
else
{
printf("\n\n Enter the y-SHEAR (^.^) Value: ");
scanf("%d",­);
}
if(ch==1)
{
x3a=x3a+shx*y3a;
y4a=y3a;
x3b=x3a+shx*y3a;
y4b=y3b;
x3c=x3a+shx*y3a;
y4c=y3c;
}
else
{
x4a=x3a;
y3a=y3a+shy*x3a;
x4b=x3b;
y3b=y3b+shy*x3b;
x4c=x3c;
y3c=y3c+shy*x3c;
}
printf("\n\n After Shearing "); /* draw the final object after shearing*/
line(x3a,y3a,x3b,y3b);
line(x3b,y3b,x3c,y3c);
line(x3c,y3c,x3a,y3a);
delay(1);
printf("\n\n Next Operation is Reflection");
t=abs(y3a-y3c); /* calculate the value for reflection*/
x5a=x3a;
x5b=x3b;
x5c=x3c;
y5a=y3a+10+(2*t);
y5b=y3b+10;
y5c=y3c+10;
printf("\n\n After Reflection "); /* the final object after all the transformations*/
line(x5a,y5a,x5b,y5b);
line(x5b,y5b,x5c,y5c);
line(x5c,y5c,x5a,y5a);
getch();
closegraph();
return 0;
}
OUTPUT:
2D Composite Transformations
Enter all coordinates values 213 236 253 321 256 214
The original Image
After Scaling
Next Operation is Shearing
Enter 0 for x axis and 1 for y axis 0
Enter the x-shear value 1
After Shearing
Ex.No: 5
Date:
COHEN SUTHERLAND 2D LINE CLIPPING ALGORITHM
Program:
#include<stdio.h>
#include<graphics.h>
#include<conio.h>
typedef unsigned int outcode;
enum { TOP=0x1, BOTTOM=0x2, RIGHT=0x4, LEFT=0x8 };
void lineclip(x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax )
float x0,y0,x1,y1,xwmin,ywmin,xwmax,ywmax;
{
int gd,gm;
outcode code0,code1,codeout;
int accept = 0, done=0;
code0 = calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
do
{
if(!(code0 | code1))
{
accept =1 ; done =1;
}
else if(code0 & code1) done = 1;
else
{
float x,y;
codeout = code0 ? code0 : code1;
if(codeout & TOP)
{
x = x0 + (x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
}
else if( codeout & BOTTOM)
{
x = x0 + (x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
}
else if ( codeout & RIGHT)
{
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
}
else
{
y = y0 + (y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
}
if( codeout == code0)
{
x0 = x; y0 = y;
code0=calcode(x0,y0,xwmin,ywmin,xwmax,ywmax);
}
else
{
x1 = x; y1 = y;
code1 = calcode(x1,y1,xwmin,ywmin,xwmax,ywmax);
}
}
}
while( done == 0);
if(accept) line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
}
int calcode (x,y,xwmin,ywmin,xwmax,ywmax)
float x,y,xwmin,ywmin,xwmax,ywmax;
{
int code =0;
if(y> ywmax)
code |=TOP;
else if( y<ywmin)
code |= BOTTOM;
else if(x > xwmax)
code |= RIGHT;
else if ( x< xwmin)
code |= LEFT;
return(code);
}
void main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"Z:\\bgi");
printf("\n\n\tEnter the co-ordinates of Line :");
printf("\n\n\tX1 Y1 : ");
scanf("%f %f",&x1,&y1);
printf("\n\n\tX2 Y2 : ");
scanf("%f %f",&x2,&y2);
printf("\n\tEnter the co_ordinates of window :\n ");
printf("\n\txwmin , ywmin : ");
scanf("%f %f",&xwmin,&ywmin);
printf("\n\txwmax , ywmax : ");
scanf("%f %f",&xwmax,&ywmax);
cleardevice();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
cleardevice();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax );
getch();closegraph();
}
Output:
Enter the co-ordinates of line:
X1 y1:100 150
X2 y2:200 250
Enter the co-ordinates of window:
Xwmin,ywmin: 110 140
Xwmax,ywmax:210 240
Ex.No: 6
Date:
SUTHERLAND - HODGEMAN POLYGON CLIPPING ALGORITHM
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#define round(a) ((int)(a+0.5))
int k;
float xmin,ymin,xmax,ymax,arr[20],m;
void clipl(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1>=xmin && x2>=xmin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1<xmin && x2>=xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1>=xmin && x2<xmin)
{
arr[k]=xmin;
arr[k+1]=y1+m*(xmin-x1);
k+=2;
}
}
void clipt(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1<=ymax && y2<=ymax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1>ymax && y2<=ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1<=ymax && y2>ymax)
{
arr[k]=x1+m*(ymax-y1);
arr[k+1]=ymax;
k+=2;
}
}
void clipr(float x1,float y1,float x2,float y2)
{
if(x2-x1)
m=(y2-y1)/(x2-x1);
else
m=100000;
if(x1<=xmax && x2<=xmax)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(x1>xmax && x2<=xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(x1<=xmax && x2>xmax)
{
arr[k]=xmax;
arr[k+1]=y1+m*(xmax-x1);
k+=2;
}
}
void clipb(float x1,float y1,float x2,float y2)
{
if(y2-y1)
m=(x2-x1)/(y2-y1);
else
m=100000;
if(y1>=ymin && y2>=ymin)
{
arr[k]=x2;
arr[k+1]=y2;
k+=2;
}
if(y1<ymin && y2>=ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
arr[k+2]=x2;
arr[k+3]=y2;
k+=4;
}
if(y1>=ymin && y2<ymin)
{
arr[k]=x1+m*(ymin-y1);
arr[k+1]=ymin;
k+=2;
}
}
void main()
{
int gdriver=DETECT,gmode,n,poly[20],i;
float xi,yi,xf,yf,polyy[20];
clrscr();
printf("Coordinates of rectangular clip window :\nxmin,ymin
scanf("%f%f",&xmin,&ymin);
printf("xmax,ymax
:");
scanf("%f%f",&xmax,&ymax);
printf("\n\nPolygon to be clipped :\nNumber of sides
:");
scanf("%d",&n);
printf("Enter the coordinates :");
for(i=0;i<2*n;i++)
scanf("%f",&polyy[i]);
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
for(i=0;i<2*n+2;i++)
poly[i]=round(polyy[i]);
initgraph(&gdriver,&gmode,"Z:\\BGI\\");
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
printf("\t\tUNCLIPPED POLYGON");
setcolor(WHITE);
fillpoly(n,poly);
getch();
cleardevice();
k=0;
for(i=0;i<2*n;i+=2)
clipl(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipt(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
:");
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipr(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
n=k/2;
for(i=0;i<k;i++)
polyy[i]=arr[i];
polyy[i]=polyy[0];
polyy[i+1]=polyy[1];
k=0;
for(i=0;i<2*n;i+=2)
clipb(polyy[i],polyy[i+1],polyy[i+2],polyy[i+3]);
for(i=0;i<k;i++)
poly[i]=round(arr[i]);
if(k)
fillpoly(k/2,poly);
setcolor(RED);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
Output:
Co-ordinates of rectangular clip window:
Xmin,ymin:20 20
Xmax,ymax: 100 100
Polygon to be clipped:
Enter the co-ordinates:
UNCLIPPED POLYGON:
CLIPPED POLYGON:
Ex.No: 7
THREE DIMENSIONAL TRANSFORMATIONS
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,o,x1,x2,y1,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"E:\\TC\\BGI\\");
setfillstyle(0,getmaxcolor());
maxx=getmaxx();
maxy=getmaxy();
midx=maxx/2;
midy=maxy/2;
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Translation Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After Translation");
bar3d(midx+(x+50),midy-(y+100),midx+x+60,midy-(y+90),5,1);
axis();
bar3d(midx+50,midy+100,midx+60,midy-90,5,1);
printf("Enter Scaling Factor");
scanf("%d%d%d",&x,&y,&z);
axis();
printf("After Scaling");
bar3d(midx+(x*50),midy-(y*100),midx+(x*60),midy-(y*90),5*z,1);
axis();
bar3d(midx+50,midy-100,midx+60,midy-90,5,1);
printf("Enter Rotating Angle");
scanf("%d",&o);
x1=50*cos(o*3.14/180)-100*sin(o*3.14/180);
y1=50*cos(o*3.14/180)+100*sin(o*3.14/180);
x2=60*sin(o*3.14/180)-90*cos(o*3.14/180);
y2=60*sin(o*3.14/180)+90*cos(o*3.14/180);
axis();
printf("\nAfter Rotation about Z Axis");
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
Output :
Translation
Enter Translation Factor : 20 20 20
After Translation
Scaling
Enter Scaling Factor : 2 2 5
After Scaling
Rotation
Enter Rotating Angle : 60
Ex.No:8 3D Transformation
Date:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,mx,my,cont=0,ch;
int sx,sy,tx,ty,angle,a,b;
int xa,xb,ya,yb,xa1,ya1,xb1,yb1,xa2,ya2,xb2,yb2,xa3,ya3,xb3,yb3;
initgraph(&gd,&gm," ");
cleardevice();
mx=100;my=200;xa=mx-50;xb=mx+50;ya=my-50;yb=my+50;
setfillstyle(EMPTY_FILL,9);
do
{
cleardevice();
printf("\n WHAT DO U WANT TO PERFORM?\n 1.TRANSLATION\n 2.SCALING \n
3.ROTATION\n");
printf("enter your option\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n Enter the translation value tx:\n");
scanf("%d",&tx);
printf("\n Enter the translation value ty:\n");
scanf("%d",&ty);
xa1=xa+tx;
xb1=xb+tx;
ya1=ya+ty;
yb1=yb+ty;
cleardevice();
setfillstyle(LINE_FILL,3);
bar3d(xa,ya,xb,yb,25,1);
setfillstyle(LINE_FILL,13);
bar3d(xa1,ya1,xb1,yb1,25,1);
getch();
break;
case 2:
printf("\n Enter the scaling value sx:\n");
scanf("%d",&sx);
printf("\n Enter the scaling value sy:\n");
scanf("%d",&sy);
xa2=xa*sx;
xb2=xb*sx;
ya2=ya*sy;
yb2=yb*sy;
cleardevice();
setfillstyle(LINE_FILL,3);
bar3d(xa,ya,xb,yb,10,1);
Dr.N.N.C.E IT / VII Sem GMM Lab - LM
31
setfillstyle(LINE_FILL,10);
bar3d(xa2,ya2,xb2,yb2,10,1);
getch();break;
case 3:
printf("enter the rotation angle\n");
scanf("%d",&angle);
a=xb+50;
b=yb+50;
xa3=a+((xa-a)*cos(angle))-((ya-b)*sin(angle));
ya3=b+((xa-a)*sin(angle))+((ya-b)*cos(angle));
xb3=a+((xb-a)*cos(angle))-((yb-b)*sin(angle));
yb3=b+((xb-a)*sin(angle))-((yb-b)*cos(angle));
cleardevice();
setfillstyle(LINE_FILL,1);
bar3d(xa,ya,xb,yb,50,1);
setfillstyle(LINE_FILL,5);
bar3d(xa3,ya3,xb3,yb3,50,1);
getch();break;
}
cleardevice();
printf("\n WANT TO CONTINUE YES(0)/NO(1):");
scanf("%d",&cont);
}
while(cont==0);
getch();
closegraph();
}
d) Output:
WHAT DO U WANT TO PERFORM?
1.TRANSLATION
2.SCALING
3.ROTATION
ENTER UR OPTION: 1
ENTER THE TRANSLATION VALUE TX: 200
ENTER THE TRANSLATION VALUE TY: 200
3D PROJECTION OUTPUT
ENTER THE TOP-LEFT AND BOTTOM RIGHT CORNER:
50 50 100 100
ENTER THE DEPTH ALONG Z AXIS: 20
MENU:
1. PARALLEL PROJECTION
2. PRESPECTIVE PROJECTION
3. EXIT
ENTER UR OPTION: 1 TOP VIEW
FRONT VIEW
MENU:
1. PARALLEL PROJECTION
2. PRESPECTIVE PROJECTION
3. EXIT
ENTER UR OPTION: 2
PRESPECTIVE PROJECTION
Ex.No: 8
COMPOSITE THREE DIMENSIONAL TRANSFORMATIONS
Aim:
To write a C program to implement composite 3D transformations.
Algorithm:
1. Start the program
2. Declare the variables and initialise the graphics driver.
3. Get the translation factors,scaling factors form the user.
4. Call the function to draw the 3d bar.
5. Then perform the composite transformations.
6. Display the corresponding outputs.
7. Stop the driver.
8. Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<ctype.h>
#include<math.h>
#include<stdlib.h>
void draw_bar3d()
{
int x1,x2,y1,y2,i,tx,ty,tz,z;
float sx,sy,sz;
x1=200;y1=200;x2=250;y2=250,z=20;
bar3d(x1,y1,x2,y2,z,1);
printf("Enter translation factor and scale factor:");
scanf("%d%d%d%f%f%f",&tx,&ty,&tz,&sx,&sy,&sz);
getch();
cleardevice();
printf("After translation:\n");
setcolor(111);
x1=x1+tx;y1=y1+ty;x2=x2+tx;y2=y2+ty;
bar3d(x1,y1,x2,y2,z,1);
getch();
cleardevice();
x1=x1*sx;y1=y1*sy;z=z*sz;
printf("After scaling:\n");
bar3d(x1,y1,x2,y2,z,1);
getch();
}
void main()
{
int x1,y1,x2,y2,ch;
int gdriver=DETECT,gmode,gerror;
initgraph(&gdriver,&gmode,"D:\\TC\\BGI\\");
draw_bar3d();
getch();
}
Output:
Enter translation factor and scale factor: 100 100 20 1.5 0.5 1
After translation:
After scaling:
Result:
Thus the C program to implement composite 3D transformations was done
successfully.
Ex.No: 9
DRAWING TWO DIMENSIONAL OBJECTS AND SCENES
Program:
#include<conio.h>
#include<graphics.h>
#include<stdio.h>
#include<math.h>
void main()
{
int gd,gm;
int x,y;
int i,j,kk;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\tc\\bgi");
setcolor(WHITE);
line(0,400,640,400);
rectangle(300,330,340,400);
rectangle(310,320,330,330);
setcolor(4);
line(319,280,319,398);
line(320,280,320,398);
rectangle(320,280,330,300);
outtextxy(340,280,"PRESS ANY KEY TO IGNITE THE ROCKET");
getch();
for(j=400;j<640;j++)
{
cleardevice();
setcolor(WHITE);
line(0,j,640,j);
rectangle(300,j-70,340,j);
rectangle(310,j-80,330,j-70);
setcolor(RED);
line(319,280,319,400);
line(320,280,320,400);
rectangle(320,280,330,300);
setcolor(YELLOW);
circle(325,300,2);
delay(5);
}
for(i=400;i>340;i--)
{
cleardevice();
setcolor(RED);
line(319,i,319,i-120);
line(320,i,320,i-120);
rectangle(320,i-120,330,i-100);
setcolor(YELLOW);
circle(325,i-100,2);
delay(25);
}
cleardevice();
kk=0;
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(kk);
kk=kk+3;
delay(50);
}
ellipse(320,30,0,360,j+100,j+0);
}
for(j=100;j<350;j++)
{
if(j%20==0)
{
setcolor(BLACK);
delay(2);
}
ellipse(320,30,0,360,j+100,j+0);
}
cleardevice();
for(i=0;i<70;i++)
{
setcolor(i);
settextstyle(GOTHIC_FONT,HORIZ_DIR,6);
outtextxy(110,150,"HELLO WORLD!");
delay(90);
}
getch();
}
Output:
HELLO WORLD!
Ex.No: 12
glPushMatrix();
glTranslated(1.0,0,0);
glScaled(0.15,0.15,0.15);
glutWireDodecahedron();
glPopMatrix();
glPushMatrix();
glTranslated(0,1.0,1.0);
glutWireCube(0.25);
glPopMatrix();
glPushMatrix();
glTranslated(0,0,1.0);
GLUquadricObj * qobj;
qobj = gluNewQuadric();
gluQuadricDrawStyle(qobj,GLU_LINE);
gluCylinder(qobj,0.2,0.2,0.4,8,3);
glPopMatrix();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow(" Transformation test wire frames");
glutDisplayFunc(displayWire);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();
}
Output: