You are on page 1of 82

Ex1 a

DDA
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void endpt(int,int,int,int);
void eppts(int,int,int,int);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
endpt(300,200,15,80);
getch();
closegraph();
}
void endpt(int xc,int yc,int rx,int ry )
{
int a=rx+ry,b=rx+ry,p,x=0,y=ry,px=0,c=2*b,d=2*a,py=c*y;
p=(b-(b-ry)+(.25*9));
while(px<py)
{
eppts(xc,yc,x,y);
x++;
px+=d;
if(p<c)
p+=b+px;

else
{
y--;
py-=c;
p-=c;
p-=b+px-py;
}
eppts(xc,yc,x,y);
}
p=(b*(x+.5)*(x+.5)+a*(y-1)*(y-1)-a*b);
while(y>0)
{
y--;
py-=c;
if(p>0)
p+=a-py;
else
{
x++;
p+=a-py+px;
}
eppts(xc,yc,x,y);
}
getch();
}
void eppts(int xc,int yc,int x,int y)
{

putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
}

OUTPUT

1.b
LINE DRAWING ALGORITHM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT,gm,x1,x2,y1,y2,k,steps,p,p0,dx,dy,x,y;
clrscr();
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
printf("\n Enter the coordinates ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
setcolor(GREEN);
dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1;
if(dx>dy)
steps=dx;
else
steps=dy;
putpixel(x,y,GREEN);
p0=2*(dx=dy);
p=p0;
for(k=0;k<steps;k++)
{

x=x+1;
if(p0<1)
p=p+(2*dy);
else
{
y=y+1;
p=p+(2*dy-dx);
}
putpixel(x,y,GREEN);
}
getch();
closegraph();
}
OUTPUT

1.c>
CIRCLE GENERATING ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void cmp(int xc,int yc,int r)
{
int x=0,y=r,p=1-r;
void cpp(int,int,int,int);
cpp(xc,yc,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y--;
p+=2*(x-y)+1;
}
cpp(xc,yc,x,y);
}
}
void cpp(int xc,int yc,int x,int y)

{
putpixel(xc+x,yc+y,RED);
delay(20);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
putpixel(xc+y,yc+x,RED);
putpixel(xc-y,yc+x,RED);
putpixel(xc+y,yc-x,RED);
putpixel(xc-y,yc-x,RED);
}
void main()
{
int xc,yc,r,gd,gm;
gd=gm=DETECT;
clrscr();
initgraph(&gd,&gm,"C:\\Turboc3\\bgi");
printf("\n Enter the centre value and radius ");
scanf("%d%d%d",&xc,&yc,&r);
cmp(xc,yc,r);
getch();
closegraph();
}

OUTPUT

1.d>
ELLIPSE GENERATING ALGORITHM
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<graphics.h>
void endpt(int,int,int,int);
void eppts(int,int,int,int);
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\turboc3\\bgi");
endpt(300,200,15,80);
getch();
closegraph();
}
void endpt(int xc,int yc,int rx,int ry )
{
int a=rx+ry,b=rx+ry,p,x=0,y=ry,px=0,c=2*b,d=2*a,py=c*y;
p=(b-(b-ry)+(.25*9));
while(px<py)
{
eppts(xc,yc,x,y);
x++;
px+=d;

if(p<c)
p+=b+px;
else
{
y--;
py-=c;
p-=c;
p-=b+px-py;
}
eppts(xc,yc,x,y);
}
p=(b*(x+.5)*(x+.5)+a*(y-1)*(y-1)-a*b);
while(y>0)
{
y--;
py-=c;
if(p>0)
p+=a-py;
else
{
x++;
p+=a-py+px;
}
eppts(xc,yc,x,y);
}
getch();
}

void eppts(int xc,int yc,int x,int y)


{
putpixel(xc+x,yc+y,RED);
putpixel(xc-x,yc+y,RED);
putpixel(xc+x,yc-y,RED);
putpixel(xc-x,yc-y,RED);
}
OUTPUT

