You are on page 1of 61

Computer Graphics

Lab Manual

Syllabus as per M. D. University


Session: 2013-2014
B.TECH (CSE ) SEMESTER V
SUBJECT: COMPUTER GRAPHICS LAB
SUBJECT CODE: CSE-309F
1. Write a program for 2D line drawing as Raster Graphics Display.
2. Write a program for circle drawing as Raster Graphics Display.
3. Write a program for polygon filling as Raster Graphics Display
4. Write a program for line clipping.
5. Write a program for polygon clipping.
6. Write a program for displaying 3D objects as 2D display using perspective
transformation.
7. Write a program for rotation of a 3D object about arbitrary axis.
8. Write a program for Hidden surface removal from a 3D object.
Note: At least 5 to 10 more exercises to be given by the teacher concerned.

CSE-309 E Computer Graphics Lab.


.
Section A
List of programs to be developed in Lab
1.

Write a program for 2D line drawing as Raster Graphics Display.

2.

Write a program for circle drawing as Raster Graphics Display.

3.

Write a program to draw an ellipse using Mid Point Algorithm.

4.

Write a program to draw a circle using Midpoint algorithm. Modify the same for
drawing an arc and sector.

5.

Write a program to rotate a point about origin.

6.

Write a program to rotate a triangle about origin.

7.

Write a program to scale the triangle.

8.

Write a program to translate a triangle.

9.

Write a program to reflect a triangle.

10.

Write a program for polygon filling as Raster Graphics Display

11.

Write a program for line clipping.

12.

Write a program for polygon clipping.

13.

Write a program for displaying 3D objects as 2D display using perspective


transformation.

14.

Write a program for rotation of a 3D object about arbitrary axis.

15.

Write a program for Hidden surface removal from a 3D object.

B.TECH (CSE & IT) SEMESTER V


LAB PLAN
SUBJECT (CSE -309 E):- COMPUTER GRAPHICS
Session : 2010-11
15

S. No.

Total No of Labs :

Programs List

No.
of
Labs

Write a program for 2D line drawing using DDA algorithm.

Write a program to draw a line using Bresenhams Algo.

Write a program for circle drawing as Raster Graphics Display.

Write a program to draw a circle using Midpoint algo. Modify the


same for drawing an arc and sector

Write a program to rotate a point about origin.

Write a program to rotate a triangle about origin.

Write a program to scale the triangle

Write a program to translate the triangle

Write a program to reflect the triangle

10

Write a program for polygon filling as Raster Graphics Display

11

Write a program for line clipping.

12

Write a program for polygon clipping.

13

Write a program for displaying 3D objects as 2D display using


perspective transformation.

Write a program for rotation of a 3D object about arbitrary axis

Write a program for Hidden surface removal from a 3D object.

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

October Third Week


October Fourth Week
November First Week

Section - C
Project Allocation Table
Project No.

Student Roll-No

1
2
3
4

1---8
9--16
17-24
25-28

B.TECH (CSE ) SEMESTER V


TEACHING PLAN
SUBJECT (CSE -309 E):- COMPUTER GRAPHICS

BASIC GRAPHICS FUNCTION


1) INITGRAPH

Initializes the graphics system.

Declaration

Void far initgraph(int far *graphdriver)

Remarks

To start the graphic system, you must first call initgraph.

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

Getpixel gets the color of a specified pixel.

Putpixel places a pixel at a specified point.

Decleration

Unsigned far getpixel(int x, int y)

Void far putpixel(int x, int y, int color)

Remarks

Getpixel gets the color of the pixel located at (x,y);

Putpixel plots a point in the color defined at (x, y).

Return value

Getpixel returns the color of the given pixel.

Putpixel does not return.

3) CLOSE GRAPH

Shuts down the graphic system.

Decleration

Void far closegraph(void);

Remarks

Close graph deallocates all memory allocated by the graphic system.

