You are on page 1of 35

Computer Aided Design (CAD)Lab

(Instruction Manual)

ETAT- 451

VII Sem

B.Tech (MAE)

Maharaja Agrasen Institute of Technology, PSP area,


Sector 22, Rohini, New Delhi 110086
(Affiliated to Guru Gobind Singh Indraprastha University,
Dwarka, New Delhi)

1
Maharaja Agrasen Institute of Technology B.Tech(MAE)

INDEX OF THE CONTENTS:

1. Introduction to the lab.


2. Lab requirements (details of H/W & S/W to be used).
3. List of experiments.
4. Format of lab record to be prepared by the students.
5. Marking scheme for the practical examination.
6. Details of the each method/ experiment along with flowchart,
algorithm, main program idea & expected viva questions.

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.

The students may write alternative programs in MATLAB or SCILAB, if so


desired, as per their convenience.

Further this course is basically an entension of Computer graphics where


graphics are created using computers and, more generally, the representation
and manipulation of image data by a computer. The development of computer
graphics has made computers easier to interact with, and better for
understanding and interpreting many types of data. Developments in computer
graphics have had a profound impact on many types of media and have
revolutionized the animation and video game industry.

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

Applications: Computer Aided Design Computer simulation Digital art


Education Graphic design Information visualization Scientific
visualization Video Games Virtual reality Web design

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

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 1
given 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.

Alternative Matlab/ Scilab Programs:

1. Make a program in Matlab/ Scilab to perform basic 2D transformations.


2. Make a program in Matlab/ Scilab to perform basic 3D transformations.
3. Make a program in Matlab/ Scilab to generate Hermitis Cubic spline curve, Bezier curve
and B-spline curves..
4. Make a program in Matlab/ Scilab to show 3D objects as 2D using perspective and
Orthographic projections.

VII Semester 5
Maharaja Agrasen Institute of Technology B.Tech(MAE)
5. FORMAT OF THE LAB RECORD

(TO BE PREPARED BY THE STUDENTS)

The front page of the lab record should have a cover page as displayed below.

NAME OF THE LAB


Font should be (Size 20, italics bold, Times New Roman)

Faculty name: Student name:


Font should be (12, Times Roman) Roll No.:
Semester:
Group:
Font should be (12, Times Roman)

Maharaja Agrasen Institute of Technology, PSP Area,


Sector 22, Rohini, New Delhi 110085
Font should be (18, Times Roman)

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

LAB CODE : ETAT-451


Name of the student :
University Roll No. :
Branch :
Section/ Group :

PRACTICAL DETAILS:

Experiments according to CAD lab syllabus prescribed by GGSIPU.

Exp. Experiment Name Date of Date of Remarks Marks


No perform checking
ance

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

INTERNAL PRACTICAL EXAM

It is taken by the aasigned assistant Professor of the lab batch.


MARKING SCHEME FOR THIS EXAM IS:
Total Marks: 40
Division of 40 marks is as follows

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.

EXTERNAL PRACTICAL EXAM

It is taken by the concerned Assistant Professor of the batch and by an external


examiner. In this exam student needs to perform the experiment allotted at the
time of the examination, a sheet will be given to the student in which some details
asked by the examiner needs to be written and at the last viva will be taken by the
external examiner.

MARKING SCHEME FOR THIS EXAM IS:

Total Marks: 60
Division of 60 marks is as follows

1. Sheet filled by the student (With program code): 10


2. Viva Voice: 20
3. Experiment performance: 20
4. File submitted: 10

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.

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


VII Semester 9
Maharaja Agrasen Institute of Technology B.Tech(MAE)
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).
VII Semester 10
Maharaja Agrasen Institute of Technology B.Tech(MAE)
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.

Here is a sample program that initializes the graphics mode in C Language.


#include<graphics.h>
#include<conio.h>
main()
{
int gd=DETECT,gm; /*Auto detection of graphic driver & graphic mode
initgraph(&gd,&gm," "); / * initialization of graphic mode */
circle(150,150,100);
putpixel(x,y,RED);
getch();
closegraph(); /* Restore orignal screen mode */
} /* End of program */

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

Description: DDA algorithm is an incremental scan conversion method. Here we