EX-2
OUTPUT PRIMITIVES
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<string.h>
void main()
{
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,xrad,yrad,rad,sa,ea,i;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
while(ch=='y')
{
cleardevice();
setbkcolor(10);
outtextxy(100,150,"Enter 1 to get line");
outtextxy(100,170,"Enter 2 to get circle");
outtextxy(100,190,"Enter 3 to get bar");
outtextxy(100,210,"Enter 4 to get arc ");
outtextxy(100,230,"Enter 5 to get ellipse");
outtextxy(100,250,"Enter 6 to get rectangle");
outtextxy(100,270,"Enter 7 to get text");
scanf("%c",&ch);
cleardevice();
switch(ch)

{
case '1': line(100,20,200,50);
break;
case '2': circle(200,200,50);
break;
case '3': setfillstyle(5,4);
bar(100,300,20,100);
break;
case '4': setfillstyle(5,4);
arc(250,200,100,300,100);
break;
case '5': setfillstyle(5,4);
fillellipse(350,250,250,100);
break;
case '6': settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"MCE");
line(100,100,100,300);
line(300,300,100,300);
line(100,100,300,100);
line(300,100,300,300);
break;
case '7': closegraph();
return;
}
ch='y';
getch();
}

OUTPUT

Ex-3
TWO DIMENSIONAL TRANSFORMATION
#include <graphics.h>
#include <string.h>
#include <stdio.h>
#include <conio.h>
#include <graphics.h>
void main(void)
{
int gd=DETECT, gm,ch;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
cleardevice();
printf("1.Translation\n2.Rotation\n3.Scaling\n4.Reflection\n5.Shearing\n6.Exit\nEnter
choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
int tx1=50,tx2=50,ty2=50,ty1=50,x1=100,x2=230,y1=100,y2=70;
cleardevice();
printf("Rectangle before translation\n");
rectangle(x1,y1,x2,y2);
getch();cleardevice();
printf("Rectangle after translation");
rectangle(x1+tx1,y1+ty1,x2=tx2,y2=ty2);
getch();

main();
}
case 2:
{
int x1=100,y1=100,x2=200,y2=200;
double xt,yt,d;
cleardevice();
printf("Enter angle of rotation");
scanf("%f",&d);
xt=x1+(((x2-x1)*cos(d))-((y2-y1)*sin(d)));
yt=y1+(((x2-x1)*sin(d))-((y2-y1)*cos(d)));
line(x1,y1,x2,y2);
getch();
cleardevice();
printf("link after rotation");
line(x1,y1,xt,yt);
getch();main();
}
case 3:
{
int x1=30,y1=30,x2=70,y2=70,x=2,y=2;
cleardevice();
printf("Rectangle before scaling:\n");
rectangle(x1,y1,x2,y2);
getch();
cleardevice();
printf("Rectangle after scaling:");

rectangle(x1,y1,x2*x,y2*y);
getch();main();
}
case 4:
{
int x1=50,y1=150,y3=150,x2=75,y2=125,x3=100,xt;
xt=abs(y2-y1);
cleardevice();
printf("Triangle before reflection");
line(x1,y1,x2,y2);
line(x1,y1,x3,y3);
line(x2,y2,x3,y3);
getch();
cleardevice();
printf("Triangle after reflection");
line(x1+10,y1+10,x2+10,(y2+(2*xt)+10));
line(x1+10,y1+10,x3+10,y3+10);
line(x2+10,(y2+(2*xt)+10),x3+10,y3+10);
getch();main();
}
case 5:
{
int x1=100,x2=100,y1=100,y4=100,y2=30,y3=30,x3=170,x4=170,x=5;
cleardevice();
printf("Rectangle before shearing");
line(x1,y1,x2,y2);
line(x1,y1,x4,y4);

line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
getch();
cleardevice();
printf("Rectangle after shearing");
line(x1,y1,x2+x,y2);
line(x1,y1,x4,y4);
line(x2+x,y2,x3+x,y3);
line(x3+x,y3,x4,y4);
getch();main();
}
}
cleardevice();
closegraph();
}

OUTPUT

