CGPGM
CGPGM
CGPGM
EX NO: 1
Aim :
Algorithm:
Step 1: Input the line endpoints and store the left endpoint in (x1, y1) and right endpoint
in (x2, y2)
x = xb xa, y = yb ya
Step 3: if the values of x > y assign values of steps as x otherwise the values of steps
as y
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
void main()
{
int i,steps,x1,x2,y1,y2;
float x,y,xinc,yinc,dx,dy;
char msg[86];
clrscr();
initgraph(&gdriver,&gmode,f:\\tc);
scanf(%d%d%d%d,&x1,&x2,&y1,&y2);
cleardevice();
line(x1,x2,y1,y2);
dx = x2 x1;
dy = y2 y1;
steps = abs(dx);
else
steps = abs(dy);
xinc = (float)dx/steps ;
yinc = (float)dy/steps ;
y = y1;
x = x1;
putpixel(ceil(x),ceil(y),20);
x += xinc ;
y += yinc ;
putpixel(x,y,2);
delay(45);
getch();
OUTPUT :
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 2
Aim :
Algorithm:
Step 4: Calculate the value of dx and dy and find the starting value of decision parameter
as dp=2*dy-dx.
Step 5: Calculate the values of s1 and s2 depending on (x1,y1) and (x2,y2) values.
dp=dp+2*dx-2*dy.
PROGRAM :
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
void main()
int x1,x2,y1,y2;
int gd=DETECT,gm;
void linebres(int,int,int,int);
initgraph(&gd,&gm,);
cleardevice();
linebres(x1,y1,x2,y2);
getch();
line(x1,y1,x2,y2);
getch();
closegraph();
int dx=abs(x1-x2),dy=abs(y1-y2);
int p,x,y,i,xend,yend;
if(dx!=0)
p=2*dy-dx;
if(x1>x2)
x=x2;
y=y2;
xend=x1;
else
{
x=x1;
y=y1;
xend=x2;
putpixel(x,y,2);
for(i=x;i<xend;i++)
x+=1;
if(p<0)
p+=2*dy;
else
p+=2*(dy-dx);
putpixel(x,y,2);
else
p=2*dx-dy;
if(y1>y2)
x=x2;
y=y2;
yend=y2;
putpixel(x,y,2);
for(i=y;i<yend;i++)
y+=1;
if(p<0)
p+=2*dx;
else
x+=1;
p+=2*(dx-dy);
putpixel(x,y,2);
OUTPUT :
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 3
Aim :
Algorithm:
Step 2: Input radius r and the midpoint of the circle (x,y) and obtain the first point on the
circumference for the circle as (0,r).
Step 6: Move each calculated pixel position (x,y) onto the circular path centered on
(xc,yc) and plot the coordinate value as x=x+xc and y=y+yc.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
int gd=DETECT,gm;
int x,y,r;
void cir(int,int,int);
scanf(%d%d%d,&x,&y,&r);
initgraph(&gd,&gm,);
cir(x,y,r);
getch();
closegraph();
int x=0,y=r,p=1-r;
void cliplot(int,int,int,int);
cliplot(x1,y1,x,y);
while(x<y)
x++;
if(p<0)
p+=2*x+1;
else
y;
p+=2*(x-y)+1;
cliplot(x1,y1,x,y);
OUTPUT:
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 4
Aim :
Algorithm:
Step 3: Calculate the initial value of the decision parameter in region1 as P10 = ry2 rx2ry
+ rx2
Step 4: At each position k x in region 1 , starting at k=0,perform the following test. If p1k
< 0 the next point along the ellipse centered on (0,0) is (xk+1,yk) and p1k+1 = p1k + 2ry2xk+1 +
ry2
Step 5: Otherwise the next point along the ellipse is( xk+1 ,yk-1 ) and
p1k+1 = p1k +2ry2 xk+1 2 rx2 yk+1 + ry2 with
2ry2xk+1 = 2ry2xk+2ry2 , 2rx2yk+1 = 2rx2yk 2rx2 and continue until 2ry2x 2rx2y.
Step 7: At each yk position in region 2, starting at k=0,perform the following test , if p2k
> 0 the next point along the ellipse centered on (0,0) is ( xk , yk+1) and p2k+1 = p2k
2rx2yk+1 + ry2.
Step 8: Otherwise the next point along the ellipse is ( xk + 1 ,yk -1) and
Step 9: Using the same incremental values for x and y as in region 1 continue until y=0.
Step 10: For both regions determine symmetry points along the other three quadrants.
Step 11: Move each calculated pixel position (x,y) on to the elliptical path centered on (xc ,
yc ) and plot the co-ordinates values
x = x + xc ,
y = y + yc .
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
main()
int gd=DETECT,gm;
int xcenter,ycenter,rx,ry;
int p,x,y,px,py,rx1,ry1,rx2,ry2;
initgraph(&gd,&gm,c:\\tc\\bgi);
scanf(%d%d,&rx,&ry);
scanf(%d%d,&xcenter,&ycenter);
ry1=ry*ry;
rx1=rx*rx;
ry2=2*ry1;
rx2=2*rx1;
/* REGION 1 */
x=0;
y=ry;
plotpoints(xcenter,ycenter,x,y);
p=(ry1-rx1*ry+(0.25*rx1));
px=0;
py=rx2*y;
while(px<py)
x=x+1;
px=px+ry2;
if(p>=0)
y=y-1;
py=py-rx2;
if(p<0)
p=p+ry1+px;
else
p=p+ry1+px-py;
plotpoints(xcenter,ycenter,x,y);
/* REGION 2*/
p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);
while(y>0)
y=y-1;
py=py-rx2;
if(p<=0)
{
x=x+1;
px=px+ry2;
if(p>0)
p=p+rx1-py;
else
p=p+rx1-py+px;
plotpoints(xcenter,ycenter,x,y);
getch();
return(0);
putpixel(xcenter+x,ycenter+y,6);
putpixel(xcenter-x,ycenter+y,6);
putpixel(xcenter+x,ycenter-y,6);
putpixel(xcenter-x,ycenter-y,6);
}
OUTPUT:
RESULT:
Thus the above program has been executed and output is verified.
OUTPUT PRIMITIVES
EX NO: 5
Aim :
To write a C program to draw the various attributes of line, circle and ellipse.
Algorithm:
Step 1: Start the program.
PROGRAM:
#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,rad,sa,ea,xrad,yrad,i;
initgraph(&gd,&gm,);
while(ch==y)
cleardevice();
setbkcolor(9);
outtextxy(100,130,Choose From The Following );
outtextxy(100,150,1. 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,AMSCOLLEGE);
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:
1.Line
2. Circle
3.Box
4.Arc
5.Ellipse
6.Rectangle
7.Exit
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 6
Aim :
Algorithm:
Step 1: Start the program.
b) Rotate a point at position (x,y) through an angle about the origin x1=xcos
ysin , y1=ycos + xsin .
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
b) Reflection about y axis : The transformed coordinates are x1=x and y1=y.
x1=x
PROGRAM:
#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);
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++)
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();
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();
scanf(%d,&y);
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();
scanf(%f%f,&sx,&sy);
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();
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
Translation
1. Rotation
2. Rotation
3. Scaling
4. Shearing
5. Reflection
6. Exit
ROTATION
SCALING
SHEARING
Enter the choice : 4
if y-axis then 0
REFLECTION
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 7
Aim :
Algorithm:
Step 3: Translation
a) Rotate a point at position (x,y) through an angle about the origin x1=xcos ysin ,
y1=ycos + xsin.
Step 5: Scaling
Step 6: Reflection
a) Reflection about x axis : The transformed coordinates are x1=a and y1=-y.
Step 7: Reflection about y axis : The transformed coordinates are x1=x and y1=y.
Step 8: Shearing
f) Y axis shear related to the reference line x=xref is x1=x and y1=y+shy(x-xref)
Step 9: Finally display the transformed object after the successive transformations.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,
sx,sy,shx,shy,xref,yref;
char d;
gd=DETECT;
initgraph(&gd,&gm,);
cin>>n;
for(i=0;i<n;i++)
cin>>xa[i]>>ya[i];
do
cout<<menu;
cout<<\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit;
cin>>op;
switch(op)
case 1:
for(i=0;i<n;i++)
xa1[i]=xa[i]+tx;
ya1[i]=ya[i]+ty;
cleardevice();
cout<<before translation;
for(i=0;i<n;i++)
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
cout<<after translation;
for(i=0;i<n;i++)
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
getch();
cleardevice();
break;
case 2:
cin>>theta;
theta=(theta*3.14)/180;
cin>>xf>>yf;
for(i=0;i<n;i++)
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
cleardevice();
cout<<before rotation;
for(i=0;i<n;i++)
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
cout<<after rotation;
for(i=0;i<n;i++)
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
getch();
cleardevice();
break;
case 3:
cout<<enter the scaling factor;
cin>>sx>>sy;
cin>>rx>>ry;
for(i=0;i<n;i++)
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
cleardevice();
cout<<before scaling;
for(i=0;i<n;i++)
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
cout<<after scaling;
for(i=0;i<n;i++)
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
getch();
cleardevice();
break;
case 4:
cin>>shx>>shy;
cin>>xref>>yref;
cin>>d;
if(d==x)
for(i=0;i<n;i++)
xa1[i]=xa[i]+shx*(ya[i]-yref);
ya1[i]=ya[i];
cleardevice();
cout<<before shearing;
for(i=0;i<n;i++)
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
cout<<after shearing;
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
getch();
cleardevice();
break;
case 5:
cout<<before reflection;
for(i=0;i<n;i++)
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
cout<<after reflection;
for(i=0;i<n;i++)
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
getch();
cleardevice();
break;
case 6:
exit(0);
break;
}
}while(op!=6);
OUTPUT :
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
1
enter the translation vector: 30 40
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
BEFORE AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
4
Enter the shear value: 3 4
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
BEFORE
AFTER
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 8
Aim :
Algorithm:
Step 3: Every line end point is assigned a code that identified the location of the point
relative to the boundaries of the clipping rectangle.
Step 4: Check whether the line lies inside the window then it is entirely drawn.
Step 5: Check whether the line lies outside the window then it is entirely clipped.
b) Determine the intersection point and how much of the line is to be discarded.
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
code(float,float);
void clip(float,float,float,float);
void rect(float,float,float,float);
void main()
{
float x1,y1,x2,y2;
int g=0,d;
initgraph(&g,&d,c:\\tc\\bin);
settextstyle(1,0,1);
outtextxy(40,15,BEFORE CLIPPING);
scanf(%f%f%f%f,&cxl,&cyt,&cxr,&cyt);
rect(cxl,cyb,cxr,cyt);
getch();
scanf(%f%f%f%f,&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
getch();
cleardevice();
settextstyle(1,0,1);
outtextxy(40,15,after clipping);
clip(x1,y1,x2,y2);
getch();
closegraph();
int c,c1,c2;
float x,y;
c1=code(x1,y1);
c2=code(x2,y2);
getch();
while((c1!=0)||(c2!=0))
if((c1&c2)!=0)
goto out;
c=c1;
if(c==0)
c=c2;
if((c&1)==1)
y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
x=cxl;
else
if((c&2)==2)
y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
x=cxr;
else
if((c&8)==8)
x=x1+(x2-x1)*(cyb-y1)/(y2-y1);
y=cyb;
else
if((c&4)==4)
x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
y=cyt;
if(c==c1)
x1=x;
y1=y;
c1=code(x,y);
else
x2=x;
y2=y;
c2=code(x,y);
}
out : rect(cxl,cyb,cxr,cyt);
line(x1,y1,x2,y2);
code(float x,float y)
int c=0;
if(x<cxl)
c=1;
else
if(x>cxr)
c=2;
if(y<cyb)
c=c|8;
else
if(y>cyt)
c=c|4;
return c;
line(xl,yb,xr,yb);
line(xr,yb,xr,yt);
line(xr,yt,xl,yt);
line(xl,yt,xl,yb);
OUTPUT
Enter the left, bottom, right ,top of clip window
200
200
400
400
250
300
400
450
Before Clipping:
After Clipping
RESULT:
Thus the above program has been executed and output is verified.
Aim :
Algorithm:
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);
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
100
200
300
400
500
350
400
500
350
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 10
Aim :
Algorithm:
Step 5: Compare the vertices of each edge of the polygon , individually with the clipping
plane
Step 6: Save the resulting intersections and vertices in the new list of vertices according
to four possible relationships between the edge and the clipping boundary discussed earlier
Step 7: Repeat the steps 4 and 5 for remaining edges of the clipping window. Each time
the resultant list of vertices is successively passed to process the next edge of the clipping
window
PROGRAM:
#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include <process.h>
#define TRUE 1
#define FALSE 0
enum
TOP = 0x1,
BOTTOM = 0x2,
RIGHT = 0x4,
LEFT = 0x8
};
float xwmin,xwmax,ywmin,ywmax;
outcode outcode0,outcode1,outcodeOut;
outcode0 = CompOutCode(x0,y0);
outcode1 = CompOutCode(x1,y1);
do
if(!(outcode0|outcode1))
accept = TRUE;
done = TRUE;
else
done = TRUE;
else
float x,y;
outcodeOut = outcode0?outcode0:outcode1;
if(outcodeOut & TOP)
x = x0+(x1-x0)*(ywmax-y0)/(y1-y0);
y = ywmax;
else
x = x0+(x1-x0)*(ywmin-y0)/(y1-y0);
y = ywmin;
else
y = y0+(y1-y0)*(xwmax-x0)/(x1-x0);
x = xwmax;
else
y = y0+(y1-y0)*(xwmin-x0)/(x1-x0);
x = xwmin;
if(outcodeOut==outcode0)
{
x0 = x;
y0 = y;
outcode0 = CompOutCode(x0,y0);
else
x1 = x;
y1 = y;
outcode1 = CompOutCode(x1,y1);
while(done==FALSE);
if(accept)
line(x0,y0,x1,y1);
rectangle(xwmin,ywmin,xwmax,ywmax);
outcode code = 0;
if(y>ywmax)
code|=TOP;
else
if(y<ywmin)
code|=BOTTOM;
if(x>xwmax)
code|=RIGHT;
else
if(x<xwmin)
code|=LEFT;
return code;
void main( )
float x1,y1,x2,y2;
clrscr( );
scanf(%d,&n);
for(i=0;i<2*n;i++)
scanf(%d,&poly[i]);
}
poly[2*n]=poly[0];
poly[2*n+1]=poly[1];
scanf(%f%f%f%f,&xwmin,&ywmin,&xwmax,&ywmax);
drawpoly(n+1,poly);
rectangle(xwmin,ywmin,xwmax,ywmax);
getch( );
cleardevice( );
for(i=0;i<n;i++)
clip(poly[2*i],poly[(2*i)+1],poly[(2*i)+2],poly[(2*i)+3]);
getch( );
restorecrtmode( );
OUTPUT:
POLYGON CLIPPING
RESULT:
Thus the above program has been executed and output is verified.
EX NO: 11
Aim :
Algorithm:
Step 4: using the function line, display the object before and after
translation.
Step 6: using the function line, display the object before and after scaling.
Step 8: using the function line,display the object before and after rotation.
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, );
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);
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);
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);
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();
bar3d(midx+x1,midy-y1,midx+x2,midy-y2,5,1);
axis();
bar3d(midx+50,midy-x1,midx+60,midy-x2,5,1);
axis();
bar3d(midx+x1,midy-100,midx+x2,midy-90,5,1);
getch();
closegraph();
}
OUTPUT: Download PDF for Image click HERE
Translation
Before Translation
After Translation
Scaling
After Scaling
Rotation
Thus the above program has been executed and output is verified.
EX NO: 12
Aim :
Algorithm:
Step 2: input the points for the cude and size of the Cube.
Step 7: If the choice is 3 get the rotation angle. Rotate the figure with respect to the axis
of rotation.
PROGRAM:
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
public:
int i;
for(i=0;i<4;i++)
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
for(i=4;i<8;i++)
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
for(i=0;i<4;i++)
line(x1[i],y1[i],x1[i+4],y1[i+4]);
};
void main()
int
i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,
x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,C:\\tc++\bgi);
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
cube c;
getch();
cleardevice();
do
{
cout<<menu<<endl;
cout<<\n1.translation\n2.rotation\n3.scaling\n4.exit\n;
cin>>ch;
switch(ch)
case 1:
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
for(i=0;i<8;i++)
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
cleardevice();
cout<<before translation;
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<after translation;
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cin>>theta;
theta=(theta*3.14)/180;
if(op==1)
for(i=0;i<8;i++)
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
else
if(op==2)
for(i=0;i<8;i++)
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
else
if(op==3)
for(i=0;i<8;i++)
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
else
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
cleardevice();
cout<<before rotation;
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<after rotation;
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
cin>>sx>>sy>>sz;
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
}
for(i=0;i<8;i++)
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
cleardevice();
cout<<before scaling;
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<after scaling;
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
while(op!=4);
getch();
}
OUTPUT :
MENU
1.Translation
2.Rotation
3.scaling
4.exit
Before After
Rotation
Before :
After :
MENU
1.Translation
2.Rotation
3.scaling
4.exit
BEFORE
AFTER
MENU
1.Translation
2.Rotation
3.scaling
4.exit
RESULT:
Thus the above program has been executed and output is verified..
EX NO: 13
Aim :
Algorithm:
Step 3: input the start pt. and end pt. for the all the edges.
Step 4: draw and display the image obtained from the these points.
Step 5: generate the top view and display it.
PROGRAM :
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
struct point
int x,y,z;
};
struct edge
};
float pi=3.14/180.0;
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;
int xm,ym;
xm=getmaxx();
ym=getmaxy();
*x=xm/2+*x;
*y=ym/2-*y;
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;
initgraph(&gd,&gm,c:\\tc\\bgi);
cleardevice();
scanf(%d,&n);
for(i=0;i<n;i++)
scanf(%d%d%d,&p[i].start.x,&p[i].start.y,&p[i].start.z);
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:
3D VIEW
TOP VIEW
SIDE VIEW
FRONT VIEW
RESULT:
Thus the above program has been executed and output is verified.