Professional Documents
Culture Documents
(Instruction Manual)
ETAT- 451
VII Sem
B.Tech (MAE)
1
Maharaja Agrasen Institute of Technology B.Tech(MAE)
INDEX OF THE CONTENTS:
VII Semester 2
Maharaja Agrasen Institute of Technology B.Tech(MAE)
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
3.LIST OF EXPERIMENTS
VII Semester 4
Maharaja Agrasen Institute of Technology B.Tech(MAE)
(As prescribed by G.G.S.I.P.U)
Paper Code: ETAT-451 Paper: Computer Aided Design LAB
S. No. Programs List No. of
Labs
0 Study of basic graphics functions defined in graphics.h, graphic driver 1
& graphic mode.
1. Write a program to implement Digital Differential Analyzer (DDA) 1
Algorithm for drawing a line segment between two given end pixel points
A (x1, y1) & B(x2, y2) with any slope.
2 Write a program to implement (Bresenhams) Mid-point Circle drawing 1
algorithm for drawing a circle with a given center of circle P(Xc,Yc) and
radius R in terms of pixels.
3 Write a program to generate a parametric smooth curve by using Bezier 1
Curve for a given set of 4 control points.
4 Write a program to apply the basic transformation- Rotation for a given 1
2D object.(triangle).
5 Write a program to Translate a triangle with three input points and 1
translation values in x & y directions.
6 Write a program to Scale a 2D object (rectangle/ triangle)., with the given 1
scale values in x & y directions
7 Write a program to Reflect a polynomial of side n about x-axis, y-axis or 1
y=x line.
8 Write a program to Shear a polynomial of side n along x-axis, or along y- 1
axis.
9 Write a program for displaying 3D objects as 2D display using Isometric 1
Projection.
10 Write a program for displaying 3D objects as 2D display using 1
Perspective Projection.
11 Write a program to generate a smooth curve by using Hermits Cubic 1
Curve technique for a given set of 2 control points. and 2 tangents.
VII Semester 5
Maharaja Agrasen Institute of Technology B.Tech(MAE)
(TO BE PREPARED BY THE STUDENTS)
The front page of the lab record should have a cover page as displayed below.
The second page in the record should be the index as displayed below.
CAD LAB
VII Semester 6
Maharaja Agrasen Institute of Technology B.Tech(MAE)
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)
6. MARKING SCHEME FOR THE PRACTICAL
EXAMS
There will be two practical exams in each semester.
Internal Practical Exam
External Practical Exam
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.
VII Semester 9
Maharaja Agrasen Institute of Technology B.Tech(MAE)
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.
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
y = x line
VIII Octant
Maharaja Agrasen Institute of Technology B.Tech(MAE)
I Octant
P8 ( x, y )
Experiment 2
Aim: To implement Bresenhams Mid-Point Circle drawing algorithm for
VII Octant P7 (drawing
y, x) a circle with a given P2 ( y, of
center
II Octant x) circle P(xc, yc) and radius r.
Description:
Bresenhams
VI Octant algorithm extensively uses symmetry of the circle. This is fact that if
III Octant
1 7
8 part of the circle is generated then rest 8 part of the circle can be generated
y = - x line ( x, y ) P
using the P6 symmetry,
( y, x) as shown in Fig.. PIf3 ( y, x)coordinates of a point 1 in the first
octant is calculated, it is easy to find out the remaining seven points in the other
V Octant
seven octants such P5 ( xIV
,Octant
as yP) 2 ( y, x), P 3P( 4y,(xx,), yP)4 ( x, y), P 5 ( x, y), P 6 ( y, x), P 7 ( y, x) and
P 8 ( x, y ) . Therefore, in Bresenhams midpoint circle generating algorithm, it is
1
required to find out pixel positions only in the region of 8 part of the circle (i.e.,
one octant), and hence, development of software is very easy. Moreover, the circle
draws at a faster rate.
y
P1 ( x, y )
(0, r )
In this method, at first, the topmost point, i.e., is plotted if equation of
x2 y2 r 2 .
circle is However, for circle with centre position ( xc , y c ) 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 y c 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
( x, y )
< 0, if point 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 y k or one at y k 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 , y k 1 ) as choice II,
depending upon position of the midpoint between two adjacent pixels y k and y k 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 ( x k 1 , ) f circle ( x k 1, y k 12 )
2 (1)
( x k 1) ( y k ) r
2 1 2 2
= 2
Choice I
Scan line
(0, r )
yk Mid point
y k 1 Circle boundary
y k 2
Choice II
xk xk 1 xk 2
Pixel No.
Pk
Now, there are two possibilities for the decision parameter :
Pk 0 ,
(i). If the midpoint lies inside the circle; alternatively, pixel on the scan line
yk is closer to the circle boundary and pixel choice will be ( x k 1 , y k ) , i.e., choice
I.
(ii). If Pk 0 , the midpoint lies outside or on the circle boundary;
alternatively, pixel on the scan line y k 1 becomes closer to the circle boundary
and pixel choice will be ( x k 1 , y k 1 ) , i.e., choice II.
The recursive formula for successive values of the decision parameter Pk may be
developed by using the incremental calculations for the next pixel position at
x k 1 1 x k 2 . Thus, recursive expression for Pk 1 may be written as
VII Semester 15
Maharaja Agrasen Institute of Technology B.Tech(MAE)
Pk 1 f circle ( x k 1 1, y k 1 12 )
{( x k 1) 2 1}2 { y k 1 12 }2 r 2
=
( x k 1) 2 1 2.( xk 1) y k21 14 y k 1 r 2
=
{( x k 1) 2 ( y k2 14 y k ) r 2 } 1 2.( x k 1) y k21 y k 1 y k2 y k
=
{( x k 1) 2 ( y k2 14 y k ) r 2 } 1 2.( x k 1) y k21 y k 1 y k2 y k
=
Pk 1 Pk 2.( xk 1) ( y k21 y k2 ) ( y k 1 y k ) 1
or
(2)
where y k 1 y k (if Pk 0, i.e., negative) when pixel ( x k 1 , y k ) lies inside the circle
Pk 1 Pk 2.x k 1 1
boundary; therefore,
(3)
y k 1 y k 1 Pk 0 , ( x k 1 , y k 1 )
and (if i.e., positive) when pixel lies outside the circle
y k 1 y k 1 = y k 1 gives y k 1 y k 1 y k21 y k2 1 2 y k
boundary; therefore, and
moreover, the decision parameter may be written as
Pk 1 Pk 2.x k 1 1 2.( y k 1)
= Pk 2.xk 1 1 2. y k 1 (because y k 1 y k 1 )
(4)
(0, r ) ,
From eqn. (3.13), the initial value of decision parameter at topmost point
calculated as
P0 f circle (1, r 12 ) 1 (r 12 ) 2 r 2
= 4 r
5
(5)
P0
When radius of the circle is specified as integer value, 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)
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)
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;
VII Semester 20
Maharaja Agrasen Institute of Technology B.Tech(MAE)
a[2][0]=0;
a[2][1]=0;
a[2][2]=1;
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;}
VII Semester 22
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 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;}
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)
VII Semester 26
Maharaja Agrasen Institute of Technology B.Tech(MAE)
t[1][1]=-1;
else
{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
VII Semester 29
Maharaja Agrasen Institute of Technology B.Tech(MAE)
int i,j,k; //DEFINING GLOBAL VARIABLES
//DEFINING STRUCTURES
struct point
{
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;
}
VII Semester 30
Maharaja Agrasen Institute of Technology B.Tech(MAE)
for(i=0;i<10;i++)
{
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;
VII Semester 31
Maharaja Agrasen Institute of Technology B.Tech(MAE)
/* for(i=0;i<4;i++)
{ cout<<endl;
for(j=0;j<4;j++)
{
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]);
VII Semester 32
Maharaja Agrasen Institute of Technology B.Tech(MAE)
}
//PLOTTING THE COORDINATE SYSTEM
setbkcolor(RED+WHITE);
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];
VII Semester 33
Maharaja Agrasen Institute of Technology B.Tech(MAE)
float par[4][4],b[4][8];
int i,j,k,m,n,p;
int xp, yp, zp, x, y, z;
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);
VII Semester 34
Maharaja Agrasen Institute of Technology B.Tech(MAE)
}
getch(); getch();
}
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