It then restores the screen to the mode it was in before you called initgraph.

Return value

None.

4) ARC, CIRCLE, PIESLICE

arc draws a circular arc.

Circle draws a circle

Pieslice draws and fills a circular pieslice

Decleration

Void far arc(int x, int y, int stangle, int end_angle, int radius);

Void far circle(int x, int y, int radius);

Void far pieslice(int x, int y, int stangle, int end_angle, int radius);

Remarks

Arc draws a circular arc in the current drawing color

Circle draws a circle in the current drawing color

Pieslice draws a pieslice in the current drawing color, then fills it using the current fill
pattern and fill color.

5) ELLIPSE, FILLELIPSE, SECTOR

Ellipse draws an elliptical arc.

Fillellipse draws and fills ellipse.

Sector draws and fills an elliptical pie slice.

Decleration

Void far ellipse(int x, int y, int stangle, int end_angle, int xradius, int yradius)

Void far fillellipse(int x, int y, int xradius, int yradius)

Void farsectoe(int x, int y, int stangle, int end_angle, int xradius, int yradius)

Remarks

Ellipse draws an elliptical arc in the current drawing color.

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

Flood-fills a bounded region.

Decleration

Void far floodfill(int x, int y, int border)

Remarks

Floodfills an enclosed area on bitmap device.

The area bounded by the color border is flooded with the current fill pattern and fill
color.

(x,y) is a seed point

If the seed is within an enclosed area, the inside will be filled.

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.

Floodfill doesnot work with the IBM-8514 driver.

Return value

If an error occurs while flooding a region, graph result returns 1.

7) GETCOLOR, SETCOLOR

Getcolor returns the current drawing color.

Setcolor returns the current drawing color.

Decleration

Int far getcolor(void);

Void far setcolor(int color)

Remarks

Getcolor returns the current drawing color.

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.

8) LINE, LINEREL, LINETO

Line draws a line between two specified pints.

Onerel draws a line relative distance from current position(CP).

Linrto draws a line from the current position (CP) to(x,y).

Decleration

Void far lineto(int x, int y)

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

Draws a rectangle in graphics mode.

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.

Note: Algorithms of some simple programs have also been included.

Bresenhams algorithm for line drawing.

Start.

Declare variables x,y,x1,y1,x2,y2,p,dx,dy and also declare gdriver=DETECT,gmode.

Initialize the graphic mode with the path location in TC folder.

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).

Starting from first point (x,y) perform the following test:

Repeat step 9 while(x<=x2).

If p<0,next point is (x+1,y) and p=(p+2dy).

Otherwise, the next point to plot is (x+1,y+1) and p=(p+2dy-2dx).

Place pixels using putpixel at points (x,y) in specified colour.

Close Graph.

Stop.

DDA Algorithm for line drawing.

Start.

Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare


gdriver=DETECT,gmode.

Initialise the graphic mode with the path location in TC folder.

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.

If abs(dx) > abs(dy), do s=abs(dx).


Otherwise s= abs(dy).

Then xi=dx/s and yi=dy/s.

Start from k=0 and continuing till k<s,the points will be


o

x=x+xi.

y=y+yi.

Place pixels using putpixel at points (x,y) in specified colour.

Close Graph.

Stop.

WAP TO DRAW A LINE USING


MID POINT ALGORITHM
BRESENHAMS ALGORITHM.

#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();
}

BRESENHAMS ALGORITHM TO DRAW A CIRCLE.

Start.

Declare variables x,y,p and also declare gdriver=DETECT,gmode.

Initialise the graphic mode with the path location in TC folder.

Input the radius of the circle r.

Load x-0,y=r,initial decision parameter p=1-r.so the first point is (0,r).

Repeat Step 7 while (x<y) and increment x-value simultaneously.

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).

Place pixels using putpixel at points (x+300,y+300) in specified colour in circlepoints()


function shifting the origin to 300 on both x-axis and y-axis.