Ex-4(a)
COMPOSITE TWO DIMENSIONAL TRANFORMATION (SCALING)
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int pts[3][3],x,r,yr,teta,i,j,k;
double theta;
char ch;
double a1[3][3]={1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0};
double b1[3][1]={0.0,0.0,1.0};
double ans1[3][1]={0.0,0.0,1.0};
double ans2[3][1]={0.0,0.0,1.0};
double ans3[3][1]={0.0,0.0,1.0};
int x1,y1,x2,y2,x3,y3;
int sx1,sx2,sy1,sy2;
void mul(double a[3][3],double b[3][1],double c[3][1]);
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
do
{

printf("Enter value for triangle");


for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&pts[i][j]);}
line(pts[0][0],pts[0][1],pts[1][0],pts[1][1]);
line(pts[1][0],pts[1][1],pts[2][0],pts[2][1]);
line(pts[2][0],pts[2][1],pts[0][0],pts[0][1]);
printf("Enter sx1,sx2,sy1,sy2");
scanf("%d%d%d%d",&sx1,&sx2,&sy1,&sy2);
a1[0][0]=sx1*sx2;
a1[1][1]=sy1*sy2;
b1[0][0]=pts[0][0];
b1[1][1]=pts[0][1];
mul(a1,b1,ans1);
x1=ceil(ans1[0][0]);
y1=ceil(ans1[1][0]);
b1[0][0]=pts[1][0];
b1[1][0]=pts[1][0];
mul(a1,b1,ans2);
x2=ceil(ans2[0][0]);
y2=ceil(ans2[1][0]);
b1[0][0]=pts[2][0];
b1[1][0]=pts[2][1];
mul(a1,b1,ans3);
x3=ceil(ans3[0][0]);
y3=ceil(ans3[1][0]);

line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
while(ch=='y');
getch();
//closegraph();
return 0;
}
void mul(double a[3][3],double b[3][1],double c[3][1])
{
for(i=0;i<3;i++){
for(j=0;j<1;j++){
c[i][j]=0;
for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];}}}}
OUTPUT

Ex-4(b)
TWO DIMENSIONAL COMPOSITE TRANSFORMATION
SCALING,ROTATION,TRANSLATION
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int pts[3][3],xr,yr,teta,i,j,k;
double theta;
char ch;
double a1[3][3]={1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0};
double b1[3][1]={0.0,0.0,1.0};
double ans1[3][1]={0.0,0.0,1.0};
double ans2[3][1]={0.0,0.0,1.0};
double ans3[3][1]={0.0,0.0,1.0};
int x1,y1,x2,y2,x3,y3;
int tx,ty,sx,sy;
void mul(double a[3][3],double b[3][1],double c[3][1]);
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
do

{
printf("Enter the coordinate value for triangle");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&pts[i][j]);}
line(pts[0][0],pts[0][1],pts[1][0],pts[1][1]);
line(pts[1][0],pts[1][1],pts[2][0],pts[2][1]);
line(pts[2][0],pts[2][1],pts[0][0],pts[0][1]);
flushall();
printf("Enter the value of scaling factor sx,sy:\n");
scanf("%d%d",&sx,&sy);
printf("Enter the center of triangle xr,yr\n");
scanf("%d%d",&xr,&yr);
printf("Enter the theta angle:\n");
scanf("%d",&theta);
printf("Enter the translation factors:\n");
scanf("%d%d",&tx,&ty);
theta=teta*3.14/180;
a1[0][0]=sx*cos(theta);
a1[0][1]=sy*sin(theta);
a1[0][2]=((1-sx*cos(theta))*xr)-(yr*sy*sin(theta))+tx;
a1[1][0]=sx*sin(theta);
a1[1][1]=sy*cos(theta);
a1[1][2]=((1-sy*cos(theta))*yr)-(xr*sx*sin(theta))+ty;
b1[0][0]=pts[0][0];
b1[1][0]=pts[0][1];

mul(a1,b1,ans1);
x1=ceil(ans1[0][0]);
y1=ceil(ans1[1][0]);
b1[0][0]=pts[1][0];
b1[1][0]=pts[1][1];
mul(a1,b1,ans2);
x2=ceil(ans2[0][0]);
y2=ceil(ans2[1][0]);
b1[0][0]=pts[2][0];
b1[1][0]=pts[2][1];
mul(a1,b1,ans3);
x3=ceil(ans3[0][0]);
y3=ceil(ans3[1][0]);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
while(ch=='y');
getch();
//closegraph();
return 0;
}
void mul(double a[3][3],double b[3][1],double c[3][1])
{
for(i=0;i<3;i++){
for(j=0;j<1;j++){
c[i][j]=0;

for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}}}
OUTPUT

