PROGRAM:/* Implementation of Bresenhams Algorithm – Line, Circle, Ellipse*/
#include
#include
#include
#include
void line()
{
float x,y,x1,y1,x2,y2,dx,dy,e;
int i,gd,gm;
printf("Enter the value of x1 y1");
scanf("%f %f",&x1,&y1); printf("Enter the value of x2 y2"); scanf("%f %f",&x2,&y2); dx=abs(x2-x1);
dy=abs(y2-y1);
x=x1;
y=y1; e=2*(dy-dx); i=1;
do
{ putpixel(x,y,30); while(e>=0)
{
y=y+1;
e=e-2*dx;
}
x=x+1; e=e+2*dy; i=i+1;
}while(i<=dx);
}
void circle()
{
float d;
int x,y,z,gd,gm;
int r;
printf("Enter the radius of circle :");
scanf("%d",&r);
x=0;
y=r;
do
{
putpixel(200+x,200+y,18);
putpixel(200+y,200+x,18);
putpixel(200+x,200-y,18); putpixel(200-x,200-y,20); putpixel(200-y,200-x,18); putpixel(200-y,200+x,20); putpixel(200-x,200+y,18); putpixel(200-x,200+y,20); if(d<=0)
{
d=d+4*x+6;
}
else
{
d=d+4*(x-y)+10;
y=y-1;
}
x=x+1;
delay(100);
}while(x
{
x=x;
y=y-1;
dy=dy-tworxsq;
d2=d2+dy+rysq;
}
else
{
x=x+1;
y=y-1;
dy=dy-tworxsq;
dx=dx+tworysq;
d2=d2+dx-dy+rxsq;
}
}while(y>0);
}
void main()
{
clrscr();
int gd,gm; detectgraph(&gd,&gm); initgraph(&gd,&gm," "); while(ch=='y')
{cleardevice();
setbkcolor(9);
outtextxy(100,150,"Enter 1 to get line");
outtextxy(100,170,"2.Circle");
outtextxy(100,190,"3. Ellipse”); outtextxy(100,270,"4.Exit"); ch=getch();
cleardevice();
switch(ch)
{case '1':
line1();
break;
case '2': circle1(); break;
case '3':
ellipse1();
break;
case '4': closegraph(); return;
}ch='y';
getch();
}}
#include
#include
#include
#include
void main()
{
char ch='y';
int gd=DETECT,gm,x1,y1,x2,y2,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,"");
while(ch=='y')
{cleardevice();
setbkcolor(9);
outtextxy(100,150,"Enter 1 to get line"); outtextxy(100,170,"2.Circle"); outtextxy(100,190,"3.Box"); outtextxy(100,210,"4.Arc"); outtextxy(100,230,"5.Ellipse"); outtextxy(100,250,"6.Rectangle"); outtextxy(100,270,"7.Exit");
ch=getch();
cleardevice();
switch(ch)
{case '1': line(100,200,300,400); break;
case '2':
circle(200,200,100);
break;
case '3': setfillstyle(5,4); bar(100,300,200,100); break;
case '4': setfillstyle(5,4); arc(200,200,100,300,100); break;
case '5': setfillstyle(5,4); fillellipse(100,100,50,100); break;
case '6':
settextstyle(DEFAULT_FONT,0,2);
outtextxy(120,140,"siddiq "); 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();
}}
#include
#include
#include
#include
#include
#include
void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"..\\bgi");
}
void main()
{
int p,q,i,xp,yp,temp[100],gd=DETECT,gm;
float tx,ty,x;
int n;
int ch,a[100],b[100],j=0,cw=-1;
char ch1;
init();
printf("enter number of sides:");
scanf("%d",&n);
for(i=0;i
#include
#include
#include
#include
#include
#include
typedef int reg[4];
void concode(int,int,reg);
int acccode(reg,reg); int regcode(reg,reg); int points(reg);
int swap(int*,int*,int*,int*,reg,reg);
int fin=0,pr=0; int lx,ly,rx,ry; reg a,b;
void main()
{
int gd=DETECT,gm;
int x,y,x1,y1;
float mid;
char s[10],s1[10],s2[10],s3[10];
initgraph(&gd,&gm,"..\\bgi");
printf("\n enter the co-ordinates for rec:"); scanf("%d%d%d%d",&lx,&ly,&rx,&ry); printf("enter the co-ordinates for line"); scanf("%d%d%d%d",&x,&y,&x1,&y1); cleardevice();
setbkcolor(8);
outtextxy(10,10,"original line");
setcolor(BLUE);
line(x,y,x1,y1); setcolor(YELLOW); rectangle(lx,ly,rx,ry); getch();
while(!fin)
{
concode(x,y,a); concode(x1,y1,b); if(acccode(a,b))
else if(regcode(a,b))
{
pr=1;
fin=1;
}
{
fin=1;
}
else
{
if(points(a))
{
swap(&lx,&ly,&x1,&y1,a,b);
}
mid=(float)(y1-y)/(x1-x);
if(a[0])
{
y=y+mid*(lx-x);
x=lx;
}
else if(a[1])
{
y=y+mid*(rx-x);
x=rx;
}
if(a[2])
{
x=x+(ly-y)/mid;
y=ly;
}
else if(a[3])
{
x=x+(ry-y)/mid;
y=ry;
}
}
}
if(pr)
{
cleardevice(); setbkcolor(8); setcolor(GREEN); line(x,y,x1,y1); setcolor(RED); rectangle(lx,ly,rx,ry);
}
else
{
cleardevice();
setbkcolor(8);
outtextxy(10,10,"line not inside clipping window");
rectangle(lx,ly,rx,ry);
}
getch(); closegraph(); exit(0);
}
void concode(int x,int y,reg code)
{
if(x
code[1]=1;
else
code[1]=0;
if(y
code[3]=1;
else
}
code[3]=0;
int acccode(reg x,reg y)
{
int i,ok=1;
for(i=0;i<4;i++)
{
if(x[i]||y[i])
ok=0;
}
return(ok);
}
int regcode(reg x,reg y)
{
int i,ok=0;
for(i=0;i<4;i++)
{
if(x[i]&&y[i])
ok=1;
}
return(ok);
}
int swap(int *x1,int *y1,int *x2,int *y2, reg a,reg b)
{
int tm,tm1;
reg c;
if(!a[0]||a[1]||a[2]||a[3])
{
tm=*x1;
*x1=*x2;
*x2=tm;
tm1=*y1;
*y1=*y2;
*y2=tm1; c[0]=a[0];a[0]=b[0];b[0]=c[0]; c[1]=a[1];a[1]=b[1];b[1]=c[1]; c[3]=a[3];a[3]=b[3];b[3]=c[3];
}
return 1;
}
int points(reg e)
{
int points;
if(e[0]||e[1]||e[2]||e[3])
{
points=0;
}
else
{
points=1;
}
return(points);
}
PROGRAM:/* Sutherland – Hodgeman Polygon clipping Algorithm */
#include
#include
#include
#include
#include
#include
int code(int,int);
int xl=100,yb=200,xr=200,yt=100,xi,yi,c;
void main()
{
int visible(int,int,int);
int gd=DETECT,gm,n,i,m=0,c1,c2,ct=0,v,e,s=0,t;
int nx[20],ny[20],x1,x2,y1,y2,x[20],y[20]; detectgraph(&gd,&gm); initgraph(&gd,&gm,"..\\bgi");
printf("\n Enter the no of vertices:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n Enter the polygon co-ordinates:");
scanf("%d%d",&x[i],&y[i]);
}
n++; x[n]=x[1]; y[n]=y[1];
printf("Enter the window co-ordinates:");
scanf("%d%d%d%d",&xl,&yb,&xr,&yt);
cleardevice();
rectangle(xl,yb,xr,yt); moveto(x[1],y[1]); for(i=1;i<=n;i++) lineto(x[i],y[i]); getch();
cleardevice();
do
{
printf("eter do:"); m=0; for(i=1;i<=n;i++)
{
if((i==1)&&(ct==0))
{
printf("enter goto:");
goto pro;
}
else
{
c2=code(x[i],y[i]); c=(int)pow((float)2,(float)t); if(((c1&c)||(c2&c))!=0)
{
x1=x[s]; x2=x[i]; y1=y[s]; y2=y[i]; e=ct; if(e==0)
{
xi=xl;
yi=y2-(float)(y2-y1)/(x2-x1)*(x2-xl);
}
if(e==1)
{
xi=xr;
yi=y2-(float)(y2-y1)/(x2-x1)*(x2-xr);
}
if(e==2)
{
yi=yt;
xi=x2-(float)(x2-x1)/(y2-y1)*(y2-yt);
}
if(e==3)
{
yi=yb;
xi=x2-(float)(x2-x1)/(y2-y1)*(y2-yb);
}
m++; nx[m]=xi; ny[m]=yi;
}
pro: s=i; c1=code(x[i],y[i]);
v=visible(x[i],y[i],ct);
if(v==1)
{
m++; nx[m]=x[i]; ny[m]=y[i];
}} }
ct++;
n=m;
for(i=1;i<=n;i++)
{
x[i]=nx[i];
y[i]=ny[i];
}
}while(ct<4); i=1; moveto(x[i],y[i]); for(i=2;i<=n;i++) lineto(x[i],y[i]); rec(xl,yb,xr,yt); getch(); closegraph();
}
int code(int a,int b)
{ c=0; if(a
c+=1;
c+=2;
if(b
#include
#include
#include
#include
#include
int x,y,z,s,ds;
int ch,tx,ty,tz,i,m=9;
int sx,sy,sz;
int cho,i,n; float theta; int xx,yy,zz;
struct cube
{
int x1,y1,z1;
};
struct cube1
{
int x2,y2,z2;
};
struct cube a[8]={0},t[8]={0},f[8]={0},g[8]={0},k[8]={0},l[8]={0}; struct cube1 b[8]={0},c[8]={0},d[8]={0},e[8]={0},h[8]={0},j[8]={0}; void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
}
void create();
void drawcube(struct cube1 v[]);
void rotate();
void main()
{
init(); clrscr(); do{ flushall();
gotoxy(1,1);
cleardevice();
printf("\n 1.create\n2.Translation \n 3.Scaling\n 4.Rotation\n5.Exit\n\nEnter your choice:");
scanf("%d",&ch);
setcolor(m++);
switch(ch)
{
case 1:
cleardevice();
init();
create(); getch(); break;
case 2:
cleardevice(); init(); flushall();
printf("\n Enter the translation vector:");
scanf("%d%d%d",&tx,&ty,&tz);
drawcube(b);
for(i=0;i<8;i++)
{
t[i].x1=a[i].x1+tx; t[i].y1=a[i].y1+ty; t[i].z1=a[i].z1+tz;
}
for(i=0;i<8;i++)
{
c[i].x2=t[i].x1+t[i].z1/2;
c[i].y2=t[i].y1+t[i].z1/2;
}
drawcube(c);
break;
case 3:
cleardevice(); init(); flushall();
printf("\n Enter the scaling vector :");
scanf("%d%d%d",&sx,&sy,&sz);
drawcube(b);
for(i=0;i<8;i++)
{
f[i].x1=a[i].x1*sx; f[i].y1=a[i].y1*sy; f[i].z1=a[i].z1*sz;
}
for(i=0;i<8;i++)
{
d[i].x2=f[i].x1+f[i].z1/2;
d[i].y2=f[i].y1+f[i].z1/2;
}
drawcube(d);
break;
case 4:
cleardevice();
init();
rotate();
break;
case 5: closegraph(); exit(0);
default:
continue;
}
}while(1);
}
void drawcube(struct cube1 v[])
{
gotoxy(3,4); line(v[0].x2,v[0].y2,v[1].x2,v[1].y2); line(v[1].x2,v[1].y2,v[2].x2,v[2].y2); line(v[2].x2,v[2].y2,v[3].x2,v[3].y2); line(v[3].x2,v[3].y2,v[0].x2,v[0].y2); line(v[4].x2,v[4].y2,v[5].x2,v[5].y2); line(v[5].x2,v[5].y2,v[6].x2,v[6].y2); line(v[6].x2,v[6].y2,v[7].x2,v[7].y2); line(v[7].x2,v[7].y2,v[7].x2,v[7].y2); line(v[0].x2,v[0].y2,v[4].x2,v[4].y2); line(v[1].x2,v[1].y2,v[5].x2,v[5].y2); line(v[2].x2,v[2].y2,v[6].x2,v[6].y2); line(v[3].x2,v[3].y2,v[7].x2,v[7].y2); line(v[4].x2,v[4].y2,v[7].x2,v[7].y2); getch();
}
void create()
{
int d,i;
flushall();
printf("\n Enter the cube co-ordinate:"); scanf("%d%d%d",&x,&y,&z); printf("\nEnter the side of the cube:"); scanf("%d",&d);
cleardevice(); gotoxy(1,1); settextstyle(1,1,5); outtextxy(10,30,"cube");
a[0].x1=x; a[0].y1=y; a[0].z1=z; a[1].x1=x+d; a[1].y1=y; a[1].z1=z; a[2].x1=x+d; a[2].y1=y+d; a[2].z1=z; a[3].x1=x;a[3].y1=y+d;a[3].z1=z;
a[4].x1=x;a[4].y1=y;a[4].z1=z-d; a[5].x1=x+d;a[5].y1=y;a[5].z1=z-d; a[6].x1=x+d;a[6].y1=y+d;a[6].z1=z-d; a[7].x1=x;a[7].y1=y+d;a[7].z1=z-d; for(i=0;i<8;i++)
{
b[i].x2=a[i].x1+a[i].z1/2;
b[i].y2=a[i].y1+a[i].z1/2;
}
cleardevice(); drawcube(b); getch();
}
void rotate()
{ cleardevice(); clrscr();
printf("\n Rotation with respect to axis:");
printf("\n 1.x-axis\n 2.y-axis\n 3.z-axis"); printf("\n Enter ur choice"); scanf("%d",&cho);
printf("\n Enter the rotation angle:");
scanf("%f",&theta); theta=(theta * 180/3.14); switch(cho)
{
case 1: cleardevice(); init(); drawcube(b); for(i=0;i<8;i++)
{
g[i].x1=a[i].x1;
g[i].y1=(a[i].y1-y)*cos(theta)+(a[i].z1-z)*sin(theta)+y;
g[i].z1=-(a[i].y1-y)*sin(theta)+(a[i].z1-z)*cos(theta)+z;
}
for(i=0;i<8;i++)
{
e[i].x2=g[i].x1+g[i].z1/2;
e[i].y2=g[i].y1+g[i].z1/2;
} drawcube(e); break;
case 2:
cleardevice();
init();
drawcube(b);
for(i=0;i<8;i++)
{
k[i].y1=a[i].y1;
k[i].x1=(a[i].x1-x)*cos(theta)+(a[i].z1-z)*sin(theta)+x;
k[i].z1=-(a[i].z1-z)*sin(theta)+(a[i].z1-z)*cos(theta)+z;
}
for(i=0;i<8;i++)
{ h[i].x2=k[i].x1+k[i].z1/2; h[i].y2=k[i].y1+k[i].z1/2;
}
drawcube(h); getch(); break;
case 3:
cleardevice(); init(); drawcube(b); for(i=0;i<8;i++)
{
l[i].z1=a[i].z1;
l[i].x1=(a[i].x1-x)*cos(theta)+(a[i].y1-y)*sin(theta)+x;
l[i].y1=(a[i].y1-y)*sin(theta)+(a[i].y1-y)*cos(theta)+y;
}
for(i=0;i<8;i++)
{ j[i].x2=l[i].x1+l[i].z1/2; j[i].y2=l[i].y1+l[i].z1/2;
}
drawcube(j); getch(); break;
}
}
PROGRAM:/* Composite 3D transformations*/
#include
#include
#include
#include
#include
int x,y,z,s,ds;
int ch,tx,ty,tz,i,m=9,tx1,ty1,tz1,tx2,ty2,tz2;
int sx,sy,sz,sx1,sx2,sy1,sy2,sz1,sz2;
int cho,i,n;
float theta,theta1,theta2;
int xx,yy,zz;
struct cube
{
int x1,y1,z1;
};
struct cube1
{
int x2,y2,z2;
};
struct cube a[8]={0},t[8]={0},f[8]={0},g[8]={0},k[8]={0},l[8]={0}; struct cube1 b[8]={0},c[8]={0},d[8]={0},e[8]={0},h[8]={0},j[8]={0}; void init()
{
int gd=DETECT,gm;
initgraph(&gd,&gm," ");
}
void create();
void drawcube(struct cube1 v[]);
void rotate();
void main()
{
init(); clrscr(); do{ flushall();
gotoxy(1,1);
cleardevice();
printf("\n \t1.create\n\t2.Translation \n \t3.Scaling\n \t4.Rotation\n\t5.Exit\n\nEnter your choice:");
scanf("%d",&ch);
setcolor(m++);
switch(ch)
{
case 1:
cleardevice();
init();
create(); getch(); break;
case 2:
cleardevice(); init(); flushall();
printf("\n Enter the translation vector1:");
scanf("%d%d%d",&tx1,&ty1,&tz1); printf("\n Enter the translation vector2:"); scanf("%d%d%d",&tx2,&ty2,&tz2); drawcube(b);
for(i=0;i<8;i++)
{
t[i].x1=a[i].x1+tx1; t[i].y1=a[i].y1+ty1; t[i].z1=a[i].z1+tz1;
}
for(i=0;i<8;i++)
{
c[i].x2=t[i].x1+t[i].z1/2;
c[i].y2=t[i].y1+t[i].z1/2;
} setcolor(1); drawcube(c); tx=tx1+tx2; ty=ty1+ty2; tz=tz1+tz2; for(i=0;i<8;i++)
{
t[i].x1=a[i].x1+tx; t[i].y1=a[i].y1+ty; t[i].z1=a[i].z1+tz;
}
for(i=0;i<8;i++)
{
c[i].x2=t[i].x1+t[i].z1/2;
c[i].y2=t[i].y1+t[i].z1/2;
} setcolor(2); drawcube(c); break;
case 3:
cleardevice();
init();
flushall();
printf("\n Enter the scaling vector1 :"); scanf("%d%d%d",&sx1,&sy1,&sz1); printf("\n Enter the scaling vector2 :"); scanf("%d%d%d",&sx2,&sy2,&sz2); drawcube(b);
for(i=0;i<8;i++)
{ f[i].x1=a[i].x1*sx1; f[i].y1=a[i].y1*sy1; f[i].z1=a[i].z1*sz1;
}
for(i=0;i<8;i++)
{ d[i].x2=f[i].x1+f[i].z1/2; d[i].y2=f[i].y1+f[i].z1/2;
//d[i].z2=f[i].z1+f[i].z1/2;
} sx=sx1*sx2; sy=sy1*sy2; sz=sz1*sz2; setcolor(3); drawcube(d); for(i=0;i<8;i++)
{
f[i].x1=a[i].x1*sx; f[i].y1=a[i].y1*sy; f[i].z1=a[i].z1*sz;
}
for(i=0;i<8;i++)
{
d[i].x2=f[i].x1+f[i].z1/2;
d[i].y2=f[i].y1+f[i].z1/2;
//d[i].z2=f[i].z1+f[i].z1/2;
}
setcolor(4); drawcube(d); break;
case 4:
cleardevice();
init();
rotate();
break;
case 5:
closegraph();
exit(0);
default:
continue;
}
}while(1);
}
void drawcube(struct cube1 v[])
{
gotoxy(3,4); line(v[0].x2,v[0].y2,v[1].x2,v[1].y2); line(v[1].x2,v[1].y2,v[2].x2,v[2].y2); line(v[2].x2,v[2].y2,v[3].x2,v[3].y2); line(v[3].x2,v[3].y2,v[0].x2,v[0].y2); line(v[4].x2,v[4].y2,v[5].x2,v[5].y2); line(v[5].x2,v[5].y2,v[6].x2,v[6].y2); line(v[6].x2,v[6].y2,v[7].x2,v[7].y2); line(v[7].x2,v[7].y2,v[7].x2,v[7].y2); line(v[0].x2,v[0].y2,v[4].x2,v[4].y2); line(v[1].x2,v[1].y2,v[5].x2,v[5].y2); line(v[2].x2,v[2].y2,v[6].x2,v[6].y2); line(v[3].x2,v[3].y2,v[7].x2,v[7].y2); line(v[4].x2,v[4].y2,v[7].x2,v[7].y2); getch();
}
void create()
{
int d,i;
flushall();
printf("\n Enter the cube co-ordinate:"); scanf("%d%d%d",&x,&y,&z); printf("\n enter the side of the cube:"); scanf("%d",&d);
cleardevice();
gotoxy(1,1);
settextstyle(1,1,5); outtextxy(10,30,"cube");
a[0].x1=x; a[0].y1=y; a[0].z1=z; a[1].x1=x+d; a[1].y1=y; a[1].z1=z; a[2].x1=x+d; a[2].y1=y+d; a[2].z1=z; a[3].x1=x;a[3].y1=y+d;a[3].z1=z; a[4].x1=x;a[4].y1=y;a[4].z1=z-d; a[5].x1=x+d;a[5].y1=y;a[5].z1=z-d; a[6].x1=x+d;a[6].y1=y+d;a[6].z1=z-d; a[7].x1=x;a[7].y1=y+d;a[7].z1=z-d; for(i=0;i<8;i++)
{
b[i].x2=a[i].x1+a[i].z1/2;
b[i].y2=a[i].y1+a[i].z1/2;
} cleardevice(); drawcube(b); getch();
}
void rotate()
{
cleardevice();
// int cho,i,n;
// float theta;
clrscr();
printf("\n Rotation with respect to axis:");
printf("\n 1.x-axis\n 2.y-axis\n 3.z-axis"); printf("\n Enter ur choice"); scanf("%d",&cho);
// printf("\n Enter the rotation angle:");
// scanf("%f",&theta);
// theta=(theta * 180/3.14);
switch(cho)
{
case 1: cleardevice(); init();
printf("\n Enter the rotation angle1:");
scanf("%f",&theta1);
theta1=(theta1 * 180/3.14);
printf("\n Enter the rotation angle2:");
scanf("%f",&theta2);
//setcolor(0); drawcube(b); for(i=0;i<8;i++)
{
g[i].x1=a[i].x1;
g[i].y1=(a[i].y1-y)*cos(theta1)+(a[i].z1-z)*sin(theta1)+y;
g[i].z1=-(a[i].y1-y)*sin(theta1)+(a[i].z1-z)*cos(theta1)+z;
}
for(i=0;i<8;i++)
{
e[i].x2=g[i].x1+g[i].z1/2;
e[i].y2=g[i].y1+g[i].z1/2;
}
setcolor(1); drawcube(e); flushall();
theta=theta1+theta2; theta=(theta * 180/3.14); for(i=0;i<8;i++)
{
g[i].x1=a[i].x1;
g[i].y1=(a[i].y1-y)*cos(theta)+(a[i].z1-z)*sin(theta)+y;
g[i].z1=-(a[i].y1-y)*sin(theta)+(a[i].z1-z)*cos(theta)+z;
}
for(i=0;i<8;i++)
{
e[i].x2=g[i].x1+g[i].z1/2;
e[i].y2=g[i].y1+g[i].z1/2;
}
setcolor(2);
drawcube(e);
break;
case 2: cleardevice(); init();
printf("\n Enter the rotation angle1:");
scanf("%f",&theta1);
theta1=(theta1 * 180/3.14);
printf("\n Enter the rotation angle2:");
scanf("%f",&theta2);
drawcube(b);
for(i=0;i<8;i++)
{
k[i].y1=a[i].y1;
k[i].x1=(a[i].x1-x)*cos(theta1)+(a[i].z1-z)*sin(theta1)+x;
k[i].z1=-(a[i].z1-z)*sin(theta1)+(a[i].z1-z)*cos(theta1)+z;
}
for(i=0;i<8;i++)
{
h[i].x2=k[i].x1+k[i].z1/2;
h[i].y2=k[i].y1+k[i].z1/2;
}
setcolor(1); drawcube(h); theta=theta1+theta2; theta=(theta * 180/3.14); for(i=0;i<8;i++)
{
k[i].y1=a[i].y1;
k[i].x1=(a[i].x1-x)*cos(theta)+(a[i].z1-z)*sin(theta)+x;
k[i].z1=-(a[i].z1-z)*sin(theta)+(a[i].z1-z)*cos(theta)+z;
}
for(i=0;i<8;i++)
{
h[i].x2=k[i].x1+k[i].z1/2;
h[i].y2=k[i].y1+k[i].z1/2;
}
setcolor(2); drawcube(h); break;
case 3: cleardevice(); init();
printf("\n Enter the rotation angle1:");
scanf("%f",&theta1);
theta1=(theta1 * 180/3.14);
printf("\n Enter the rotation angle2:");
scanf("%f",&theta2);
drawcube(b);
for(i=0;i<8;i++)
{
l[i].z1=a[i].z1;
l[i].x1=(a[i].x1-x)*cos(theta1)+(a[i].y1-y)*sin(theta1)+x;
l[i].y1=(a[i].y1-y)*sin(theta1)+(a[i].y1-y)*cos(theta1)+y;
}
for(i=0;i<8;i++)
{
j[i].x2=l[i].x1+l[i].z1/2;
j[i].y2=l[i].y1+l[i].z1/2;
}
setcolor(1); drawcube(j); theta=theta1+theta2; theta=(theta * 180/3.14); for(i=0;i<8;i++)
{
l[i].z1=a[i].z1;
l[i].x1=(a[i].x1-x)*cos(theta)+(a[i].y1-y)*sin(theta)+x;
l[i].y1=(a[i].y1-y)*sin(theta)+(a[i].y1-y)*cos(theta)+y;
}
for(i=0;i<8;i++)
{
j[i].x2=l[i].x1+l[i].z1/2;
j[i].y2=l[i].y1+l[i].z1/2;
}
setcolor(2); drawcube(j); getch();
break;
}
Drawing three dimensional objects and Scenes
PROGRAM:/* Drawing three dimensional objects and Scenes*/
#include
#include
#include
#inlclude
Main()
{
Int gd,gm;
Double d,h,l,x,y,stx,sty,dx,dy,dΘ,sinΘ,cosΘ,angle=0,theta,x-offset,y_offset;
Int I,j,k,balll[4][3]={1},ball[4][3]={1}; Float pi=3.142; Detectgraph(&gd,&gm); Initgraph(&gd,&gm,”C:\\tc\\bgi”);
Printf(“enter the starting x coordinates:”); Scanf(„%f”,&x);
Printf(“enter the starting y coordinates); Scanf(“%f”,&y);
Printf(“enter height”); Scanf(“%f”,&h);
Printf(“%f”,&l); Printf(“enter diameter”);
Scanf(“%f”,&d);
Theta=atan(h/l);
y_offset=d/(2*cos(theta))-(d/2*tan(theta));
x_offset=d/2;
circle(x+x_offset,y-y_offset,d/2); line(x,y,x,y+h); line(x,y+h,x+l,y+h);
stx=x;
sty=y; ball[0][0]=0; ball[0][1]=0-d/2; ball[1][0]=0; ball[1][1]=0+d/2; ball[2][0]=0-d/2; ball[2][1]=0; ball[3][0]=0+d/2; ball[3][1]=0;
ball l[0][0]=0;
ball l[0][1]=0-d/2;
ball l[1][0]=0;
ball l[1][1]=0+d/2;
ball l[2][0]=0-d/2;
ball l[2][1]=0;
ball l[3][0]=0+d/2
ball l[3][1]=0;
line(ball[0][0]+x+x-offset,ball[0][1]+y-y-offset,ball[1][0]+x+x-offset,ball[1][1]+y+y_offset);
line(ball[2][0]+x+x_offset,ball[2][1]+y-y-offset,ball[3][0]+x+x_offset,ball[3][1]+y_offset);
dy=h/l;
dx=1; do=(sqrt(h*h+l*l)/(pi*d)*360)/l; i=0;
do
{ Setcolor(0);
Circle(x+x_offset,ball l[0][1]+y-y_offset,ball l[1][0],+x+d/2);
line(ball l[0][0]+x+x_offset,ball l[0][1]+y-y-offset,ball l[1][0]+x+d/2,ball l[1][1]+y+y_offset);
line(ball l[2][0]+x+x_offset,ball l[2][1]+y-y-offset,ball l[3][0]+x+d/2,ball l[3][1]+y+y_offset);
sinΘ=sin(angle*pi/180);
cosΘ=cos(angle*pi/180);
for(j=0;j<4;j++)
{
Ball l[j][0]=ball[j][0]*cosΘ+ball[j][1]*(-sinΘ);
Ball l[j][1]=ball[j][0]*sinΘ+ball[j][1]*(cosΘ);
}
Set color(15); Angle=angle+do; X=x+dx; Y=y+dy;
Line(stx,sty,stx+l,sty+h);
Line(ball l[0][0]+x+x_offset,ball l[0][1]+y-y_offset ball l[1][1]+x+x_offset,+y_y_offset);
Line(ball l[2][0]+x+x_offset,ball l[2[1]+y-y_offset ball l[3][0]+x+x_offset,ball l[3][1]+y_y_offset);
Circle(x+x_offset,y-y_offset-d/2); I=i+1;
Delay(1000);
}
While(i
#include
#include
#include
#include
void myint(void)
{ glClearColor(1.0,1.0,1.0,1.0); glColor3f(1.0,0.0,0.0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0,500.0,0.0,500.0); glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
typedef GLfloat point2[2];
point2 vertices[3]={{0.0,0.0},{250.0,500.0},{500.0,0.0}};
int j,k;
int rand();
point2 p={{75.0},{50.0}}; glClear(GL_COLOR_BUFFER_BIT); for(k=0;k<5000;k++)
{ j=rand()%3; p[0]=(p[0]+vertices[j][0])/2.0; p[1]=(p[1]+vertices[j][1])/2.0;
glBegin(GL_POINTS);
glVertex2fv(p);
glEnd();
}
glFlush();
}
void main(int argc, char **argv)
{
glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(500,500); glutInitWindowPosition(0,0); glutCreateWindow("Pyramid"); glutDisplayFunc(display);
myint();
glutMainLoop();
}
No comments:
Post a Comment