CAD Lab - Exp - 1 - 7 - by - Anil Gupta

Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

By: Mr.

Anil Gupta
Asstt. Professor – MAE Deptt.
MAIT. Sec-22 ROHINI DELHI-86
University Prescribed Syllabus of CAD Lab
Use computer software such as: C / C++ / MATLAB / SCILAB /
Java / any other to make programs for under mentioned:

1. Line(s) Drawing;
2. Drawing Bezier curve(s);
3. Drawing B-Spline curve(s);
4. Develop menu-bar and buttons for above;
5. Do geometric transformations for translation
6. Use menu-bar for rotation / mirror;
7. Use menu-bar for scaling;
8. Perform numerical calculations of any problem done
in class and show its graphical manipulation on
software.
9. Exposure to any 2D / 3D modeling commercially
available software;

NOTE:- At least 8 Experiments from the syllabus must be done in the


semester
1. Write a program to implement Digital Differential Analyzer (DDA)
Algorithm for drawing a line segment between two given end pixel
points A (x1, y1) & B(x2, y2).
2. Write a program to implement (Bresenham’s) Mid-point Circle drawing
algorithm for drawing a circle with a given center of circle P(xc,yc) and
radius R in pixels.
3. Write a program to generate a parametric cubic Bezier Curve for a given
set of 4 control points.
4. Write a program to Translate a triangle with three input points and tx,
ty as translation values in x & y directions.
5. Write a program to apply the basic transformation- Rotation for a given
2D object(triangle), to change its position and orientation.
6. Write a program to Scale a 2D object (rectangle / triangle) with the
given scale values Sx, Sy in x & y directions.
7. Write a program to Reflect a polynomial of side n about x-axis, y-axis,
origin or y=x line using menu bar options.
8. Write a program to Shear a polynomial of side n along x-axis with shear
factor Shx, or along y-axis with shear factor Shy.
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;
We know the equation of straight line is: y=mx+c

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=mΔx, y2-y1 = m (x2-x1) where m< 1 (say 0.8)
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=mΔx , y2-y1 = m (x2-x1)
Algorithm
 Start.
 Declare variables x,y,x1,y1,x2,y2,i,dx,dy,max,xd,yd 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; thatis, plot the first point.
 put x=x1,y=y1. Also load second point as x=x2, y=y2.
 Calculate dx=x2-x1 and dy=y2-y1.
 If abs(dx) > abs(dy), do max=abs(dx). Otherwise max= abs(dy).
 Then xd=dx/s and yd=dy/s.
 Start from i=0 and continuing till i<max,the points will be
 x=x+xd.
 y=y+yd.
 Place pixels using putpixel at points (x,y) in specified colour.
 Close Graph.
 Stop.
 Program to draw a straight line by DDA Algorithm

// Write your name, enrollment no. & group here.


//Note: First point on LHS and second point on RHS always.
#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:\\turboc3\\bgi");
clrscr();
float x1,x2,y1,y2, xd,yd,x=320,y=240,m,c,max,dx,dy;
 // Drawing central coordinate axes to divide screen in 4
quadrants.
 setcolor(BLACK);
 line(0,240,640,240);
 line(320,0,320,480);
 cout<<"Enter first point coordinates in pixels:";
 cin>>x1>>y1;
 cout<<"Enter second point coordinates in pixels:";
 cin>>x2>>y2;
 dx=x2-x1;
 dy=y2-y1;
 m=dy/dx;
 c=y1-m*x1;
 cout<<"The slope of line is m="<<m<<"\n";
 cout<<"Inclination of line with x-axis in degrees
is:"<<atan(m)*180*7/22<<"\n";
 cout<<"The equation of line is: "<<"y="<<m<<"x+"<<c;
 if(abs(dx)>abs(dy))
 {max=dx;}
 else
 {max=dy;}
 xd=dx/max; / unit pixel movement in x-direction as slope <= 1
 yd=dy/max; / unit pixel movement in y-direction as slope > 1
 x=x+(x1+0.5);
 y=y-(y1+0.5);
 for(int i=1; i<=max; i++)
 {
 putpixel(x,y,RED);
 x=x+xd;
 y=y-yd;
 }
 getch( );
 }