EX-4(c)
TWO DIMENSIONAL COMPOSITE TRANFORMATION PIVOT POINT
ROTATION
#include<stdio.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int pts[3][2],xr,yr,teta,i,j,k,op,shx,pts1[4][2];
int b[3][1]={0,0,1};
double theta;
char ch;
double a1[3][3]={1.0,0.0,0.0,
0.0,1.0,0.0,
0.0,0.0,1.0};
double b1[3][1]={0.0,0.0,1.0};
double ans1[3][1]={0.0,0.0,1.0};
double ans2[3][1]={0.0,0.0,1.0};
double ans3[3][1]={0.0,0.0,1.0};
int x1,y1,x2,y2,x3,y3;
void mul(double a[3][3],double b[3][1],double c[3][1]);
int main(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
do

{
printf("Enter the coordinate value for triangle \n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&pts[i][j]);}
line(pts[0][0],pts[0][1],pts[1][0],pts[1][1]);
line(pts[1][0],pts[1][1],pts[2][0],pts[2][1]);
line(pts[2][0],pts[2][1],pts[0][0],pts[0][1]);
printf("Enter the center of triangle xr,yr\n");
scanf("%d%d",&xr,&yr);
printf("Enter the theta angle:\n");
scanf("%d",&teta);
theta=teta*3.14/180;
a1[0][0]=cos(theta);
a1[0][1]=-sin(theta);
a1[0][2]=((1-cos(theta))*xr)+(yr*sin(theta));
a1[1][0]=sin(theta);
a1[1][1]=cos(theta);
a1[1][2]=((1-cos(theta))*yr)-(xr*sin(theta));
b1[0][0]=pts[0][0];
b1[1][0]=pts[0][1];
mul(a1,b1,ans1);
x1=ceil(ans1[0][0]);
y1=ceil(ans1[1][0]);
b1[0][0]=pts[1][0];
b1[1][0]=pts[1][1];

mul(a1,b1,ans2);
x2=ceil(ans2[0][0]);
y2=ceil(ans2[1][0]);
b1[0][0]=pts[2][0];
b1[1][0]=pts[2][1];
mul(a1,b1,ans3);
x3=ceil(ans3[0][0]);
y3=ceil(ans3[1][0]);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
}
while(ch=='y');
getch();
//closegraph();
return 0;
}
void mul(double a[3][3],double b[3][1],double c[3][1])
{
for(i=0;i<3;i++){
for(j=0;j<1;j++){
c[i][j]=0;
for(k=0;k<3;k++){
c[i][j]+=a[i][k]*b[k][j];
}
}}}

OUTPUT

5a
COHEN SUTHERLAND LINE CLIPPING

#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;
}
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);
}
main()
{
float x2,y2,x1,y1,xwmin,ywmin,xwmax,ywmax;
int gd=DETECT,gm;
clrscr();
initgraph(&gd,&gm,"C:\turboc3\bin");
printf("\n\nEnter the coodinate of line:");
printf("\n\nx1,y1 :");
scanf("%f%f",&x1,&y1);
printf("\n\nx2,y2 :");
scanf("%f%f",&x2,&y2);
printf("\nEnter the coordinates of window");
printf("\nxwmin,xwmax,ywmin,ywmax :");
scanf("%f%f%f%f",&xwmin,&ywmin,&xwmax,&ywmax);
clrscr();
line(x1,y1,x2,y2);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch();
clrscr();
lineclip(x1,y1,x2,y2,xwmin,ywmin,xwmax,ywmax);

getch();
closegraph();
return;
}

OUTPUT:

5BWINDOW TO VIEWPOINT MAPPING CLIPPING