perform calculations at each step using the results from the preceding step. The
characteristic of the DDA algorithm is to take unit steps along one coordinate and
compute the corresponding values along the other coordinate. The unit steps are
always along the coordinate of greatest change, e.g. if dx = 11 and dy = 7, then we
would take unit steps along x and compute the steps along y.

In DDA we need to consider two cases;


One is slope of the line less than or equal to one (|m| 1) and slope of the line
greater than one (|m| > 1).
1) When |m| 1 means y2-y1 = x2-x1 or y2-y1 <x2-x1.In both these cases we
assume x to be the major axis. Therefore we sample x axis at unit intervals
and find the y values corresponding to each x value. We have the slope
equation as
y=mx, y2-y1 = m (x2-x1)
In general terms we can say that y i+1 - yi = m(x i+1 - xi ). But here x = 1;
therefore the equation reduces to y i+1= yi + m = yi + dy/dx.
2) When | m| > 1 means y2-y1> x2-x1 and therefore we assume y to be the major
axis. Here we sample y axis at unit intervals and find the x values
corresponding to each y value. We have the slope equation as
y=mx , y2-y1 = m (x2-x1)

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

using the symmetry, as shown in Fig.. If ( x, y ) coordinates of a point P1 in the first


octant is calculated, it is easy to find out the remaining seven points in the other
seven octants such as P 2 ( y, x), P 3 ( y, x), P 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
required to find out pixel positions only in the region of 18 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

VIII Octant I Octant


y = x line
P8 ( x, y) P1 ( x, y)

P7 ( y, x) P2 ( y, x)
VII Octant II Octant

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.

Now, there are two possibilities for the decision parameter Pk :


(i). If Pk 0 ,
the midpoint lies inside the circle; alternatively, pixel on the scan line
yk is closer to the circle boundary and pixel choice will be ( xk 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 yk 1 becomes closer to the circle boundary and pixel
choice will be ( xk 1 , yk 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
xk 1 1 xk 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 ( 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:

A Bzier curve is a parametric curve frequently used in computer graphics and


related fields. Generalizations of Bzier curves to higher dimensions are called
Bzier surfaces, of which the Bzier triangle is a special case.

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

cout<<"Enter first point:";


cin>>x[0][0]>>x[0][1];
cout<<"Enter second point:";
cin>>x[1][0]>>x[1][1];
cout<<"Enter third point:";
cin>>x[2][0]>>x[2][1];
x[0][2]=1;x[1][2]=1;x[2][2]=1;

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 tx and ty as translation points;";


cin>>tx>>ty;
t[2][0]=tx;
t[2][1]=ty;

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

cout<<"Enter first point:";


cin>>x[0][0]>>x[0][1];
cout<<"Enter second point:";
cin>>x[1][0]>>x[1][1];
cout<<"Enter third point:";
cin>>x[2][0]>>x[2][1];
x[0][2]=1;x[1][2]=1;x[2][2]=1;

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 sx and sy as scaling factors;";


cin>>sx>>sy;
t[0][0]=sx;
t[1][1]=sy;

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

cout<<"Enter first point:";


cin>>x[0][0]>>x[0][1];
cout<<"Enter second point:";
cin>>x[1][0]>>x[1][1];
cout<<"Enter third point:";
cin>>x[2][0]>>x[2][1];
x[0][2]=1;x[1][2]=1;x[2][2]=1;

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;

a[0][2] = 200; a[1][2] = 200; a[2][2] = -100;


a[0][3] = 100; a[1][3] = 200; a[2][3] = -100;

a[0][4] = 100; a[1][4] = 100; a[2][4] = -200;


a[0][5] = 200; a[1][5] = 100; a[2][5] = -200;

a[0][6] = 200; a[1][6] = 200; a[2][6] = -200;


a[0][7] = 100; a[1][7] = 200; a[2][7] = -200;

detectgraph(&gd,&gm);
initgraph(&gd,&gm, "c:\\tc\\bgi");

ymax = getmaxy();
xp = 300; yp = 320; zp = 100;

for(j=0; j<8; j++)


{
x = a[0][j]; y = a[1][j]; z = a[2][j];

b[0][j] = xp - ( (float)( x - xp )/(z - zp)) * (zp);


b[1][j] = yp - ( (float)( y - yp )/(z - zp)) * (zp);
}

/*- front plane display -*/

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

/*- back plane display -*/


setcolor(11);
for(j=4;j<7;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][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

You might also like