Experiment No. 2
 Aim:To implement Bresenham’sMid-Point Circle
drawing algorithm for drawing a CIRCLE with a
given center of circle P(xc, yc) and radius r.

 Start x=0 , y=r , decision parameter p = 1-r
 (if p<0) do p=p+2*x+1, then x=x+1 , y=y
 (if p>0) do p=p+2*x-2*y+1, then x=x+1 , y=y-1
Algorithm:
 Start.
 Declare variables x,y,p .xc.yc and also declare gdriver=DETECT,gmode.
 Initialise the graphic mode with the path location in TC folder.
 Input the radius of the circle r and centre coordinates (xc,yc).
 Load x-0, y=r, so the first point is (0,r)..
 initial decision parameter p=1-r.

 (if p<0) do p=p+2*x+1, x is incremented and y remains same


simultaneously.
 Otherwise, If (p>0), do p=p+2*(x-y)+1. x is incremented, y is
decremented.
 Repeat Steps while (x<y) and increment x-value simultaneously.
 Then calculate the value of the function circlepoints() with parameters
(x,y).
 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.
 Close Graph.
 Stop.
 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:\\turboc3\\bgi");
 clrscr();
 float x,y,xc,yc,r,p;
 cout<<"Enter circle centre: ";
 cin>>xc>>yc;
 cout<<"Enter the radius of circle in pixels:";
 cin>>r;
 x=0;
 y=r;
 p=1-r;
 do
 Program to draw a circle by Mid-point algorithm
 {putpixel(xc+x,yc+y,RED);
 putpixel(xc+x,yc-y,ORANGE);
 putpixel(xc-x,yc+y,BLUE);
 putpixel(xc-x,yc-y,GREEN);
 putpixel(xc+y,yc+x,CYAN);
 putpixel(xc+y,yc-x,YELLOW);
 putpixel(xc-y,yc+x,PINK);
 putpixel(xc-y,yc-x,BROWN);
 if(p<0)
 {
 x++;
 y=y;
 p=p+2*x+1;
 }
 else
 {
 x++;
 y--;
 p=p+2*x-2*y+1;
 }
 delay(100);
 }
 while(x<y);
 getch( );}
Experiment No. 3
Aim: To generate a smooth curve by using Bezier curve
technique for a given set of 4 control points.
 Description:
 A Bézier curve is a parametric synthetic free form of
curve frequently used in computer graphics and related
fields. Bézier curves are also used in animation as a tool
to control motion.

 The general equation of non-parametric polynomial form


of 2D curve is:
y= a0+ a1.x+ a2.x2+ a3.x3+……………..+an.xn

where n = degree of curve


n+1 = order of curve
= no. of control points.
 The parametric form of BEZIER Curve is given by:
 P(u) = (u) where
 P(u) = Position of a pixel point on the curve in
parametric space.
 u = a parameter such that umin ≤ u ≤ umax or
 0≤u≤1
 Pi = Position or coordinates of ith control point.
 Bi,n (u) =
A basis function to generate Bezier curve
called Bernstein Blending Function and is given by:

 Bi,n (u)=
 For a Cubic BEZIER Curve,
 we have Degree of curve= n = 3
 no. of control points = n +1 = 4

 Four points P0, P1, P2 and P3 (Pi , i=0….n) in the plane
define a cubic Bézier curve.
 The curve starts at P0 going toward P1 and arrives at P3
coming from the direction of P2.
 The curve passes through start point P0 and end point
P3.
 Usually, it will not pass through intermediate control
points P1 or P2; these points are only there to provide
directional information.
 (u) = (u)
=

P(u) =

In terms of parametric 2D Cartesian coordinates, the above


can be written as:
 x(u) =
 y(u) =
 Program to draw a Bezier curve

 #include<iostream.h>
 #include<conio.h>
 #include<math.h>
 #include<graphics.h>

 void main( )
 {
 int gd=DETECT,gm;
 initgraph(&gd,&gm,"c:\\turboc3\\bgi");
 clrscr();
 int i,x,y,px[4]={0},py[4]={0}; //Initialisation of x, y row vectors of length4
and 0 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( );
 }
