Professional Documents
Culture Documents
RCS 653 CG LAB FIle (Solution)
RCS 653 CG LAB FIle (Solution)
RCS 653 CG LAB FIle (Solution)
#include <graphics.h>
#include <stdio.h>
#include <math.h>
#include <conio.h>
void main( )
{
float x,y,x1,y1,x2,y2,dx,dy,pixel;
int i,gd,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
dx=abs(x2-x1);
dy=abs(y2-y1);
if(dx>=dy)
pixel=dx;
else
pixel=dy;
dx=dx/pixel;
dy=dy/pixel;
x=x1;
y=y1;
i=1;
while(i<=pixel)
{
putpixel(x,y,15);
x=x+dx;
y=y+dy;
i=i+1;
//delay(100);
}
getch();
closegraph();
}
OUTPUT –
EXPERIMENT -02
#include <stdio.h>
# include <conio.h>
# include <graphics.h>
void main()
{
int dx,dy,x,y,p,x1,y1,x2,y2;
int gd,gm;
clrscr();
dx = (x2 - x1);
dy = (y2 - y1);
p = 2 * (dy) - (dx);
x = x1;
y = y1;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"c:\\Turboc3\\bgi");
putpixel(x,y,WHITE);
OUTPUT –
EXPERIMENT -03
1. To implement Bresenham’s algorithm for drawing circle using C
PROGRAM:
# include<stdio.h>
# include<conio.h>
# include<graphics.h>
void main()
{
int gd=DETECT,gm,xc,yc,r,x,y,Pk;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Bresenham's Circle Drawing Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter the Radius of circle\t");
scanf("%d",&r);
x=0;
y=r;
putpixel(xc+x,yc-y,1);
Pk=3-(2*r);
for(x=0;x<=y;x++)
{
if (Pk<0)
{
y=y;
Pk=(Pk+(4*x)+6);
}
else
{
y=y-1;
Pk=Pk+((4*(x-y)+10));
}
putpixel(xc+x,yc-y,7);
putpixel(xc-x,yc-y,7);
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+y,yc-x,7);
putpixel(xc-y,yc-x,7);
putpixel(xc+y,yc+x,7);
putpixel(xc-y,yc+x,7);
delay(100);
}
getch();
Output
#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,a,b;
float p1,p2;
clrscr();
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
printf("*** Ellipse Generating Algorithm ***\n");
printf("Enter the value of Xc\t");
scanf("%d",&xc);
printf("Enter the value of yc\t");
scanf("%d",&yc);
printf("Enter X axis length\t");
scanf("%d",&a);
printf("Enter Y axis length\t");
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;
delay(50);
}
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;
delay(50);
}
getch();
closegraph();
}
void disp()
{
putpixel(xc+x,yc+y,7);
putpixel(xc-x,yc+y,7);
putpixel(xc+x,yc-y,7);
putpixel(xc+x,yc-y,7);
}
Output
EXPERIMENT -04
OUTPUT –
2. To implement Mid Point Ellipse generating algorithm using C.
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,"C:\\TC\\BGI");
scanf("%d%d",&xc,&yc);
scanf("%d%d",&a,&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);
}
EXPERIMENT -05
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<math.h>
#include<stdlib.h>
void menu();
void input();
void output();
void translation();
void rotation();
void scaling();
void shearing();
void reflection();
int a[10][2],i,x,option,temp,angle,tx,ty,fx,fy,sh,k,n,axis,y;
float sx,sy;
void menu()
{
printf("menu\n");
printf("1.Translation\n");
printf("2.rotation\n");
printf("3.scaling\n");
printf("4.shearing\n");
printf("5.reflection\n");
printf("6.exit\n");
printf("enter the choice:");
scanf("%d",&option);
switch(option)
{
case 1:
input();
translation();
break;
case 2:
input();
rotation();
break;
case 3:
input();
scaling();
break;
case 4 :
input();
shearing();
break;
case 5:
input();
reflection();
break;
case 6:
exit(0);
break;
}
}
void input()
{
printf("enter the number of vertices:" );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the coordinates:");
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 tranformation vertex tx,ty:\n");
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();
delay(10);
menu();
}
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++)
{
a[i][0]=fx+(a[i][0]-fx)*cos(k)-(a[i][1]-fy)*sin(k);
a[i][1]=fy+(a[i][0]-fx)*sin(k)-(a[i][1]-fy)*cos(k);
}
output();
delay(10);
menu();
}
void scaling()
{
output();
printf("enter the scaling factor\n");
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();
delay(10);
menu();
}
void shearing()
{
output();
printf("enter the shear value:");
scanf("%d",&sh);
printf("enter the fixed point:");
scanf("%d%d",&fx,&fy);
printf("enter the axis for shearing if x-axis then 1 if y-axis the 0:");
scanf("%d",&axis);
for(i=0;i<=n;i++)
{
if(axis==1)
{
a[i][0]=a[i][0]+sh*(a[i][1]-fy);
}
else
{
a[i][1]=a[i][1]+sh*(a[i][0]-fx);
}
}
output();
delay(10);
menu();
}
void reflection()
{
output();
for(i=0;i<=n;i++)
{
temp=a[i][0];
a[i][0]=a[i][1];
a[i][1]=temp;
}
output();
delay(10);
menu();
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
menu();
getch();
}
OUTPUT:
Menu
1.Translation
2.Rotation
3.Scaling
4.Shearing
5.Reflection
6.Exit
Enter the choice : 1
Enter the number of Vertices: 3
Enter the coordinates : 30 150 10 200
Enter the coordinates : 10 200 60 200
if y-axis then 0
#include <stdio.h>
#include <stdlib.h>
#include <graphics.h>
#define MAX 20
if (y > ymax)
oc |= TOP;
else if (y < ymin)
oc |= BOTTOM;
if (x > xmax)
oc |= RIGHT;
else if (x < xmin)
oc |= LEFT;
return oc;
}
void cohen_sutherland (double x1, double y1, double x2, double y2,
double xmin, double ymin, double xmax, double ymax)
{
int accept;
int done;
outcode outcode1, outcode2;
accept = FALSE;
done = FALSE;
outcode1 = compute_outcode (x1, y1, xmin, ymin, xmax, ymax);
outcode2 = compute_outcode (x2, y2, xmin, ymin, xmax, ymax);
do
{
if (outcode1 == 0 && outcode2 == 0)
{
accept = TRUE;
done = TRUE;
}
else if (outcode1 & outcode2)
{
done = TRUE;
}
else
{
double x, y;
int outcode_ex = outcode1 ? outcode1 : outcode2;
if (outcode_ex & TOP)
{
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1);
y = ymax;
}
if (accept == TRUE)
line (x1, y1, x2, y2);
}
void main()
{
int n;
int i, j;
int ln[MAX][4];
int clip[4];
int gd = DETECT, gm;
PROGRAM –
#include<stdio.h>
#include<conio.h>
#include<graphics.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,"c:\\tc\\bgi");
printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\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)+.5);
x2=v1+floor(((float)(x2-w1)*sx)+.5);
x3=v1+floor(((float)(x3-w1)*sx)+.5);
y1=v2+floor(((float)(y1-w2)*sy)+.5);
y2=v2+floor(((float)(y2-w2)*sy)+.5);
y3=v2+floor(((float)(y3-w2)*sy)+.5);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
return 0;
}
OUTPUT –
Enter The Coordinate x1,y1,x2,y2,x3,y3
100
200
300
400
500
350
EXPERIMENT -07
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
void main()
{
float x1,y1,x2,y2,xmin,xmax,ymin,ymax,dx,dy;
float p[4],q[4],r[4];
float max,min,u1,u2;
float xi,xii,yi,yii;
int gd,gm,i;
gd=DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
printf("\n enter the line co-ordinates");
printf("\n enter 1st x=");
scanf("%f",&x1);
printf("\t 1st y=");
scanf("%f",&y1);
printf("\n enter 2nd x=");
scanf("%f",&x2);
printf("\t 2nd y=");
scanf("%f",&y2);
printf("\n enter window boundry");
printf("\n xmin=");
scanf("%f",&xmin);
printf("\n ymin=");
scanf("%f",&ymin);
printf("\n xmax=");
scanf("%f",&xmax);
printf("\n ymax=");
scanf("%f",&ymax);
dx=x2-x1;
dy=y2-y1;
cleardevice();
line(x1,y1,x2,y2);
rectangle(xmin,ymin,xmax,ymax);
p[0]=-dx;
q[0]=x1-xmin;
p[1]=dx;
q[1]=xmax-x1;
p[2]=-dy;
q[2]=y1-ymin;
p[3]=dy;
q[3]=ymax-y1;
for(i=0;i<4;i++)
{
if(p[i]==0 && q[i]<0)
{
printf("Line is outside the boundry,it is not a clipping candidate\n");
getch();
exit(0);
}}
for(i=0;i<4;i++)
{
r[i]=q[i]/p[i];
printf("\n r[%d]=%f",i,r[i]);
}
max=0;min=1;
for(i=0;i<4;i++)
if(p[i]<0)
{ if(r[i]>max)
max=r[i]; }
else
if(r[i]<min)
min=r[i];
u1=max;
u2=min;
printf("\n u1=%f",u1);
printf("\n u2=%f",u2);
if(u1>u2)
{ printf("\n line is completely outside");
getch();
exit(0); }
xi=x1+(u1*dx);
yi=y1+(u1*dy);
xii=x1+(u2*dx);
yii=y1+(u2*dy);
rectangle(xmin,ymin,xmax,ymax);
setcolor(5);
line(xi,yi,xii,yii);
getch();
closegraph();
}
OUTPUT –
Enter the line coordinates:
EXPERIMENT -08
#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 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("Coordinates of rectangular clip window :\nxmax,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,"C:\\TC\\BGI");
rectangle(xmin,ymax,xmax,ymin);
printf("\tUNCLIPPED POLYGON");
printf("hi");
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);
rectangle(xmin,ymax,xmax,ymin);
printf("\tCLIPPED POLYGON");
getch();
closegraph();
}
OUTPUT –
Polygon to be clipped:
Number of slides: 5
Enter the coordinates:
50
50
200
100
350
350
80
200
40
80
EXPERIMENT -09
1. To implement Bezier Curves algorithm using C.
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <graphics.h>
#include <math.h>
OUTPUT –
20
200
40
300
80
30
50
400
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd,gm, x1,x2,x3,x4,y1,y2,y3,y4,y11,y22,y33,y44,i;
gd=DETECT,gm=DETECT;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
x1=100;x2=130;x3=170;x4=200;y1=150;y2=16 ;y3=160;x4=165;
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x4,y4);
y11=y1;
y22=y2;
y33=y3;
y44=y4;
for(i=0;i<5;i++)
{
y11=y11+10;
y22=y22+10;
y33=y33+10;
y44=y44+10;
line(x1,y11,x2,y22);
line(x2,y22,x3,y33);
line(x3,y33,x4,y44);
}
line(x1,y1,x1,y11);
line(x2,y2,x2,y22);
line(x3,y3,x3,y33);
line(x4,y4,x4,y44);
getch();
closegraph();
}
EXPERIMENT -10
To perform 3D Transformations such as translation, rotation and scaling.
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,"..\\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("After Rotation about Z 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();
closegraph();
}
OUTPUT –
Translation
Scaling
Rotation
Enter rotating angle: 60
EXPERIMENT -11
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct point
{
int x,y,z;
};
struct edge
{
struct point start;
struct point end;
};
float pi=3.14/180.0;
void convert2d(int *x,int *y,int *z)
{
int xp,yp;
xp=(*x)+(*z)*cos(pi*45)/tan(pi*45);
yp=(*y)+(*z)*sin(pi*45)/tan(pi*45);
*x=xp;
*y=yp;
}
void screen(int *x,int *y)
{
int xm,ym;
xm=getmaxx();
ym=getmaxy();
*x=xm/2+*x;
*y=ym/2-*y;
}
void draw3d(struct edge po[],int n)
{
int i,x1,y1,z1,x2,y2,z2;
for(i=0;i<n;i++)
{
x1=po[i].start.x;
y1=po[i].start.y;
z1=po[i].start.z;
convert2d(&x1,&y1,&z1);
x2=po[i].end.x;
y2=po[i].end.y;
z2=po[i].end.z;
convert2d(&x2,&y2,&z2);
screen(&x1,&y1);
screen(&x2,&y2);
line(x1,y1,x2,y2);
}
}
void main()
{
int gd=DETECT,gm=0;
int i,tx,ty,tz,sx,sy,sz,n;
int xx1,xx2,yy1,yy2;
float rx,ry,rz;
struct edge p[50],q[50],r[50],s[50],t[50],v[50];
initgraph(&gd,&gm,"c:\\tc\\bgi");
cleardevice();
printf("\nEnter the number of edges:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nStart pt for edge %d(x,y,z):",i+1);
scanf("%d%d%d",&p[i].start.x,&p[i].start.y,&p[i].start.z);
printf("\nEnd pt for edge %d(x,y,z):",i+1);
scanf("%d%d%d",&p[i].end.x,&p[i].end.y,&p[i].end.z);
}
cleardevice();
printf("\n3D VIEW");
draw3d(p,n);
getch();
cleardevice();
printf("\nTOP VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.x;
yy1=p[i].start.z;
xx2=p[i].end.x;
yy2=p[i].end.z;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
printf("\nSIDE VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.z;
yy1=p[i].start.y;
xx2=p[i].end.z;
yy2=p[i].end.y;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
printf("\nFRONT VIEW");
for(i=0;i<n;i++)
{
xx1=p[i].start.x;
yy1=p[i].start.y;
xx2=p[i].end.x;
yy2=p[i].end.y;
screen(&xx1,&yy1);
screen(&xx2,&yy2);
line(xx1,yy1,xx2,yy2);
}
getch();
cleardevice();
}
OUTPUT –
Start pt for edge 1 (x,y,z) :0 0 0
End pt for edge 1 (x,y,z) : 200 0 0
Start pt for edge 2 (x,y,z) : 200 0 0
End pt for edge 2 (x,y,z) : 200 0 100
Start pt for edge 3 (x,y,z) : 200 0 100
End pt for edge 3 (x,y,z) :0 0 100
Start pt for edge 4 (x,y,z) :0 0 100
End pt for edge 4 (x,y,z) :0 0 0
Start pt for edge 5 (x,y,z) :0 100 0
End pt for edge 5 (x,y,z) : 200 100 0
Start pt for edge 6 (x,y,z) : 200 100 0
End pt for edge 6 (x,y,z) : 200 100 100
Start pt for edge 7 (x,y,z) : 200 100 100
End pt for edge 7 (x,y,z) :0 100 100
Start pt for edge 8 (x,y,z) :0 100 100
End pt for edge 8 (x,y,z) :0 100 0
Start pt for edge 9 (x,y,z ) :0 100 0
End pt for edge 9 (x,y,z) :0 0 0
Start pt for edge 10 (x,y,z) : 200 100 0
End pt for edge 10 (x,y,z) : 200 0 0
Start pt for edge 11 (x,y,z) : 200 100 100
End pt for edge 11 (x,y,z) : 200 0 100
Start pt for edge 12 (x,y,z) :0 100 100
End pt for edge 12 (x,y,z) :0 0 100
3D View –
Top View –
Side View –
Front View –
EXPERIMENT -12
Write a c program to draw a solid and find its vanishing point.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void createsolid();
void main()
{
int gd = DETECT,gm = DETECT;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
createsolid();
getch();
closegraph();
}
void createsolid()
{
//The coordinates of first rectangle
line(250,200,370,200);
line(370,200,370,320);
line(370,320,250,320);
line(250,200,250,320);
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd=DETECT,gm,n;
float r=0,g=0,b=0;
float y=0,i=0,q=0;
float c=0,m=0,y1=0;
initgraph(&gd,&gm,"");
printf("(r,g,b)keys for incrementing R,G,B values respectively\n");
printf("(shift+(r,g,b))keys for decrementing R,G,B values respectively\n");
printf("press esc to exit\n");
setcolor(15);
while(1)
{ gotoxy(18,10);
printf("R G B");
c=1.0-r;
m=1.0-g;
y1=1.0-y;
gotoxy(47,10);
printf("C M Y");
y=0.299*r+0.587*g+0.144*b;
i=0.596*r-0.275*g-0.3218*b;
q=0.212*r-0.528*g+0.311*b;
gotoxy(18,23);
printf("Y I Q");
switch(getche())
{ case 'r':
r++;
break;
case 'g':
g++;
break;
case 'b':
b++;
break;
case 'R':
r--;
break;
case 'G':
g--;
break;
case 'B':
b--;
break;
case 27:
closegraph();
exit(0);
}
if(r>255)
r=0;
if(g>255)
g=0;
if(b>255)
b=0;
setrgbpalette(1,r,g,b);
setfillstyle(1,1);
bar(50,50,270,250);
rectangle(50,50,270,250);
setrgbpalette(2,c,m,y1);
setfillstyle(1,2);
bar(275,50,495,250);
rectangle(275,50,495,250);
setrgbpalette(3,y,i,q);
setfillstyle(1,3);
bar(50,255,270,455);
rectangle(50,255,270,455);
}
}
OUTPUT-
EXPERIMENT -14
1. Road animation using C.
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int i,gd=DETECT,gm;
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"C:\\TC\\BGI");
// Set Background colour to GREEN
setbkcolor(GREEN);
// The program runs indefinitely until you press a key
while(!kbhit())
{
// Draw the road
setfillstyle(SOLID_FILL,8);
bar(150,0,470,500);
// Draw the WHITE divider lines
setfillstyle(SOLID_FILL,15);
bar(310,0+i,320,50+i);
bar(310,70+i,320,120+i);
bar(310,140+i,320,190+i);
bar(310,210+i,320,260+i);
bar(310,280+i,320,330+i);
bar(310,350+i,320,400+i);
bar(310,420+i,320,470+i);
delay(200);
i=i+10;
if(i >= 50) {
i=0;
}
}
closegraph();
getch();
}
while (!kbhit()) {
/* drawing 9 orbits */
setcolor(WHITE);
for (i = 0; i < 9; i++) {
ellipse(midx, midy, 0, 360, xrad[i], yrad[i]);
}
/* sun at the mid of the solar system */
setcolor(YELLOW);
setfillstyle(SOLID_FILL, YELLOW);
circle(midx, midy, 20);
floodfill(midx, midy, YELLOW);
/* mercury in first orbit */
setcolor(CYAN);
setfillstyle(SOLID_FILL, CYAN);
pieslice(x[0][pos[0]], y[0][pos[0]], 0, 360, planet[0]);
/* venus in second orbit */
setcolor(GREEN);
setfillstyle(SOLID_FILL, GREEN);
pieslice(x[1][pos[1]], y[1][pos[1]], 0, 360, planet[1]);
/* earth in third orbit */
setcolor(BLUE);
setfillstyle(SOLID_FILL, BLUE);
pieslice(x[2][pos[2]], y[2][pos[2]], 0, 360, planet[2]);
/* mars in fourth orbit */
setcolor(RED);
setfillstyle(SOLID_FILL, RED);
pieslice(x[3][pos[3]], y[3][pos[3]], 0, 360, planet[3]);
/* jupiter in fifth orbit */
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
pieslice(x[4][pos[4]], y[4][pos[4]], 0, 360, planet[4]);
/* saturn in sixth orbit */
setcolor(LIGHTGRAY);
setfillstyle(SOLID_FILL, LIGHTGRAY);
pieslice(x[5][pos[5]], y[5][pos[5]], 0, 360, planet[5]);
/* uranus in sevth orbit */
setcolor(BROWN);
setfillstyle(SOLID_FILL, BROWN);
pieslice(x[6][pos[6]], y[6][pos[6]], 0, 360, planet[6]);
/* neptune in eigth orbit */
setcolor(LIGHTBLUE);
setfillstyle(SOLID_FILL, LIGHTBLUE);
pieslice(x[7][pos[7]], y[7][pos[7]], 0, 360, planet[7]);
/* pluto in ninth orbit */
setcolor(LIGHTRED);
setfillstyle(SOLID_FILL, LIGHTRED);
pieslice(x[8][pos[8]], y[8][pos[8]], 0, 360, planet[8]);
/* checking for one complete rotation */
for (i = 0; i < 9; i++) {
if (pos[i] <= 0) {
pos[i] = 59;
} else {
pos[i] = pos[i] - 1;
}
}
/* sleep for 100 milliseconds */
delay(100);
/* clears graphic screen */
cleardevice();
}
/* deallocate memory allocated for graphic screen */
closegraph();
return 0;
}
EXPERIMENT -15
To perform basic operations on image using any image editing software (Adobe
Photoshop)
PROGRAM
The easiest way to load an image is to open the image file on your computer. You can do this
from the menu using File -> Open or pressing Ctrl+O on your keyboard.
Part 2. Cropping an Image
In cropping an image, our goal is to get rid of excess space or unwanted graphics in the
image.First, select the Select Tool, or Rectangular Marquee Tool. The hotkey for this is “M”. It
is located on the panel in the left with all of your image editing tools, pictured here to the left.
Then, drag your mouse across the area you wish to keep.
Note: While selecting part of the image, you may click elsewhere to reselect the area, or right-
click and hit “deselect”. Ctrl+R or View -> Rulers will add rulers to the top and left to help
decide which area you wish to select. You may also wish to zoom in or out using View -> Zoom
In or View -> Zoom Out.
Lastly, you may undo or redo changes with Edit -> Step Backward and Edit -> Step Forward in
the top menu. The hotkeys for these are Alt+Ctrl+Z and Shift+Ctrl+Z for backward and forward
respectively and are very useful. Once you have selected your region, open the “Image” menu at
the top and select “Crop” to crop out the rest of the image.
Part 3. Resizing the Image
At times, an image may be too small or large for its intended purpose and may need to be
resized. From the Image menu, select “Image Size”. The hotkey for this is Alt+Ctrl+I. A menu
with various options will appear. However, it is actually rather simple. You may enter the
dimensions of the image as a width and height in any units you want (it is currently set under
“pixels”, a common unit of graphical space, where one “pixel” is equal to a colored dot on your
computer)
Part 4. To adjust the brightness and contrast
1. Open adobe Photoshop 7.0-> file->open-> choose a file and open it.
2. Go to image->adjustments->Brightness/Contrast.
3. After getting the Brightness/Contrast window adjust the brightness and contrast by dragging.
the appropriate bar setting.
4. Finally save the image file.
EXPERIMENT -16
void main()
{
int gd=DETECT,gm;
int x=10,y=480;
initgraph(&gd,&gm,"..\\bgi");
while(!kbhit())
{
cleardevice();
if(y==0)
{
y=random(480);
x=random(640);
}
else
{
y=y-1;
x=x+1;
line(x-50,y,x,y-70);
line(x,y-70,x+50,y);
line(x+50,y,x,y+70);
line(x,y+70,x-50,y);
line(x,y-70,x,y+70);
line(x,y+70,x+10,y+140);
line(x,y+70,x-10,y+140);
line(x-50,y,x+50,y);
line(x,y,x+130,y+640);
}
delay(20);
}
closegraph();
restorecrtmode();
}
PROGRAM
#include<graphics.h>
#include<conio.h>
int main(){
int gd = DETECT,gm;
initgraph(&gd, &gm, "X:\\TC\\BGI");
/* Draw Hut */
setcolor(WHITE);
rectangle(150,180,250,300);
rectangle(250,180,420,300);
rectangle(180,250,220,300);
line(200,100,150,180);
line(200,100,250,180);
line(200,100,370,100);
line(370,100,420,180);
/* Fill colours */
setfillstyle(SOLID_FILL, BROWN);
floodfill(152, 182, WHITE);
floodfill(252, 182, WHITE);
setfillstyle(SLASH_FILL, BLUE);
floodfill(182, 252, WHITE);
setfillstyle(HATCH_FILL, GREEN);
floodfill(200, 105, WHITE);
floodfill(210, 105, WHITE);
getch();
closegraph();
return 0;
}
PROGRAM
#include<graphics.h>
#include<conio.h>
#include<iostream.h>
void main()
{
int gd = DETECT,gm;
initgraph(&gd,&gm ,"");
ellipse(200,200,0,360,50,30);
line(250,200,280,170);
line(280,170,280,230);
line(280,230,250,200);
circle(160,190,3);
getch();
closegraph();
}