Professional Documents
Culture Documents
(Instruction Manual)
ETAT- 451
VII Sem
B.Tech (MAE)
1
Maharaja Agrasen Institute of Technology B.Tech(MAE)
VII Semester 2
Maharaja Agrasen Institute of Technology B.Tech(MAE)
1. INTRODUCTION TO THE LAB
In this CAD Lab, the students are exposed to C/C++ coding and
programming, which can be further utilized in developing advanced softwares
like AutoCAD, Pro-E,CATIA, SolidWorks etc.
The students are provided wirh some sample codings, which can be used as a
help. Whenever the students draw a line, circle, ellipse etc, each time one of
these algorithms (DDA, Bresenhem, Mid-point etc.) runs in the background.
The students are however supposed to write their own code, usinjg these
concepts.
Typically, the term computer graphics refers to several different things: the
representation and manipulation of image data by a computer the various
technologies used to create and manipulate images the images so produced
and The subfield of computer science which studies methods for digitally
synthesizing and manipulating visual content
VII Semester 3
Maharaja Agrasen Institute of Technology B.Tech(MAE)
2. LAB REQUIREMENTS
Hardware Requirements:
Computer systems with 128MB RAM, VGA supported computer
graphics.
Software Requirements:
1. C/C++ SOFTWARE
2. SCILAB
3. MATLAB
VII Semester 4
Maharaja Agrasen Institute of Technology B.Tech(MAE)
3.LIST OF EXPERIMENTS
(As prescribed by G.G.S.I.P.U)
Paper Code: ETAT-451 Paper: Computer Aided Design LAB
VII Semester 5
Maharaja Agrasen Institute of Technology B.Tech(MAE)
5. FORMAT OF THE LAB RECORD
The front page of the lab record should have a cover page as displayed below.
VII Semester 6
Maharaja Agrasen Institute of Technology B.Tech(MAE)
The second page in the record should be the index as displayed below.
CAD LAB
PRACTICAL RECORD
PRACTICAL DETAILS:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
VII Semester 7
Maharaja Agrasen Institute of Technology B.Tech(MAE)
1. Regularity: 25
Performing program in each turn of the lab
Attendance of the lab
File
2. Viva Voice: 10
3. Project: 5
NOTE: For the regularity, marks are awarded to the student out of 10 for each
experiment performed in the lab and at the end the average marks are
giving out of 25.
Total Marks: 60
Division of 60 marks is as follows
NOTE:
Internal marks + External marks = Total marks given to the students
(40 marks) (60 marks) (100 marks)
Experiments given to perform can be from any section of the lab.
VII Semester 8
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment 0
Aim: Study of basic graphics functions defined in graphics.h.
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
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.
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.
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.
VII Semester 11
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment-1
Aim: To implement Digital differential analyzer (DDA) Algorithm for drawing
a line segment between two given end points A (x1, y1) and B(x2, y2).
Algorithm
1. Start.
2. Declare variables x,y,x1,y1,x2,y2,k,dx,dy,s,xi,yi and also declare
gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the two line end-points and store the left end-points in (x1,y1).
5. Load (x1,y1) into the frame buffer;that is,plot the first point.put x=x1,y=y1.
Also load second point as x=x2, y=y2.
6. Calculate dx=x2-x1 and dy=y2-y1.
7. If abs(dx) > abs(dy), do s=abs(dx). Otherwise s= abs(dy).
8. Then xi=dx/s and yi=dy/s.
9. Start from k=0 and continuing till k<s,the points will be
a. x=x+xi.
b. y=y+yi.
10. Place pixels using putpixel at points (x,y) in specified colour.
11. Close Graph.
12. Stop.
VII Semester 12
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Program to draw a straight line using DDA algorithm
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include <graphics.h>
#include <conio.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
clrscr();
float x1,x2,y1,y2, xd,yd,x,y,len,a,b;
cout<<"Enter first point coordinates in pixels:";
cin>>x1>>y1;
cout<<"Enter second point coordinates in pixels:";
cin>>x2>>y2;
a=x2-x1;
b=y2-y1;
if(abs(a)>abs(b))
{
len=a;
}
else
{
len=b;
}
xd=a/len;
yd=b/len;
x=x1+0.5;
y=y1+0.5;
for(int i=1;i<=len;i++)
{
putpixel(x,y,RED);
x=x+xd;
y=y+yd;
}
getch();
}
VII Semester 13
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment 2
Aim: To implement Bresenhams Mid-Point Circle drawing algorithm for
drawing a circle with a given center of circle P(xc, yc) and radius r.
Description:
Bresenhams algorithm extensively uses symmetry of the circle. This is fact that if
8 part of the circle is generated then rest 8 part of the circle can be generated
1 7
VI III Octant
Octant P3 ( y, x)
P6 ( y, x)
P5 ( x, y) P4 ( x, y)
V Octant IV y = - x line
Octant
In this method, at first, the topmost point, i.e., (0, r ) is plotted if equation of
circle is x 2 y 2 r 2 . However, for circle with centre position ( xc , yc ) and radius r , the
algorithm can be developed for the calculation of pixel positions around the circle
path, centered at origin (0,0) . Thereafter, each evaluated pixel position ( x, y) is
shifted to its proper screen position by adding xc to x and yc to y along circle
section in the first octant (from x 0 line to x y line; alternatively, when slope
varies from m 0 to m 1 ). Therefore, we can go in the positive x -direction by unit
steps over the first octant. When x coordinate of a point increases, corresponding y
coordinate decreases.
Similar to other Bresenhams algorithms, a decision parameter Pk determines
the two possible pixel positions, which is closer to the actual circle boundary at each
step in the first octant. Pixel positions in the other seven octants are determined
VII Semester 14
Maharaja Agrasen Institute of Technology B.Tech(MAE)
using the symmetry of circle. Let circle function itself is the decision parameter in
the Bresenhams algorithm. Thus, the equation of circle in implicit form is
< 0, if point ( x, y) lies inside the circle boundary
Pk f circle ( x, y) x y r
2 2 2
= 0, if point ( x, y) lies on the circle boundary
> 0, if point ( x, y) lies outside the circle boundary
Suppose, Fig. 3.17 shows the plot for the current pixel coordinates ( xk , y k ) . Now, it
is to decide that at the sampling position xk 1 , whether the pixel yk or one at yk 1 is
closer to the circle boundary. Alternatively, at position xk 1 , we have to select either
pixel coordinate position ( xk 1 , y k ) as choice I or pixel position ( xk 1 , yk 1 ) as choice II,
depending upon position of the midpoint between two adjacent pixels yk and yk 1
from the circle boundary. The decision parameter Pk at the midpoint between these
two pixels will be
y k y k 1
Pk f circle ( xk 1 , ) f circle ( xk 1, y k 12 ) (1)
2
= ( xk 1) 2 ( y k 12 ) 2 r 2
Choice I
Scan line
(0, r )
yk Mid point
y24
k 1
Circle
22 boundar
y k 2 y
24
20 Choice II
22
24
20
22
20
xk xk 1 xk 2
Pixel No.
VII Semester 15
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Pk 1 f circle ( xk 1 1, yk 1 12 )
= {( xk 1) 2 1}2 { y k 1 12 }2 r 2
= ( xk 1) 2 1 2.( xk 1) y k21 14 y k 1 r 2
= {( xk 1) 2 ( y k2 14 y k ) r 2 } 1 2.( xk 1) y k21 y k 1 y k2 y k
= {( xk 1) 2 ( y k2 14 y k ) r 2 } 1 2.( xk 1) y k21 y k 1 y k2 y k
or Pk 1 Pk 2.(xk 1) ( yk21 yk2 ) ( yk 1 yk ) 1
(2)
where yk 1 yk (if Pk 0 , i.e., negative) when pixel ( xk 1 , y k ) lies inside the circle
boundary; therefore, Pk 1 Pk 2.xk 1 1
(3)
and yk 1 yk 1 (if Pk 0 , i.e., positive) when pixel ( xk 1 , yk 1 ) lies outside the circle
boundary; therefore, y k 1 y k 1 = y k 1 gives y k 1 y k 1 and yk21 yk2 1 2 yk
moreover, the decision parameter may be written as
Pk 1 Pk 2.xk 1 1 2.( y k 1)
= Pk 2.xk 1 1 2. y k 1 (because y k 1 y k 1 )
(4)
From eqn. (3.13), the initial value of decision parameter at topmost point (0, r ) ,
calculated as
P0 f circle (1, r 12 ) 1 (r 12 ) 2 r 2 = 54 r
(5)
When radius of the circle is specified as integer value, P0 may be modified as
P0 1 r
Algorithm:
1. Start.
2. Declare variables x,y,p and also declare gdriver=DETECT,gmode.
3. Initialise the graphic mode with the path location in TC folder.
4. Input the radius of the circle r.
5. Load x-0,y=r,initial decision parameter p=1-r.so the first point is (0,r).
6. Repeat Step 7 while (x<y) and increment x-value simultaneously.
7. If (p>0),do p=p+2*(x-y)+1.
Otherwise (if p<0) do p=p+2*x+1 and y is decremented simultaneously.
8. Then calculate the value of the function circlepoints() with parameters (x,y).
9. Place pixels using putpixel at points (x+xc,y+yc) in specified colour in
circlepoints() function shifting the origin to xc on x-axis and yc on y-axis.
10. Close Graph.
11. Stop.
VII Semester 16
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Program to draw a circle by Mid-point algorithm
#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include <graphics.h>
#include <conio.h>
#include <dos.h>
#include <process.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\tc\\bgi");
clrscr();
float x,y,r,p;
cout<<"Enter circle radius in pixels:";
cin>>r;
x=0;
y=r;
p=1-r;
do
{
putpixel(320+x,240+y,RED);
putpixel(320+x,240-y,RED);
putpixel(320-x,240+y,RED);
putpixel(320-x,240-y,RED);
putpixel(320+y,240+x,RED);
putpixel(320+y,240-x,RED);
putpixel(320-y,240+x,RED);
putpixel(320-y,240-x,RED);
if(p<0)
{
x++; y=y;
p=p+2*x+1;
}
else
{
x++; y--;
p=p+2*x-2*y+1;
}
delay(500);
}
while(x<y);
getch();
}
VII Semester 17
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 3
Aim: To generate a smooth curve by using Bezier curve technique for a given
set of 4 control points.
Description:
In vector graphics, Bzier curves are used to model smooth curves that can be scaled
indefinitely. "Paths," as they are commonly referred to in image manipulation
programs are combinations of linked Bzier curves. Paths are not bound by the
limits of rasterized images and are intuitive to modify. Bzier curves are also used in
animation as a tool to control motion
Four points P0, P1, P2 and P3 in the plane or in higher-dimensional space define a
cubic Bzier curve. The curve starts at P0 going toward P1 and arrives at P3 coming
from the direction of P2. Usually, it will not pass through P1 or P2; these points are
only there to provide directional information. The distance between P0 and P1
determines "how long" the curve moves into direction P2 before turning towards P3.
VII Semester 18
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Program to draw a Bezier curve
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
//int fact(int x) // Subprogram to find factorial of a number
{
for(int i=x-1;i>=1;i--)
x=x*i;
if(x==0)
x=1;
return x;
}//
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
int x,y,px[4]={0},py[4]={0}; //row0; px values, row2; py values
int n=3; //therefore, no. of ctrl pts=n+1=4
cout<<"enter the control points :\n";
setcolor(BLUE);
for(i=0;i<=n;i++)
{
cin>>px[i]>>py[i];
}
line(px[0],py[0],px[1],py[1]);
line(px[1],py[1],px[2],py[2]);
line(px[2],py[2],px[3],py[3]);
float u=0;
for(u=0;u<=1;u=u+0.0001)
{
x=0;
y=0;
x=x+pow(1-u,3)*px[0]+3*u*pow(1-u,2)*px[1]+3*u*u*(1-
u)*px[2]+pow(u,3)*px[3];
y=y+pow(1-u,3)*py[0]+3*u*pow(1-u,2)*py[1]+3*u*u*(1-
u)*py[2]+pow(u,3)*py[3];
putpixel(x,y,RED);
}
getch();
}
VII Semester 19
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 4
Program to rotate a triangle about a point
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
float h,k,x1,x2,x3,y1,y2,y3,x=320,y=240,a[3][3],b[3][3],c[3][3], ang,m;
setcolor(BLUE);
line(0,240,640,240);
line(320,0,320,480);
cout<<"Enter first point:";
cin>>x1>>y1;
cout<<"Enter second point:";
cin>>x2>>y2;
cout<<"Enter third point:";
cin>>x3>>y3;
cout<<"Enter angle of rotation:";
cin>>ang;
cout<<"Enter refence point:";
cin>>h>>k;
setcolor(RED)
;
line(x+x1,y+y1,x+x2,y+y2);
line(x+x2,y+y2,x+x3,y+y3);
line(x+x3,y+y3,x+x1,y+y1);
ang=(ang*3.14)/180;
a[0][0]=cos(ang);
a[0][1]=-sin(ang);
a[0][2]=h*cos(ang)-k*sin(ang)-h;
a[1][0]=sin(ang);
a[1][1]=cos(ang);
a[1][2]=h*sin(ang)+k*sin(ang)-k;
a[2][0]=0;
a[2][1]=0;
a[2][2]=1;
VII Semester 20
Maharaja Agrasen Institute of Technology B.Tech(MAE)
b[0][0]=x1;
b[0][1]=x2;
b[0][2]=x3;
b[1][0]=y1;
b[1][1]=y2;
b[1][2]=y3;
b[2][0]=1;
b[2][1]=1;
b[2][2]=1;
for(int i=0;i<3;i++)
{for(int j=0;j<3;j++)
{c[i][j]=0;
for(m=0;m<3;m++)
{c[i][j]+=a[i][m]*b[m][j];
}
}
}
cout<<"output matrix"<<"\n";
for(i=0;i<3;i++)
{for(int j=0;j<3;j++)
{cout<<c[i][j]<<"\t";}
cout<<"\n";
}
setcolor(GREEN);
{line(c[0][0]+x,c[1][0]+y,c[0][1]+x,c[1][1]+y);
line(c[0][1]+x,c[1][1]+y,c[0][2]+x,c[1][2]+y);
line(c[0][2]+x,c[1][2]+y,c[0][0]+x,c[1][0]+y);
}
getch();
}
VII Semester 21
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 5
Prgram to translate a 2D triangular object
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
float tx,ty,x[3][3],t[3][3],n[3][3];
setcolor(BLUE);
line(x[0][0],x[0][1],x[1][0],x[1][1]);
line(x[1][0],x[1][1],x[2][0],x[2][1]);
line(x[2][0],x[2][1],x[0][0],x[0][1]);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{if(i==j)
t[i][j]=1;
else
t[i][j]=0;}
for(i=0;i<3;i++)
VII Semester 22
Maharaja Agrasen Institute of Technology B.Tech(MAE)
{for(j=0;j<3;j++)
{n[i][j]=0;
for(int m=0;m<3;m++)
{n[i][j]+=x[i][m]*t[m][j];}
}
}
cout<<"output matrix"<<"\n";
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{cout<<n[i][j]<<"\t";}
cout<<"\n";
}
setcolor(RED);
{
line(n[0][0],n[0][1],n[1][0],n[1][1]);
line(n[1][0],n[1][1],n[2][0],n[2][1]);
line(n[2][0],n[2][1],n[0][0],n[0][1]);
}
getch();
}
VII Semester 23
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 6
Prgram to SCALE a 2D triangular object
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
float sx,sy,x[3][3],t[3][3],n[3][3];
setcolor(BLUE);
line(x[0][0],x[0][1],x[1][0],x[1][1]);
line(x[1][0],x[1][1],x[2][0],x[2][1]);
line(x[2][0],x[2][1],x[0][0],x[0][1]);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{if(i==j)
t[i][j]=1;
else
t[i][j]=0;}
VII Semester 24
Maharaja Agrasen Institute of Technology B.Tech(MAE)
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{n[i][j]=0;
for(int m=0;m<3;m++)
{n[i][j]+=x[i][m]*t[m][j];}
}
}
cout<<"output matrix"<<"\n";
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{cout<<n[i][j]<<"\t";}
cout<<"\n";
}
setcolor(RED);
{
line(n[0][0],n[0][1],n[1][0],n[1][1]);
line(n[1][0],n[1][1],n[2][0],n[2][1]);
line(n[2][0],n[2][1],n[0][0],n[0][1]);
}
getch();
}
VII Semester 25
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 7
Prgram to REFLECT a 2D triangular object about any axis passing
through origin
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<dos.h>
#include<graphics.h>
void main()
{int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
clrscr();
float sx,sy,x[3][3],t[3][3],n[3][3],k;
setcolor(BLUE);
line(x[0][0],x[0][1],x[1][0],x[1][1]);
line(x[1][0],x[1][1],x[2][0],x[2][1]);
line(x[2][0],x[2][1],x[0][0],x[0][1]);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{if(i==j)
t[i][j]=1;
else
t[i][j]=0;}
cout<<"enter the reflection line(0 for x-axis, 1 for y-axis, 2 for bith
axis);";
cin>>k;
if (k==0)
t[1][1]=-1;
else
VII Semester 26
Maharaja Agrasen Institute of Technology B.Tech(MAE)
{if (k==1)
t[0][0]=-1;
else
{
t[0][0]=-1;
t[1][1]=-1 ;
}
}
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{n[i][j]=0;
for(int m=0;m<3;m++)
{n[i][j]+=x[i][m]*t[m][j];}
}
}
cout<<"output matrix"<<"\n";
for(i=0;i<3;i++)
{for(j=0;j<3;j++)
{cout<<n[i][j]<<"\t";}
cout<<"\n";
}
setcolor(RED);
{
line(n[0][0],n[0][1],n[1][0],n[1][1]);
line(n[1][0],n[1][1],n[2][0],n[2][1]);
line(n[2][0],n[2][1],n[0][0],n[0][1]);
}
getch();
}
VII Semester 27
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 8
C-Program for X-shear:
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"C:\\Tc\\BGI");
float x1,y1,x2,y2;
float shx,shy;
char ch;
x1=50;y1=100;x2=100;y2=200;
rectangle(x1,y1,x2,y2);
delay(10);
cout<<"enter the direction of shear : ";
cin>>ch;
if(ch=='x')
{
cout<<"enter x-direction of shear : ";
cin>>shx;
y1=y1+shx*x1;
y2=y2+shx*x2;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}
else
{
cout<<"enter y-direction of shear : ";
cin>>shy;
x1=x1+shy*y1;
x2=x2+shy*y2;
setcolor(RED);
rectangle(x1,y1,x2,y2);
}
getch();
closegraph();
}
VII Semester 28
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Experiment - 9
ISOMETRIC.CPP
#include<stdio.h>
#include<math.h>
#include<graphics.h>
#include<conio.h>
#include<iomanip.h>
#include<iostream.h>
#define rx 200
#define ry 200
#define pi 3.14
int i,j,k; //DEFINING GLOBAL VARIABLES
//DEFINING STRUCTURES
struct point
VII Semester 29
Maharaja Agrasen Institute of Technology B.Tech(MAE)
{
float x;
float y;
float z;
};
struct final
{
float x;
float y;
};
//DEFINING FUNCTIONS
//FUNCTION TO MAP THE POINT FROM ONE REFERENCE
FRAME TO THE OTHER
final reference(final p)
{
p.x=p.x*100+rx;
p.y=p.y*100+ry;
return(p);
}
void main()
{
int gd=DETECT,gm;
float a[10][4];
float theta,phi=0,radangle=0,trans[4][4],c[10][4],l,m,n,temp,q;
point p[10];
final pf[10],kf[10];
initgraph(&gd,&gm,"c:\tc\bgi");
for(i=0;i<10;i++)
{
cout<<"ENTER THE COORDINATES FOR THE POINT"
<<i+1<<endl;
cout<<"ENTER THE X COORDINATES FOR THE POINT"
<<i+1<<endl;
cin>>p[i].x;
cout<<"ENTER THE Y COORDINATES FOR THE POINT"
<<i+1<<endl;
cin>>p[i].y;
cout<<"ENTER THE Z COORDINATES FOR THE POINT"
<<i+1<<endl;
cin>>p[i].z;
}
for(i=0;i<10;i++)
{
VII Semester 30
Maharaja Agrasen Institute of Technology B.Tech(MAE)
for(j=0;j<4;j++)
{
a[i][0]=p[i].x;
a[i][1]=p[i].y;
a[i][2]=p[i].z;
a[i][3]=1;
}
}
cout<<"ENTER THE ANGLE BY WHICH THE CUBE HAS TO BE
ROTATED"<<endl;
cin>>radangle;
theta= (pi/180)*radangle;
phi=-(pi/180)*45;
q=sin(theta);
l=cos(theta);
m=cos(phi);
n=sin(phi);
clrscr();
//FORMING THE TRANSFORMATION MATRIX
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
if(j==(2||3))
{
trans[i][j]=0;
}
if(i==3)
{
trans[i][j]=0;
}
}
}
trans[0][0]=m;
trans[2][0]=n;
trans[0][1]=n*q;
trans[1][1]=l;
trans[2][1]=-m*q;
trans[3][3]=1;
trans[1][0]=0;
/* for(i=0;i<4;i++)
{ cout<<endl;
for(j=0;j<4;j++)
VII Semester 31
Maharaja Agrasen Institute of Technology B.Tech(MAE)
{
cout<<trans[i][j];
cout<<" ";
}
}
cout<<endl<<endl<<endl; */
//performing the matrix multiplication
temp=0;
for(i=0;i<10;i++)
{
for(k=0;k<4;k++)
{
for(j=0;j<4;j++)
{
temp=temp+a[i][j]*trans[j][k];
}
c[i][k]=temp;
temp=0;
}
}
/*
for(i=0;i<10;i++)
{
cout<<endl;
for(j=0;j<4;j++)
{
cout<<c[i][j];
cout<<" ";
}
}
*/
//ASSIGNING THE POINT TO 2D STRUCTURE
for(i=0;i<10;i++)
{
pf[i].x=c[i][0];
pf[i].y=c[i][1];
}
//FUNCTION CALL FOR FUNCTION REFERENCE
for(i=0;i<10;i++)
{
kf[i]=reference(pf[i]);
}
//PLOTTING THE COORDINATE SYSTEM
setbkcolor(RED+WHITE);
VII Semester 32
Maharaja Agrasen Institute of Technology B.Tech(MAE)
putpixel(rx,ry,BLACK);
setlinestyle(0,0,3);
line(0,ry,2*rx,ry);
line(rx,0,rx,2*ry);
setcolor(BLUE);
//PLOTTING THE CUBE COORDINATES ON THE 2D PLANE
for(j=0;j<4;j++)
{
line(kf[j].x,kf[j].y,kf[j+1].x,kf[j+1].y);
}
line(kf[0].x,kf[0].y,kf[4].x,kf[4].y);
for(j=5;j<8;j++)
{
line(kf[j].x,kf[j].y,kf[j+1].x,kf[j+1].y);
}
line(kf[5].x,kf[5].y,kf[8].x,kf[8].y);
line(kf[4].x,kf[4].y,kf[8].x,kf[8].y);
line(kf[0].x,kf[0].y,kf[5].x,kf[5].y);
line(kf[1].x,kf[1].y,kf[6].x,kf[6].y);
line(kf[3].x,kf[3].y,kf[9].x,kf[9].y);
line(kf[2].x,kf[2].y,kf[9].x,kf[9].y);
line(kf[7].x,kf[7].y,kf[9].x,kf[9].y);
line(kf[2].x,kf[2].y,kf[3].x,kf[3].y);
getch();
closegraph();
} //END OF FUNCTION MAIN
Experiment - 10
PERSPECTIVE.CPP
//PROGRAM FOR THREE POINT PERSPECTIVE PROJECTION
#include<stdio.h>
#include<math.h>
#include<graphics.h>
main()
{
int x1,y1,x2,y2,gd,gm;
int ymax,a[4][8];
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;
VII Semester 33
Maharaja Agrasen Institute of Technology B.Tech(MAE)
a[0][0] = 100; a[1][0] = 100; a[2][0] = -100;
a[0][1] = 200; a[1][1] = 100; a[2][1] = -100;
detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");
ymax = getmaxy();
xp = 300; yp = 320; zp = 100;
for(j=0;j<3;j++)
{
x1=(int) b[0][j]; y1=(int) b[1][j];
x2=(int) b[0][j+1]; y2=(int) b[1][j+1];
line( x1,ymax-y1,x2,ymax-y2);
}
x1=(int) b[0][3]; y1=(int) b[1][3];
x2=(int) b[0][0]; y2=(int) b[1][0];
line( x1, ymax-y1, x2, ymax-y2);
}
x1=(int) b[0][7]; y1=(int) b[1][7];
x2=(int) b[0][4]; y2=(int) b[1][4];
line( x1, ymax-y1, x2, ymax-y2);
setcolor(7);
for(i=0;i<4;i++)
{
x1=(int) b[0][i]; y1=(int) b[1][i];
x2=(int) b[0][4+i]; y2=(int) b[1][4+i];
line( x1, ymax-y1, x2, ymax-y2);
}
getch(); getch();
VII Semester 34
Maharaja Agrasen Institute of Technology B.Tech(MAE)
}
6. VIVA QUESTIONS
1. What is CAD? What are its applications. List some CAD Tools.
2. How is Cad different from traditional manual designing / drafting?
3. What is parallel projection?
4. What is orthographic projection?
5. What is oblique projection?
6. What is isometric projection? What is forshortening factor ?
7. Why do objects need to be transformed?
8. What is the importance of homogeneous coordinates?
9. How are animations created? What transformations are used?
10.Which is more effective- random scan or raster scan?
11.What is the logic of drawing a line by DDA algorithm?
12.Explain the logic of Bresenhems mid point circle drawing
algorithm.
13.What is the smallest unit of computer graphics screen? What is its
shape?
14.Where is the (0,0) coordinates of the computer screen located?
What is the size of the screen in pixels?
15.How is an object translated on the screen?
16.How is a 3-D object rotated about an axis on the screen?
17.What happens when an object is sheared along an axis?
18.Explain the importance of matrix operations in CAD?
19.Difference between world/global and User coordibnate systems.
20.How are the database stored and handled in a CAD software.
VII Semester 35