Experiment No. 4
Aim: Write a program to translate a given 2D object (triangle with
three input points) at a desired position with tx, ty as translation
values in x & y directions.
Description: Moving an object across the screen parallel to its initial
position is called linear translation.
Every point on the object moves by a prescribed distance in a given
direction.
This is accomplished by adding to the coordinates of each corner point, the
distance through which the object is to be moved.
Mathematically
x’=x+tx
y’=y+ty
In matrix form,
[x’ y’] = [x y] + [tx ty]
[P’] =[P] + [T]
This is usually the operation used in CAD system as
MOVE command.
Now for a 2D Homogeneous Coordinate System, the
translation of a point is represented as
[P’] = [P].[T]

[x’ y’ 1] = [x y 1].
The above result can also be achieved by:

[P’] = [T].[P] or

 For the translation of a 2D triangular object in HCS,


we have

[n] =[x] *[t]


Program 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:\\turboc3\\bgi");
 clrscr();
 float tx,ty,xc=320,yc=240, x[3][3],t[3][3],n[3][3];

 setcolor(BLUE);
 line( 0,240,640,240);
 line( 320,0,320,480);
 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(xc+x[0][0],yc-x[0][1],xc+x[1][0],yc-x[1][1]);
 line(xc+x[1][0],yc-x[1][1],xc+x[2][0],yc-x[2][1]);
 line(xc+x[2][0],yc-x[2][1],xc+x[0][0],yc-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++)
 {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];} // Main logic of matrix multiplication.
 }
 }
 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(xc+n[0][0],yc-n[0][1],xc+n[1][0],yc-n[1][1]);
 line(xc+n[1][0],yc-n[1][1],xc+n[2][0],yc-n[2][1]);
 line(xc+n[2][0],yc-n[2][1],xc+n[0][0],yc-n[0][1]);
 }
 getch( );
Experiment No. 5
Aim: Write a program to apply the affine transformation- Rotation
for a given 2D object (triangle) to change its position and
orientation.
Description: Rotation is a transformation operation which changes the
position and orientation of an object.
In 2D rotation, the object is rotated about origin by an angle about z-axis in
X-Y plane.
For positive angles, the object is rotated in counter clockwise direction,
while for negative angles, the object is rotated in clockwise direction.
 Consider a point P (x,y) be at a distance r from origin and
make an initial angular position 𝟇 with the horizontal x-
axis so that
x=rcos 𝟇. and y=rsin 𝟇.

Let this point be rotated about origin by an angle θ in


positive ccw direction to a new position P’(x’,y’) so that.

x’ =rcos(θ + 𝟇) = rcosθcos 𝟇-rsin θsin 𝟇 = xcos θ–ysin θ


y’=rsin(θ + 𝟇)=rsin θ cos 𝟇+rcos θsin 𝟇 = xsin θ+ycos θ

In Matrix form, the above can be written as


[P’] =[P].[R]
[x’ y’] =[x y].
 In Homogeneous Coordinate System, it can written as:
 [x’ y’ 1] =[x y 1].

 For rotation of a triangular obt ABC about any


reference point (h,k), we need to perform
concatenation of matricesin HCS to carry out 3
operations simultaneously.
 Translation of reference point to origin by tx = -h and
ty = -k
 Deisred rotaion by θ about origin in cw or ccw
direction.
 Tranlation back to refernce point position by tx = h
and ty= k.
c[3][3] = a[3][3].b[3][3]
 Program to rotate a Triangular object 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:\\turboc3\\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;
setcolor(BLUE);
line(0,240,640,240);
line(320,0,320,480);
Cout<<“Name, Enrollment No., 7M1 “;
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 in degrees:"; cin>>ang;
cout<<"Enter reference point of rotation:"; cin>>h>>k;
 setcolor(BLUE);
 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]=x1; a[0][1]=y1; a[0][2]=1;
 a[1][0]=x2; a[1][1]=y2; a[1][2]=1;
 a[2][0]=x3; a[2][1]=y3; a[2][2]=1;

 b[0][0]=cos(ang); b[0][1]=sin(ang); b[0][2]=0;
 b[1][0]=-sin(ang); b[1][1]=cos(ang); b[1][2]=0;
 b[2][0]= -h*cos(ang)+k*sin(ang)+h;
 b[2][1]= -h*sin(ang)-k*cos(ang)+k;
 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(int i=0;i<3;i++)
 {for(int j=0;j<3;j++)
 {cout<<c[i][j]<<"\t";}
 cout<<"\n";
 }
 setcolor(RED);
 {line(x+c[0][0],y-c[0][1],x+c[1][0],y-c[1][1]);
 line(x+c[1][0],y-c[1][1],x+c[2][0],y-c[2][1]);
 line(x+c[2][0],y-c[2][1],x+c[0][0],y-c[0][1]);
 }
 getch( );
 }