#include<stdio.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
main()
{
float sx,sy;
int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
int gd=DETECT,gm;
initgraph(&gd,&gm,"f:\tc\bin");
printf("\nEnter the co-ords \n");

scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
cleardevice();
w1=5;
w2=5;w3=635;w4=465;
rectangle(w1,w2,w3,w4);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
v1=425;v2=75;v3=550;v4=250;
sx=(float)(v3-v1)/(w3-w1);
sy=(float)(v4-v2)/(w4-w2);
rectangle(v1,v2,v3,v4);
x1=v1+floor(((float)(x1-w1)*sx)+0.5);
x2=v1+floor(((float)(x2-w1)*sx)+0.5);
x3=v1+floor(((float)(x3-w1)*sx)+0.5);
y1=v2+floor(((float)(y1-w1)*sy)+0.5);
y2=v2+floor(((float)(y2-w1)*sy)+0.5);
y3=v2+floor(((float)(y3-w1)*sy)+0.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return;
}

OUTPUT:

Ex-6
THREE DIMENSIONAL TRANSFORMATION

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.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,y1,x2,y2;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
setfillstyle(o,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 the transformation 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 the 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+(x+50),midy-(y-100),midx+(x+60),midy-(y-90),5,1);
printf("Enter rotation angle");
scanf("%d",&o);
x1=50*cos(0*3.14/180)-100*sin(0*3.14/180);
y1=50*cos(0*3.14/180)+100*sin(0*3.14/180);
x2=50*cos(0*3.14/180)-90*sin(0*3.14/180);
y2=50*cos(0*3.14/180)+90*sin(0*3.14/180);
axis();
printf("After rotation about axis");
bar3d(midx+x1,midy+y1,midx+x2,midy+y2,5,1);
axis();
printf("After rotation about x-axis");
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);

axis();
printf("After rotation about y-axis");
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
}

OUTPUT

Ex-7
SUTHERLAND HOGEMAN POLYGON CLIPPING
#include<stdio.h>
#include<graphics.h>
#include<math.h>
typedef struct
{
float x,y;
}PT;
int n;
main()
{
int i,j,gd,gm;
PT d,p1,p2,p11,p12,pp[20],p[20];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c\\tc\bin");
printf("enter the code(left,top)of point1:");
scanf("%f%f",&p1.x,&p1.y);
printf("enter the code(right,bottom)of point2:");
scanf("%f%f",&p2.x,&p2.y);
printf("enter the no of vertex");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the cord of vertex %d:",i+1);
scanf("%f%f",&p[i].x,&p[i].y);
}
p[i].x=p[0].x;
p[i].y=p[0].y;
cleardevice();
drawpolygon(p,n);
rectangle(p1.x,p1.y,p2.x,p2.y);
getch();
left(p1,p,pp);
right(p2,p,pp);
top(p1,p,pp);
bottom(p2,p,pp);
cleardevice();
rectangle(p1.x,p1.y,p2.x,p2.y);
setcolor(3);
drawpolygon(p,n);
getch();
closegraph();
return 0;
}
left(PT p1,PT p[20],PT pp[20])
{
int x,i,j=0;
for(i=0;i<n;i++)

{
if(p[i].x<p1.x&&p[i+1].x>=p1.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y=p[j].y;
}
pp[j].x=p1.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x>p1.x&&p[i+1].x>=p1.x)
{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}
if(p[i].x>p1.x&&p[i+1].x<=p1.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[i].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p1.x-p[i].x)+p[i].y;
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p1.x;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
return;
}
right(PT p2,PT p[20],PT pp[20])
{
int i,j=0;

for(i=0;i<n;i++)
{
if(p[i].x>p2.x&&p[i+1].x<=p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].x<p2.x&&p[i+1].x<=p2.x)
{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}
if(p[i].x<p2.x&&p[i+1].x>=p2.x)
{
if(p[i+1].x-p[i].x!=0)
{
pp[j].y=(p[i+1].y-p[i].y)/(p[i+1].x-p[i].x)*(p2.x-p[i].x)+p[i].y;
}
else
{
pp[j].y=p[i].y;
}
pp[j].x=p2.x;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
return;
}
top(PT p1,PT p[20],PT pp[20])
{

int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y<p1.x&&p[i+1].y>=p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p1.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y>p1.y&&p[i+1].y>=p1.y)
{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}
if(p[i].y>p1.y&&p[i+1].y<=p1.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p1.y-p[i].y)+p[i].x;
}
else
{
pp[j].x=p[j].x;
}
pp[j].y=p1.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
return;
}
bottom(PT p2,PT p[20],PT pp[20])