Close Graph.

Stop.

PROGRAM TO DRAW A CIRCLE USING BRESENHAMS ALGORITHM.

#
#
#
#

include<stdio.h>
include<graphics.h>
include<conio.h>
include<math.h>

void bresenham_circle(const int h,const int k,const int r)


{
//
cleardevice();
int x=0,y=r,p=(3-(2*r));
cleardevice();
line(320,1,320,480);
line(1,240,640,240);
do
{
putpixel((h+x),(k+y),15);
putpixel((h+y),(k+x),15);
putpixel((h+y),(k-x),15);
putpixel((h+x),(k-y),15);
putpixel((h-x),(k-y),15);
putpixel((h-y),(k-x),15);
putpixel((h-y),(k+x),15);
putpixel((h-x),(k+y),15);
x++;
if(p<0)
p+=((4*x)+6);
else
{
y--;
p+=((4*(x-y))+10);
}
}
while(x<=y);
}
void main(void)
{
int driver=VGA,mode=VGAHI,h,k,r;
initgraph(&driver,&mode,"..\\Bgi");
printf("\n ENTER THE VALUE OF [X- COORDINATE]
scanf("%d",&h);
printf("\n ENTER THE VALUE OF [Y- COORDINATE]
scanf("%d",&k);
printf("\n ENTER THE VALUE OF RADIUS
scanf("%d",&r);
bresenham_circle(320+h,240-k,r); getch();}

:");
:");
:");

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);

printf("\n Enter the start_angle:");


scanf("%f",&start_angle);
printf("\n Enter the end_angle:");
scanf("%f",&end_angle);
cleardevice();
if(start_angle>end_angle)
{
temp=start_angle;
start_angle=end_angle;
end_angle=temp;
}
MidPointcircle(xc,yc,radius);
xstart=xc+radius*cos(PI/180*start_angle);
ystart=ycradius*sin(PI/180*start_angle);
xend=xc+radius*cos(PI/180*end_angle);
yend=yc-radius*sin(PI/180*end_angle);
line(xc,yc,xstart,ystart);
line(xc,yc,xend,yend);
getch();
break;
case 3: printf("\n Enter the center:");
scanf("%d %d",&xc,&yc);
printf("\n Enter the radius:");
scanf("%d",&radius);
printf("\n Enter the start_angle:");
scanf("%f",&start_angle);
printf("\n Enter the end_angle:");
scanf("%f",&end_angle);
cleardevice();
if(start_angle>end_angle)
{
temp=start_angle;
start_angle=end_angle;
end_angle=temp;
}
MidPointcircle(xc,yc,radius);
getch();
break;
case 4:
}
}while(choice!=4);

closegraph();

}
ALGORITHM TO DRAW AN ELLIPSE.

Start.

Initialize the graphic system using initgraph function.

Get the input of radius of major and minor arc from the user.

Store the values of major and minor arc in an another variable.

Square the values of major and minor arc.