Experiment No. 6
 Aim: Write a program to Scale a 2D object
(rectangle/triangle) with the given scale values Sx, Sy in
x & y directions.
 Description: Scaling is one the most effective transformation
operations of an object by which we get a close up view (zoom in) of any
portion of the object or get a distant view (zoom out) around the original
object. To achieve scalingthe original coordinates of an object are
multipled by scaling factor Sx along x-diection and Sy along y-direction.
 S ≥ 1, indicates Enlargement or an expansion of length.
 S < 1, indicates Contraction or an compression of length.
 Sx =Sy =1 means No Scaling.
 Sx =Sy means Uniform Scaling.
 Sx Sy means Non-uniform Scaling.
 For a 2D Scaling of a triangular object in HCS, we have

n[3][3]= x[3][3].t[3][3]
 Program 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:\\turboc3\\bgi");
 clrscr( );
 float sx,sy,X=320,Y=240,x[3][3],t[3][3],n[3][3];
 setcolor(BLUE);
line(0,240,640,240);
line(320,0,320,480);
Cout<<“Name, Enrollment No., 7M1 “;

 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+x[0][0],Y-x[0][1],X+x[1][0],Y-x[1][1]);
 line(X+x[1][0],Y-x[1][1],X+x[2][0],Y-x[2][1]);
 line(X+x[2][0],Y-x[2][1],X+x[0][0],Y-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;

 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(X+n[0][0],Y-n[0][1],X+n[1][0],Y-n[1][1]);
 line(X+n[1][0],Y-n[1][1],X+n[2][0],Y-n[2][1]);
 line(X+n[2][0],Y-n[2][1],X+n[0][0],Y-n[0][1]);
 }
 getch( ); }

Experiment No. 7
 Aim: Write a program to Reflect a polynomial of side n about x-
axis, y-axis, origin and y = x line.

 Description: Reflection or flip is a transformation that produces mirror
image of an object relative to an axis of reflection. It is a very useful
operation for producing the drawings of symmetric objects.
 Flipping of an object can be done about x-axis, y-axis, origin, or about any
arbitrary axis at a specified angle with x-axis.
 . Flip about Horizontal x-axis (Fx)
 In this transformation x-coordinate remains same and the
y-coordinate is negative.
 x’= x
 y’= -y
 For a 2Dobject homogenous x-Flip equation is given by
 Flip about any arbitrary axis passing through origin (Fa)
 The 2D homogeneous matrix for flip about an arbitrary
axis passing through the origin and making an angle with
x-axis is given by the equation:
 Program 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:\\turboc3\\bgi");
 clrscr( );
 float k, X=320,Y=240,x[3][3],t[3][3],n[3][3;
 setcolor(BLUE);

1. line(0,240,640,240);
2. line(320,0,320,480);
3. Cout<<“Name, Enrollment No., 7M1 “;
 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+x[0][0],Y-x[0][1],X+x[1][0],Y-x[1][1]);
 line(X+x[1][0],Y-x[1][1],X+x[2][0],Y-x[2][1]);
 line(X+x[2][0],Y-x[2][1],X+x[0][0],Y-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
origin);";
 cin>>k;
 if (k==0)
 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(X+n[0][0],Y-n[0][1],X+n[1][0],Y-n[1][1]);
 line(X+n[1][0],Y-n[1][1],X+n[2][0],Y-n[2][1]);
 line(X+n[2][0],Y-n[2][1],X+n[0][0],Y-n[0][1]);}
 getch( );
 }

You might also like