{
int i,j=0;
for(i=0;i<n;i++)
{
if(p[i].y>p2.x&&p[i+1].y<=p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x=p[j].x;
}
pp[j].y=p2.y;
j++;
pp[j].x=p[i+1].x;
pp[j].y=p[i+1].y;
j++;
}
if(p[i].y<p2.y&&p[i+1].y<p2.y)
{
pp[j].y=p[i+1].y;
pp[j].x=p[i+1].x;
j++;
}
if(p[i].y<p2.y&&p[i+1].y>=p2.y)
{
if(p[i+1].y-p[i].y!=0)
{
pp[j].x=(p[i+1].x-p[i].x)/(p[i+1].y-p[i].y)*(p2.y-p[i].y)+p[i].x;
}
else
{
pp[j].x=p[i].x;
}
pp[j].y=p2.y;
j++;
}
}
for(i=0;i<j;i++)
{
p[i].x=pp[i].x;
p[i].y=pp[i].y;
}
p[i].x=pp[0].x;
p[i].y=pp[0].y;
n=j;
return;
}

drawpolygon(PT x[20],int n)
{
int i;
for(i=0;i<n-1;i++)
{
line(x[i].x,x[i].y,x[i+1].x,x[i+1].y);
}
line(x[i].x,x[i].y,x[0].x,x[0].y);
return;
}
OUTPUT

Ex-8
3D COMPOSITE TRANSFORMATION
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
typedef float matrix[3][3];
typedef struct
{
float x,y;
}wept;
matrix tm;
void midentity(matrix m)
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
m[i][j]=(i==j);
}
void mpremul(matrix a,matrix b)
{
int r,c;
matrix temp;
for(r=0;r<3;r++)
for(c=0;c<3;c++)
temp[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]=temp[r][c];
}
void translate(int tx,int ty)
{
matrix m;
midentity(m);
m[0][2]=tx;
m[1][2]=ty;
mpremul(m,tm);
}
void scale(float sx,float sy,wept refpt)
{
matrix m;
midentity(m);
m[0][0]=sx;
m[0][2]=(1-sx)*refpt.x;
m[1][1]=sy;
m[1][2]=(1-sy)*refpt.y;
mpremul(m,tm);

}
void rotate(float a,wept refpt)
{
matrix m;
a=(a*3.14)/180;
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.y*sin(a);
mpremul(m,tm);
}
void transformpoints(int npts,wept *pts)
{
int k;
float temp;
for(k=0;k<npts;k++)
{
temp=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=temp;
}
}
void ptstotpts(wept *pts,int tpts[6])
{
int i,j=0;
transformpoints(2,pts);
for(i=0;i<2;i++)
{
tpts[j]=pts[i].x;
tpts[j+1]=pts[i].y;
j=j+2;
}
bar3d(tpts[0],tpts[1],tpts[2],tpts[3],10,1);
}
void main()
{
wept pts[3]={250.0,250.0,200.0,200.0};
int tpts[6]={50.0,50.0,200.0,200.0};
wept refpt={375.0,375.0};
int gd=DETECT,gm,i,j,k,temp;
initgraph(&gd,&gm,"E:\tc\bin");
setbkcolor(WHITE);
setbkcolor(BLUE);
bar3d(150,150,200,200,20,1);
midentity(tm);
sleep(4);
setfillstyle(3,5);
scale(0.5,0.5,refpt);

ptstotpts(pts,tpts);
outtextxy(250,330,"After scaling");
sleep(2);
midentity(tm);
sleep(2);
rotate(60.0,refpt);
setfillstyle(3,5);
ptstotpts(pts,tpts);
outtextxy(350,300,"After Rotation");
sleep(2);
midentity(tm);
sleep(2);
translate(0,150);
ptstotpts(pts,tpts);
outtextxy(400,450,"After Translation");
getch();}
OUTPUT:

