Computer Graphics Lab Manual
Computer Graphics Lab Manual
Computer Graphics Lab Manual
Lab Manual
2.
3.
4.
Write a program to draw a circle using Midpoint algorithm. Modify the same for
drawing an arc and sector.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
S. No.
Total No of Labs :
Programs List
No.
of
Labs
10
11
12
13
14
15
Section B
Projects of this section are allocated to the students as Lab assignments and the
students are expected to submit the project in scheduled time.
List of Projects
1. Draw a hut with two windows.
2. Draw a laptop with some object in it.
3. Draw a ceiling fan.
4. Draw a full moon and three stars.
5. Draw screen and CPU.
6. Draw a mobile phone.
7. Draw a bar chart having at least 3 bars.
Note: Try transformations and other required functions on the same to animate and
color them.
Project work will pass through given stages:
Stages
Project design as a
whole
Modular design
Algorithm
Coding
Project Plan
August Fourth Week
September First Week
September Second Week
September Third Week October
Second Week
Error Removal/Handling
Testing
Submission
Section - C
Project Allocation Table
Project No.
Student Roll-No
1
2
3
4
1---8
9--16
17-24
25-28
Declaration
Remarks
Initgraph initializes the graphic system by loading a graphics driver from disk (or
validating a registered driver) then putting the system into graphics mode.
Initgraph also resets all graphics settings (color, palette, current position, viewport, etc)
to their defaults then resets graph.
2) GETPIXEL, PUTPIXEL
Decleration
Remarks
Return value
3) CLOSE GRAPH
Decleration
Remarks
It then restores the screen to the mode it was in before you called initgraph.
Return value
None.
Decleration
Void far arc(int x, int y, int stangle, int end_angle, int radius);
Void far pieslice(int x, int y, int stangle, int end_angle, int radius);
Remarks
Pieslice draws a pieslice in the current drawing color, then fills it using the current fill
pattern and fill color.
Decleration
Void far ellipse(int x, int y, int stangle, int end_angle, int xradius, int yradius)
Void farsectoe(int x, int y, int stangle, int end_angle, int xradius, int yradius)
Remarks
Fillellipse draws an elliptical arc in the current drawing color and than fills it with fill
color and fill pattern.
Sector draws an elliptical pie slice in the current drawing color and than fills it using the
pattern and color defined by setfillstyle or setfillpattern.
6) FLOODFILL
Decleration
Remarks
The area bounded by the color border is flooded with the current fill pattern and fill
color.
If the seed is outside the enclosed area, the exterior will be filled.
Use fillpoly instead of floodfill wherever possible so you can maintain code
compatibility with future versions.
Return value
7) GETCOLOR, SETCOLOR
Decleration
Remarks
Setcolor sets the current drawing color to color, which can range from 0 to
getmaxcolor.
To set a drawing color with setcolor , you can pass either the color number or the
equivalent color name.
Decleration
Remarks
Line draws a line from (x1, y1) to (x2, y2) using the current color, line style and
thickness. It does not update the current position (CP).
Linerel draws a line from the CP to a point that is relative distance (dx, dy) from the CP,
then advances the CP by (dx, dy).
Lineto draws a line from the CP to (x, y), then moves the CP to (x,y).
Return value
None
9) RECTANGLE
Decleration
Void far rectangle(int left, int top, int right, int bottom)
Remarks
It draws a rectangle in the current line style, thickness and drawing color.
(left, top) is the upper left corner of the rectangle, and (right, bottom) is its lower right
corner.
Return value
None.
Start.
Input the two line end-points and store the left end-points in (x1,y1).
Load (x1,y1) into the frame buffer; that is, plot the first point put x=x1,y=y1.
Calculate dx=x2-x1 and dy=y2-y1,and obtain the initial value of decision parameter p
as:
p=(2dy-dx).
Close Graph.
Stop.
Start.
Input the two line end-points and store the left end-points in (x1,y1).
Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
x=x+xi.
y=y+yi.
Close Graph.
Stop.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void dda(float x1,float y1,float x2,float y2)
{
float dx,dy,x=x1,y=y1,m;
int i;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>=abs(dy))
m=abs(dx);
else m=abs(dy);
putpixel((int)x,(int)y,15);
for(i=1;i<=m;i++)
{
x=x+dx/m;
y=y+dy/m;
putpixel((int)x,(int)y,15);
}
}
void bress(float x1,float y1,float x2,float y2)
{
int x,y,end,inc=0,p,dx=abs(x2-x1),dy=abs(y2y1),c=0,current=0;
if(dx>dy)
{
p=2*dy-dx;
if(x1<x2)
{
x=x1;y=y1;end=x2;
if(y1<y2)inc=1;
if(y1>y2)inc=-1;
}
else
{
x=x2;y=y2;end=x1;
if(y2<y1)inc=1;
if(y2>y1)inc=-1;
}
while(x<=end)
{
putpixel(x,y,15);
if(p<0) p=p+2*dy;
else
{
y=y+inc;p=p+2*(dy-dx);
}
x++;
if(current==0&&c==10)
{
current=1;c=-1;
}
if(current==1&&c==6)
{
current=0;c=-1;
}
c++;
}
}
else
{
p=2*dx-dy;
if(y1<y2)
{
y=y1;x=x1;end=y2;
if(x1<x2)inc=1;
if(x1>x2)inc=-1;
}
else
{
y=y2;x=x2;end=y1;
if(x2<x1)inc=1;
if(x2>x1)inc=-1;
}
while(y<=end)
{
putpixel(x,y,15);
if(p<0)p=p+2*dx;
else
{
x=x+inc;p=p+2*(dx-dy);
}
y++;
if(current==0&&c==10)
{
current=1;c=-1;
}
if(current==1&&c==6)
{
current=0;c=-1;
}
c++;
}
}
}
void main()
{
float x1,x2,y1,y2;
int ch;
int gd=DETECT,gm=DETECT;
initgraph(&gd,&gm,"");
printf("Enter end points of line(x1,y1,x2,y2)");
scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
printf("Choose Algorithm(1-DDA 2-BRESENHAM)");
scanf("%d",&ch);
if(ch==1)
dda(x1,y1,x2,y2);
if(ch==2)
bress(x1,y1,x2,y2);
getch();
closegraph();
}
Start.
If (p>0),do p=p+2*(x-y)+1.
Otherwise p=p+2*x+1 and y is decremented simultaneously.
Then calculate the value of the function circlepoints() with p.arameters (x,y).
Close Graph.
Stop.
#
#
#
#
include<stdio.h>
include<graphics.h>
include<conio.h>
include<math.h>
:");
:");
:");
PROGRAM
TO DRAW A
CIRCLE USING MID-POINT ALGORITH
ARC
SECTOR
#include
#include
#include
#include
<graphics.h>
<conio.h>
<math.h>
<stdio.h>
#define PI 3.14
float start_angle,end_angle;
int x,y;
int DrawFigure( float theta )
{
if( theta >= start_angle && theta<= end_angle )
return 1;
return 0;
}
void Circlepoints(int x,int y,int xc,int yc)
{
float theta;
theta = atan( (float)y/x );
theta = theta * (180/M_PI);
if( DrawFigure(theta))
putpixel(xc+x,yc-y,WHITE);
if( DrawFigure(360-theta))
putpixel(xc+x,yc+y,WHITE);
if( DrawFigure(90-theta))
putpixel(xc+y,yc-x,WHITE);
if( DrawFigure(270+theta))
putpixel(xc+y,yc+x,WHITE);
if( DrawFigure(180-theta))
putpixel(xc-x,yc-y,WHITE);
if( DrawFigure(180+theta))
putpixel(xc-x,yc+y,WHITE);
if( DrawFigure(90+theta))
putpixel(xc-y,yc-x,WHITE);
if( DrawFigure(270-theta))
putpixel(xc-y,yc+x,WHITE);
}
void MidPointcircle(int xc,int yc,int rad)
{
float d = (5/4.0) - rad;
x=0,y=rad;
while(y>x)
{
if(d<0)
d += 2*x+3;
else
d+=(2*x)-(2*y)+5,y--;
x++;
Circlepoints(x,y,xc,yc);
delay(90);
}
}
void main()
{
int gd=DETECT,gm;
int radius,xc,yc,choice,temp;
float xstart,ystart,xend,yend;
initgraph(&gd,&gm,"..\\bgi");
do
{
clrscr();
cleardevice();
printf("\n Enter your choice\n");
printf("\n 1.Draw a Circle\n 2.Draw a Sector\n 3.Draw
an Arc\n 4.Exit\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("\n Enter the center:");
scanf("%d %d",&xc,&yc);
printf("\n Enter the radius:");
scanf("%d",&radius);
cleardevice();
start_angle=0,end_angle=360;
MidPointcircle(xc,yc,radius);
getch();
break;
case 2: printf("\n Enter the center:");
scanf("%d %d",&xc,&yc);
printf("\n Enter the radius:");
scanf("%d",&radius);
closegraph();
}
ALGORITHM TO DRAW AN ELLIPSE.
Start.
Get the input of radius of major and minor arc from the user.
increment x axis by 1.
If P < 0
new P = (P+( square of minor axis* square of major axis)+ square of major axis)
Else
new P = (P+( square of minor axis*x axis)-(2*square of major axis*y axis)+ square of
minor axis).
Decrement y by 1.
This will give us ellipse only across minor axis now to draw an ellipse across major axis
we proceed further.
If P1>0
Else
Increment x axis by 1.
Decrement y axis by 1.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void ellips(int x,int y);
void completellipse(int r,int g,int u,int v)
{
float s,k,e,f,x;
double p1,p2;
s=r;k=g;
e=(pow((s+.5),2));
f=(pow((k-1),2));
p2=((u*e)+(v*f)-(u*v));
ellips(s,k);
while(k>=0)
{
if(p2>0)
p2=(p2+v-(2*v*s));
else
{
p2=(p2+(2*u*(s+1))-(2*v*(k-1))+v);
s++;
}
k--;
ellips(s,k);
}
}
void main()
{
int gdriver=DETECT,gmode;
int a,b,x,y;
long u,v,p1;
initgraph(&gdriver,&gmode,"C:\\tc\\bgi::");
printf("\n enter the length of major axis:");
scanf("\t%d",&a);
printf("\n enter the length of minor axis:");
scanf("\t%d",&b);
x=0;
y=b;
u=pow(b,2);
v=pow(a,2);
p1=(u-(v*b)+(.25*v));
ellips(x,y);
while(2*(u*x)<=2*(v*y))
{
x++;
if(p1<0)
p1=(p1+(2*u*v)+v);
else
{
p1=(p1+(2*u*x)-(2*v*y)+u);
y--;
}
ellips(x,y);
}
completellipse(x,y,u,v);
getch();
closegraph();
}
void ellips(int x,int y)
{
putpixel(x+200,y+200,8);
putpixel(-x+200,y+200,8);
putpixel(x+200,-y+200,8);
putpixel(-x+200,-y+200,8);
}
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
printf"\n
scanf(%d%d,&a[0][0],&a[1][0]);
printf"\n
scanf(%d%d,&a[0][1],&a[1][1]);
printf"\n
scanf(%d%d,&a[0][2],&a[1][2]);
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
getch();
cleardevice();
printf"\n Enter angle of rotation:\n";
scanf(%d,&x);
b[0][0]=b[1][1]=cos((x*3.14)/180);
b[0][1]=-sin((x*3.14)/180);
b[1][0]=sin((x*3.14)/180);
b[2][2]=1;
b[2][0]=b[2][1]=b[0][2]=b[1][2]= 0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c[i][j]=0;
#include<iostream.h>
#include<conio.h>
#include<graphics.h>O
void main()
{
int gd=DETECT,gm;
initgraph(&gd, &gm,"");
cleardevice();
int x1,y1,x2,y2,x3,y3,x4,y4;
float sx,sy;
printf"Enter the first coordinates of triangle\n";
scanf(%d%d,&x1,&y1);
printf"Enter the second coordinates of triangle\n";
scanf(%d%d,&x2,&y2);
printf"Enter the third coordinates of triangle\n";
scanf(%d%d,&x3,&y3);
int poly[8]={x1,y1,x2,y2,x3,y3,x1,y1};
cleardevice();
drawpoly(4,poly);
getch();
printf"Enter the scaling factors\n";
scanf(%d%d,&sx,&sy);
x4=sx*x1-x1;
y4=sy*y1-y1;
x1=sx*x1-x4;
y1=sy*y1-y4;
x2=sx*x2-x4;
y2=sy*y2-y4;
x3=sx*x3-x4;
y3=sy*y3-y4;
poly[0]=x1;
poly[1]=y1;
poly[2]=x2;
poly[3]=y2;
poly[4]=x3;
poly[5]=y3;
poly[6]=x1;
poly[7]=y1;
getch();
cleardevice();
drawpoly(4,poly);
getch();
closegraph();
}
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,y1,x2,y2,x3,y3;
printf"\n
scanf(%d%d,&x1,&y1);
printf"\n
scanf(%d%d,&x2,&y2);
printf"\n
scanf(%d%d,&x3,&y3);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
cleardevice();
scanf(%d%d,&x,&y);
x1-=x;
y1-=y;
x2-=x;
y2-=y;
x3-=x;
y3-=y;
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
closegraph();
}
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
printf" OUTPUT");
printf"Enter the coordinates of point");
scanf(%d%d,&x2,&y2);
putpixel(x2,y2,2);
printf"Enter the coordinates of point around which rotation is
done");
scanf(%d%d,&h,&k);
putpixel(h,k,2);
printf"Enter the angle for rotation");
scanf(%d,&t);
cleardevice();
x1=(h*cos(t))-(k*sin(t));
y1=(h*sin(t))+(k*cos(t));
x3=x1+x2-h;
y3=y1+y2-k;
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
void main()
{
clrscr();
int gm,gd=DETECT;
initgraph(&gd,&gm,"");
int h,k,x1,y1,x2,y2,x3,y3;
float t;
printf" OUTPUT");
printf"Enter the coordinates of point");
scanf(%d%d,&x2,&y2);
putpixel(x2,y2,2);
printf"Enter the angle for rotation");
scanf(%d,&t);
cleardevice();
x1=int(x2*cos(t*3.14/180))-(y2*sin(t*3.14/180));
y1=int(x2*sin(t*3.14/180))+(y2*cos(t*3.14/180));
printf"Point after rotation is:";
putpixel(x1,y1,2);
getch();
closegraph();
PROGRAM TO REFLECT A TRIANGLE
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
#include<math.h>
void main()
{
clrscr();
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int x,y,x1,a[3][3];
double b[3][3],c[3][3];
printf"\n
scanf(%d%d,&a[0][0],&a[1][0]);
printf"\n
scanf(%d%d,&a[0][1],&a[1][1]);
printf"\n
scanf(%d%d,&a[0][2],&a[1][2]);
printf"\n Enter 1. for reflection in x-axis:\n";
printf"\n Enter 2. for reflection in y-axis:\n";
printf"\n Enter 3. for reflection in both the axis:\n";
scanf(%d,&x);
cleardevice();
line(320,0,320,479);
line(0,240,639,240);
line(a[0][0],a[1][0],a[0][1],a[1][1]);
line(a[0][1],a[1][1],a[0][2],a[1][2]);
line(a[0][0],a[1][0],a[0][2],a[1][2]);
switch(x)
{
case 1:b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
case 2:b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
case 3: b[0][0]=640-a[0][0];
b[0][1]=640-a[0][1];
b[0][2]=640-a[0][2];
b[1][0]=a[1][0];
b[1][1]=a[1][1];
b[1][2]=a[1][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
b[1][0]=480-a[1][0];
b[1][1]=480-a[1][1];
b[1][2]=480-a[1][2];
b[0][0]=a[0][0];
b[0][1]=a[0][1];
b[0][2]=a[0][2];
line(320,0,320,479);
line(0,240,639,240);
line(b[0][0],b[1][0],b[0][1],b[1][1]);
line(b[0][1],b[1][1],b[0][2],b[1][2]);
line(b[0][0],b[1][0],b[0][2],b[1][2]);
getch();
break;
}
getch();
closegraph();
}
Start.
Get the input of window co ordinates from the user and draw a window.
Get the input of line co ordinates from user and draw the line.
Calculate the region code of each end point of line using relation given in steps 6 to
step
Let (x,y) be the co ordinates of end point of line and (xmin,ymin), (xmax,ymax) be co
ordinates of world window
If y ymax = +ve
If ymin y = +ve
Region code = 1.
If x xmax = +ve
Region code = 1.
If xmin x = +ve
Else.
#include<stdio.h>
#include<graphics.h>
//#include<process.h>
void main()
{
int gd=DETECT, gm;
float i,xmax,ymax,xmin,ymin,x11,y11,x22,y22,m;
float a[4],b[4],c[4],x1,y1;
clrscr();
initgraph(&gd,&gm,"c:\\tc\\bgi");
printf("\nEnter the bottom-left coordinate of viewport: ");
scanf("%f %f",&xmin,&ymin);
printf("\nEnter the top-right coordinate of viewport: ");
scanf("%f %f",&xmax,&ymax);
rectangle(xmin,ymin,xmax,ymax);
printf("\nEnter the coordinates of 1st end point of line: ");
scanf("%f %f",&x11,&y11);
printf("\nEnter the coordinates of 2nd endpoint of line: ");
scanf("%f %f",&x22,&y22);
line(x11,y11,x22,y22);
//initgraph(&gd,&gm,"c:\\tc\\bin");
for(i=0;i<4;i++)
{
a[i]=0;
b[i]=0;
}
m=(y22-y11)/(x22-x11);
if(x11<xmin) a[3]=1;
if(x11>xmax) a[2]=1;
if(y11<ymin) a[1]=1;
if(y11>ymax) a[0]=1;
if(x22<xmin) b[3]=1;
if(x22>xmax) b[2]=1;
if(y22<ymin) b[1]=1;
if(y22>ymax) b[0]=1;
printf("\nRegion code of 1st pt ");
for(i=0;i<4;i++)
{printf("%f",a[i]);}
printf("\nRegion code of 2nd pt ");
for(i=0;i<4;i++)
{printf("%f",b[i]);}
printf("\nAnding : ");
for(i=0;i<4;i++)
{c[i]=a[i]&&b[i];}
for(i=0;i<4;i++)
printf("%f",c[i]);
getch();
if((c[0]==0)&&(c[1]==0)&&(c[2]==0)&&(c[3]==0))
{
if((a[0]==0)&&(a[1]==0)&&(a[2]==0)&&(a[3]==0)&&
(b[0]==0)&&(b[1]==0)&&(b[2]==0)&&(b[3]==0))
{
clrscr();
clearviewport();
printf("\nThe line is totally visible\nand not a clipping
candidate");
rectangle(xmin,ymin,xmax,ymax);
line(x11,y11,x22,y22);
getch();
}
else
{
clrscr();
clearviewport();
printf("\nLine is partially visible");
rectangle(xmin,ymin,xmax,ymax);
line(x11,y11,x22,y22);
getch();
if((a[0]==0)&&(a[1]==1))
{
x1=x11+(ymin-y11)/m;
x11=x1;
y11=ymin;
}
else if((b[0]==0)&&(b[1]==1))
{
x1=x22+(ymin-y22)/m;
x22=x1;
y22=ymin;
}
if((a[0]==1)&&(a[1]==0))
{
x1=x11+(ymax-y11)/m;
x11=x1; y11=ymax;
}
else if((b[0]==1)&&(b[1]==0))
{
x1=x22+(ymax-y22)/m;
x22=x1; y22=ymax;
}
if((a[2]==0)&&(a[3]==1))
{
y1=y11+m*(xmin-x11);
y11=y1; x11=xmin;
}
else if((b[2]==0)&&(b[3]==1))
{
y1=y22+m*(xmin-x22);
y22=y1;
x22=xmin;
}
if((a[2]==1)&&(a[3]==0))
{
y1=y11+m*(xmax-x11);
y11=y1; x11=xmax;
}
else if((b[2]==1)&&(b[3]==0))
{
y1=y22+m*(xmax-x22);
y22=y1;
x22=xmax;
}
clrscr();
clearviewport();
printf("\nAfter clippling:");
rectangle(xmin,ymin,xmax,ymax);
line(x11,y11,x22,y22);
getch();
}
}
else
{
clrscr();
clearviewport();
printf("\nLine is invisible");
rectangle(xmin,ymin,xmax,ymax);
getch();
}
closegraph();
getch();
}
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<dos.h>
#include<process.h>
void main()
{
int graphdriver=DETECT,graphmode;
initgraph(&graphdriver,&graphmode,"...\\bgi");
int p=1,x;
int a[12]={100,100,150,150,200,100,200,200,100,200,100,100};
drawpoly(6,a);
for(int i=100;i<200;i++)
{
p=1;
for(int j=100;j<=200;j++)
{
x=getpixel(j,i);
for(int d=0;d<11;d++)
{
if(j==a[d]&&i==a[d+1] )
break;
else
{
if(x>0&&d==10)
p++;
if(p%2==0)
putpixel(j,i,4);
}}}}
getch();
closegraph();
}
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<dos.h>
#include<math.h>
#include<graphics.h>
typedef struct
{
float x;
float y;
}PTT;
void suthhodge();
void left(PTT,PTT *,PTT *);
void right(PTT,PTT *,PTT *);
void top(PTT,PTT *,PTT *);
void bottom(PTT,PTT *,PTT *);
void drawpolygon(PTT*,int);
int n=0;
void main()
{
suthhodge();
getch();
}
void suthhodge()
{
int i,j,gd,gm;
PTT d,p1,p2,p[20],pi1,pi2,pp[20];
detectgraph(&gd,&gm);
initgraph(&gd,&gm,"");
// printfendl<<"Enter coordinates (left,top) of point1 : ";
printf("enter coordinates of point 1");
//scanf(p1.x,p1.y;
scanf("%f%f",&p1.x,p1.y);
// printfendl<<"Enter coordinates (left,top) of point2 : ";
printf("enter coordinates of point 2");
//
scanf(p2.x,p2.y;
scanf("%f%f",&p2.x,&p2.y);
//
printfendl<<
printf("Enter the number of vertex : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
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;
}
void right(PTT p2,PTT p[20],PTT 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;
}
void top(PTT p1,PTT p[20],PTT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
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[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[i].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;
}
void bottom(PTT p2,PTT p[20],PTT pp[20])
{
int i,j=0;
for(i=0;i<n;i++)
{
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.yp[i].y)+p[i].x;
else
pp[j].x = p[i].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.yp[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;
}
void drawpolygon(PTT 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);
}
#include<iostream.h>
#include<dos.h>
#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<graphics.h>
#include<process.h>
int gd=DETECT,gm;
double x1,x2,y1,y2;
void show_message()
{
char *mess[]={"-","=","[","
","3","D","-","T","r","a","n","s",
"f","o","r","m","a","t","i","o","n","
","]","=","-"};
int xx=28,xxx=52,i,j;
_setcursortype(_NOCURSOR);
for(i=0,j=24;i<15,j>=12;i++,j--)
{
gotoxy(xx,1);
printfmess[i];
xx++;
gotoxy(xxx,1);
printfmess[j];
xxx--;
delay(50);
}
_setcursortype(_NORMALCURSOR);
}
void draw_cube(double edge[20][3])
{
initgraph(&gd,&gm,"..\bgi");
int i;
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 scale(double edge[20][3])
{
double a,b,c;
int i;
printf"
" Enter The Scaling Factors ":=";
scanf(a,b,c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0]*a;
edge[i][1]=edge[i][1]*b;
edge[i][2]=edge[i][2]*c;
}
draw_cube(edge);
closegraph();
}
void translate(double edge[20][3])
{
int a,b,c;
int i;
printf"
" Enter The Translation Factors ":=";
scanf(a,b,c;
initgraph(&gd,&gm,"..\bgi");
clearviewport();
for(i=0;i<20;i++)
{
edge[i][0]+=a;
edge[i][0]+=b;
edge[i][0]+=c;
}
draw_cube(edge);
closegraph();
}
void rotate(double edge[20][3])
{
int ch;
int i;
double temp,theta,temp1;
clrscr();
printf"
-=[ Rotation About ]=-";
printf"
1:==>" X-Axis "";
printf"
2:==>" Y-Axis "";
printf"
3:==>" Z-Axis "";
printf"
" Enter Your Choice ":=";
scanf(ch;
switch(ch)
{
case 1:
printf"
" Enter The Angle ":=";
scanf(theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0];
temp=edge[i][1];
temp1=edge[i][2];
edge[i][1]=temp*cos(theta)-temp1*sin(theta);
edge[i][2]=temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
case 2:
printf"
" Enter The Angle ":=";
scanf(theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
temp=edge[i][0];
temp1=edge[i][2];
edge[i][0]=temp*cos(theta)+temp1*sin(theta);
edge[i][2]=-temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
case 3:
printf"
" Enter The Angle ":=";
scanf(theta;
theta=(theta*3.14)/180;
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
temp=edge[i][0];
temp1=edge[i][1];
edge[i][0]=temp*cos(theta)-temp1*sin(theta);
edge[i][1]=temp*sin(theta)+temp1*cos(theta);
}
draw_cube(edge);
break;
}
}
void reflect(double edge[20][3])
{
int ch;
int i;
clrscr();
printf"
-=[ Reflection About ]=-";
printf"
1:==>" X-Axis "";
printf"
2:==>" Y-Axis "";
printf"
3:==>" Z-Axis "";
printf"
" Enter Your Choice ":=";
scanf(ch;
switch(ch)
{
case 1:
for(i=0;i<20;i++)
{
edge[i][0]=edge[i][0];
edge[i][1]=-edge[i][1];
edge[i][2]=-edge[i][2];
}
draw_cube(edge);
break;
case 2:
for(i=0;i<20;i++)
{
edge[i][1]=edge[i][1];
edge[i][0]=-edge[i][0];
edge[i][2]=-edge[i][2];
}
draw_cube(edge);
break;
case 3:
for(i=0;i<20;i++)
{
edge[i][2]=edge[i][2];
edge[i][0]=-edge[i][0];
edge[i][1]=-edge[i][1];
}
draw_cube(edge);
break;
}
}
void perspect(double edge[20][3])
{
int ch;
int i;
double p,q,r;
clrscr();
printf"
-=[ Perspective Projection About ]=-";
printf"
1:==>" X-Axis "";
printf"
2:==>" Y-Axis "";
printf"
3:==>" Z-Axis "";
printf"
" Enter Your Choice ":=";
scanf(ch;
switch(ch)
{
case 1:
printf"
" Enter P ":=";
scanf(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);
}
draw_cube(edge);
break;
case 2:
printf"
" Enter Q ":=";
scanf(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);
}
draw_cube(edge);
break;
case 3:
printf"
" Enter R ":=";
scanf(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);
}
draw_cube(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,100,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,100,
100,0,100
};
while(1)
{
clrscr();
show_message();
printf"
1:==>" Draw Cube "";
printf"
2:==>" Scaling "";
printf"
3:==>" Rotation "";
printf"
4:==>" Reflection "";
printf"
5:==>" Translation "";
printf"
6:==>" Perspective Projection "";
printf"
7:==>" Exit "";
printf"
" Enter Your Choice ":=";
scanf(choice;
switch(choice)
{
case 1:
draw_cube(edge);
break;
case 2:
scale(edge);
break;
case 3:
rotate(edge);
break;
case 4:
reflect(edge);
break;
case 5:
translate(edge);
break;
case 6:
perspect(edge);
break;
case 7:
exit(0);
default:
printf"
a" Press A Valid Key...!!! "";
getch();
break;
}
closegraph();
}
}