Calculate decision parameter P = (square of minor axis (square of major axis*minor


axis) + (0.25* square of major axis).

Put the pixels symmetrically at = (0, length of minor axis).

while (2*(square of minor axis*x)<=2*(square of major axis*y)), repeat steps 9 to step


17.

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.

End of step 10 if else structure.

Plot symmetric points of ellipse in each quadrant.

End of step 8 loop.

This will give us ellipse only across minor axis now to draw an ellipse across major axis
we proceed further.

Get last point of ellipse in 1st quadrant.

Initialize e = square of (x axis+.5)

Initialize f = square of (y axis-1).

Decision parameter P1 = ((square of minor axis*e)+( square of major axis*f)-( square


of minor axis* square of major axis).

While y axis != 0 repeat steps 24 to step 32.

If P1>0

New P1 = (P1+ square of major axis-(2* square of major axis*x axis)).

Else

New P1 = (P1+(2*square of minor axis*(x axis+1))-(2* square of major axis*(y axis-1))


+square of major axis).

Increment x axis by 1.

End of step 25 if else structure

Decrement y axis by 1.

Plot symmetric point in all quadrants

End of step 23 while loop.

Close the graphic system.Stop.

PROGRAM TO DRAW AN ELLIPSE USING MID-POINT ELLIPSE DRAWING


ALGORITHM.

#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);
}

PROGRAM TO ROTATE A TRIANGLE ABOUT ORIGIN.

#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

Enter Ist coordinates of triangle:";

scanf(%d%d,&a[0][0],&a[1][0]);
printf"\n

Enter 2nd coordinates of triangle:";

scanf(%d%d,&a[0][1],&a[1][1]);
printf"\n

Enter 3rd coordinates of triangle:";

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;

for (int k=0; k<3;k++)


{
c[i][j]+=a[i][k]*b[k][j];
}
x1=(c[i][j]+0.5);
a[i][j]=x1;
}
}
printf"\n Triangle after rotation is:\n" ;
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();
closegraph();
}

PROGRAM TO SCALE THE TRIANGLE

#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();
}

PROGRAM TO TRANSLATE 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,y1,x2,y2,x3,y3;
printf"\n

Enter Ist coordinates of triangle:";

scanf(%d%d,&x1,&y1);
printf"\n

Enter 2nd coordinates of triangle:";

scanf(%d%d,&x2,&y2);
printf"\n

Enter 3rd coordinates of triangle:";

scanf(%d%d,&x3,&y3);
cleardevice();
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x1,y1,x3,y3);
getch();
cleardevice();

printf"\n Enter translatio factors :\n";

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();
}

PROGRAM TO ROTATE A POINT ABOUT A POINT

#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;

printf"Point after rotation is:";


putpixel(x3,y3,2);
getch();
closegraph();
}

PROGRAM TO ROTATE A POINT ABOUT ORIGIN

#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

Enter Ist coordinates of triangle:";

scanf(%d%d,&a[0][0],&a[1][0]);
printf"\n

Enter 2nd coordinates of triangle:";

scanf(%d%d,&a[0][1],&a[1][1]);
printf"\n

Enter 3rd coordinates of triangle:";

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();
}

ALGORITHM TO CLIP A LINE.

Start.

Initialize the graphic system using initgraph function.

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

MSB region code = 1.

Else MSB region code = 0.

If ymin y = +ve

Region code = 1.

Else Region code = 0.

If x xmax = +ve

Region code = 1.

Else Region code = 0.

If xmin x = +ve

LSB Region code = 1.

Else LSB Region code = 0.

Calculate region code of both end points.

Logically and both region code.

If Logically anded result is = 0

Line is not a clipping candidate.

Else.

Line is a clipping candidate.

Calculate slope of line using formula slope=(y2-y1)/(x2-x1).

If line is to be horizontally clipped.

New y = ymin or ymax.

New x = x1 + ((new y - y1)/slope).

If line is to be vertically clipped.

New x = xmin or xmax.

New y = y1+slope*(new x x1).

Clip the lines from these intersection points.

Display the new line.

Close the graphic system. Stop.

PROGRAM TO SHOW LINE CLIPPING.

#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();
}

PROGRAM TO FILL A POLYGON

#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();
}

PROGRAM TO CLIP A POLYGON

#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++)
{

printf("Enter coordinates 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);
drawpolygon(p,n);
getch();
closegraph();
}
void left(PTT p1,PTT p[20],PTT pp[20])
{
int 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.xp[i].x)+p[i].y;
else
pp[j].y = p[i].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[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[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);
}

PROGRAM TO PERFORM LISTED FUNCTIONS ON 3-D OBJECTS


DRAW CUBE
SCALING
ROTATION
REFLECTION
TRANSLATION
PERSPECTIVE PROJECTION

#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();
}
}

You might also like