CAD Lab - Exp - 1 - 7 - by - Anil Gupta
CAD Lab - Exp - 1 - 7 - by - Anil Gupta
CAD Lab - Exp - 1 - 7 - by - Anil Gupta
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;
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.
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) =
[x’ y’ 1] = [x y 1].
The above result can also be achieved by:
[P’] = [T].[P] or
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( );
}