Ex-9b
3 DIMENSIONAL IMAGE PROJECTION
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
int gd=DETECT,gm;
double x1,x2,y1,y2;
void drawcube(double edge[20][3])
{
int i;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clearviewport();
for(i=0;i<19;i++)
{
x1=edge[i][0]+edge[i][2]*(cos(2.3562));
y1=edge[i][1]-edge[i][2]*(sin(2.3562));
x2=edge[i+1][0]+edge[i+1][2]*(cos(2.3562));
y2=edge[i+1][1]-edge[i+1][2]*(sin(2.3562));
line(x1+320,240-y1,x2+320,240-y2);
}
line(320,240,320,25);
line(320,240,550,240);
line(320,240,150,410);
getch();
closegraph();

}
void perspect(double edge[20][3])
{
int ch;
int i;
float p,q,r;
clrscr();
printf("\n-=[perspective projection about]=-");
printf("\n 1:==>x-axis");
printf("\n 2:==>y-axis");
printf("\n 3:==>z-0axis");
printf("\n enter your nchoice :=");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter p:=");
scanf("%f",&p);
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0]/(p*edge[i][0]+1);
edge[i][1]=edge[i][1]/(p*edge[i][0]+1);
edge[i][2]=edge[i][2]/(p*edge[i][0]+1);
}
drawcube(edge);
break;
case 2:

printf("\n enter q:=");


scanf("%f",&q);
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1]/(edge[i][1]*q+1);
edge[i][0]=edge[i][0]/(edge[i][1]*q+1);
edge[i][2]=edge[i][2]/(edge[i][1]*q+1);
}
drawcube(edge);
break;
case 3:
printf("\n enter R:=");
scanf("%f",&r);
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2]/(edge[i][2]*r+1);
edge[i][0]=edge[i][0]/(edge[i][2]*r+1);
edge[i][1]=edge[i][1]/(edge[i][2]*r+1);
}
drawcube(edge);
break;
}
closegraph();
}
void main()
{
int choice;

double edge[20]
[3]={100,0,0,100,100,0,0,100,0,0,100,100,0,0,100,0,0,0,100,0,0,100,0,100,100,75,100,75,10
0,100,100,100,75,100,100,0,100,100,75,100,75,100,75,100,100,0,100,100,0,100,0,0,0,0,0,0,1
00,100,0,100};
clrscr();
drawcube(edge);
perspect(edge);
closegraph();
}
OUTPUT

Ex-10a
COLOR MODEL CONVERSION-RGB to HSV
#include<math.h>
#include<stdio.h>
#define MIN(a,b)(a<b?a:b)
#define MAX(a,b)(a>b)?a:b)
#define NO_HUE-1
void rgbtohsv(float r,float g,float b,float *h,float *s,float *v)
{
float max=MAX(r,MAX(g,b)),min=MIN(r,MIN(g,b));
float delta=max-min;
*v=max;
if(max!=0.0)
*s=delta/max;
else
*s=0.0;
if(*s==0.0)*h=NO_HUE;
else
{
if(r==max)
*h=(g-b)/delta;
else if(g==max)
*h=2+(b-r)/delta;
else if(b==max)
*h=4+(r-g)/delta;
*h*=60.0;
if(*h<0)*h+=360.0;

*h/=360.0;
}
printf("h=%fs=%fv=%f",*h,*s,*v);
}
void amin()
{
float a,b,c,d,e,f;
clrscr();
printf("enter rgb and sv value");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
rgbtohsv(a,b,c,&d,&e,&f);
getch();
}
OUTPUT

EX 9-b
COLOR MODEL CONVERSION-HSV to RGB
#include<stdio.h>
#include<conio.h>
#include<math.h>
void hsvtorgb(float h,float s,float v,float *r,float *g,float *b)
{
int i;
float aa,bb,cc,f;
if(s==0)
*r=*g=*b=v;
else
{
if(h==1.0)h=0;
h*=6.0;
i=floor(h);
f=h-i;
aa=v*(1-s);
bb=v*(1-(s*f));
cc=v*(1-(s*(1-f)));
switch(i)
{
case 0:*r=v;*g=cc;*b=aa;
printf("R=%f G=%f B=%f",*r,*g,*b);
break;
case 1:*r=bb;*g=v;*b=aa;
printf("R=%f G=%f B=%f",*r,*g,*b);

break;
case 2:*r=aa;*g=v;*b=cc;
printf("R=%f G=%f B=%f",*r,*g,*b);
break;
case 3:*r=aa;*g=bb;*b=v;
printf("R=%f G=%f B=%f",*r,*g,*b);
break;
case 4:*r=cc;*g=aa;*b=v;
printf("R=%f G=%f B=%f",*r,*g,*b);
break;
case 5:*r=v;*g=aa;*b=bb;
printf("R=%f G=%f B=%f",*r,*g,*b);
break;
}
}
}
void main()
{
float a,b,c,d,e,f;
clrscr();
printf("\nEnter hsv to rgb values = ");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&f);
hsvtorgb(a,b,c,&d,&e,&f);
getch();
}

OUTPUT

Ex-11-a
3D SCENES
#include<windows.h>
#include<GL/gl.h>
#include<GL/glu.h>
#include<GL/glut.h>

void axis(double length)


{

glPushMatrix();
glBegin(GL_LINES);
glVertex3d(0,0,0);
glVertex3d(0,0,length);
glEnd();
glTranslated(0,0,length-0.2);
glutWireCone(0.04,0.2,12,9);
glPopMatrix();

void displayWire(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0*64/48.0,2.0*64/48.0,-2.0,2.0,0.1,100);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();
gluLookAt(2.0,2.0,2.0,0.0,0.0,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(0,0,0);
axis(0.5);
glPushMatrix();
glRotated(90,0,1.0,0);
axis(0.5);
glPopMatrix();
glPushMatrix();
glTranslated(0.5,0.5,0.5);
glutWireCube(1.0);
glPopMatrix();
glPushMatrix();
glTranslated(1.0,1.0,0);
glutWireSphere(0.25,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1.0,0,1.0);
glutWireCone(0.2,0.5,10,8);
glPopMatrix();
glPushMatrix();
glTranslated(1,1,1);
glutWireTeapot(0.2);
glPopMatrix();
glPushMatrix();
glTranslated(0,1.0,0);

glRotated(90.0,1,0,0);
glutWireTorus(0.1,0.5,10,10);
glPopMatrix();
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();
glFlush();
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("transformation test-wireframes");
glutDisplayFunc(displayWire);
glClearColor(1.0f,1.0f,1.0f,0.0f);
glViewport(0,0,640,480);
glutMainLoop();
}

OUTPUT

EX 11-b
3D OBJECTS
#include<windows.h>
#include<gl/GL.h>
#include<gl/Glu.h>
#include<gl/glut.h>
#include<stdio.h>
GLfloat angle=0.0;
GLfloat dlr=1.0;
GLfloat dlg=1.0;
GLfloat dlb=1.0;
GLfloat alr=1.0;
GLfloat alg=1.0;
GLfloat alb=1.0;
GLfloat lx=0.0;
GLfloat ly=0.0;
GLfloat lz=1.0;
GLfloat lw=0.0;
void cube(void)
{
glRotatef(angle,1.0,0.0,0.0);
glRotatef(angle,0.0,1.0,0.0);
glRotatef(angle,1.0,0.0,1.0);
glutSolidCube(2);
}
void init(void)
{

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glShadeModel(GL_SMOOTH);
}
void display(void)
{
glClearColor(0.0,0.0,0.0,1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLfloat DiffuseLight[]={dlr,dlg,dlb};
GLfloat AmbientLight[]={alr,alg,alb};
glLightfv(GL_LIGHT0,GL_DIFFUSE,DiffuseLight);
glLightfv(GL_LIGHT1,GL_AMBIENT,AmbientLight);
GLfloat LightPosition[]={lx,ly,lz,lw};
glLightfv(GL_LIGHT0,GL_POSITION,LightPosition);
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);
cube();
glutSwapBuffers();
angle++;
}
void reshape(int w,int h)
{
glViewport(0,0,(GLsizei)w,(GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(60,(GLfloat)w/(GLfloat)h,1.0,100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key,int x,int y)
{
if(key=='r')
{
dlr=1.0;
dlg=0.0;
dlb=0.0;
}
if(key=='g')
{
dlr=0.0;
dlg=1.0;
dlb=0.0;
}
if(key=='b')
{
dlr=0.0;
dlg=0.0;
dlb=1.0;
}
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("A basic open GL window");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
}

OUTPUT

You might also like