0% found this document useful (0 votes)
54 views48 pages

Shivang - Practical File Exp 1-8 - CAD

The document contains 5 experiments on computer aided design topics like DDA line algorithm, Bresenham's circle algorithm, Bezier curves, translation and rotation of 2D objects using affine transformations. Each experiment includes the aim, description of the concept, C++ code implementation and output.

Uploaded by

heysiri0804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views48 pages

Shivang - Practical File Exp 1-8 - CAD

The document contains 5 experiments on computer aided design topics like DDA line algorithm, Bresenham's circle algorithm, Bezier curves, translation and rotation of 2D objects using affine transformations. Each experiment includes the aim, description of the concept, C++ code implementation and output.

Uploaded by

heysiri0804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

COMPUTER AIDED DESIGN LAB

PAPER CODE: ETAT-451

SHIVANG
Roll No.:05214803617
Group: 7M2

SUBMITTED TO: - MR. ANIL GUPTA


EXP-1
AIM: To implement Digital Differential Analyzer (DDA)
Algorithm for drawing a line segment between two given endpoints 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. This algorithm is incremental and is used for the rasterization of
lines, triangles, and polygons." 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.

CODE:
#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;
setcolor(12);
line(0,240,640,240);
line(320,0,320,480);
cout<<"NAMAN
WALIA,42414803617,7M5"<<endl; cout<<"Enter
first point coordinates in pixel"<<endl; cin>>x1>>y1;
cout<<"Enter second point coordinates in pixel"<<endl;
cin>>x2>>y2;
dx=x2-x1;
dy=y2-y1;
if(abs(dx)>abs(dy))
{max=dx;}
else
{max=dy;}
xd=dx/max;
yd=dy/max;
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();
OUTPUT:

|
EXP-2
AIM: 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.

DESCRIPTION :
These algorithms uses the key feature of circle that it is highly symmetric.
So, for whole 360 degree of circle we will divide it in 8-parts each octant
of 45 degree. In order to that we will use Bresenham’s Circle Algorithm
for calculation of the locations of the pixels in the first octant of 45
degrees. It assumes that the circle is centered on the origin. So for every
pixel (x, y) it calculates, we draw a pixel in each of the 8 octants of the
circle as shown below :

CODE:
#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();
setbkcolor(12);
float x,y,xc,yc,r,p;
cout<<"NAMAN,42414803617,7M5"<<endl;
cout<<"Enter Circle Centre:";
cin>>xc>>yc;
circle(xc,yc,1);
cout<<"Enter Radius of Circle in Pixels:";
cin>>r;
x=0;
y=r;
p=1-r;
do
{
putpixel(xc+x,yc+y,RED);
putpixel(xc+x,yc-y,MAGENTA);
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,LIGHTBLUE);
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();
}
OUTPUT:
EXP-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:

Where n = degree of curve


n+1 = order of curve

The Parametric form of BEIZER Curve is given by :

12
SHIVANK SINGAL 05314803617 7M2
CODE :
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"");
clrscr();
int i,x,y,px[4]={0},py[4]={0};
int n=3;
cout<<"NAMAN WALIA
42414803617"; cout<<"Enter the control
point\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();
}
EXP-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,
thedistance through which the object is to be moved.

Mathematically,

CODE :
#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();
cout<<"NAMAN
WALIA,42414803617,7M5"<<"\n"; float
tx,ty,xc=320,yc=240, x[3][3],t[3][3],n[3][3]; int i,j,m;
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(i=0;i<3;i++)
{
for(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(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(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();
}
OUTPUT :
EXP-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

CODE:

#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;
int i,j,m;
setcolor(BLUE);
line(0,240,640,240);
line(320,0,320,480);
cout<<"NAMAN WALIA,42414803617,
7M5"<<"\n"; 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 refrence 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(i=0;i<3;i++)
{
for(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(j=0;j<3;j++)
{
cout<<c[i][j];
cout<<"\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();
OUTPUT ;
EXP-6
AIM: Write a program to scale a 2D object (rectangular/triangle) with
the given scale values Sx and Sy in x and 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 :
CODE (without reference 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();
setbkcolor(12);

line(0,240,640,240);
line(320,0,320,480);
float sx,sy,X=320,Y=240,x[3][3],t[3][3],n[3][3];
setcolor(BLUE);
cout<<"NAMAN WALIA, 42414803617, 7M5"<<"\n";
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 sx and sy as scalling 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();
}
OUTPUT (without reference point):
CODE (with reference 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 sx,sy,h,k,X=320,Y=240,x[3][3],t[3][3],n[3][3];
int i,j,m;
setcolor(BLUE);
line(0,240,640,240);
line(320,0,320,480);
cout<<"NAMAN WALIA,
42414803617,7M5"<<"\n"; 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(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i==j)
t[i][j]=1;
else
t[i][j]=0;
}
cout<<"Enter Refrence point:"<<"\n";
cin>>h>>k;
cout<<"Enter sx and sy scaling factors:";
cin>>sx>>sy;
t[0][0]=sx;
t[1][1]=sy;
t[2][0]=h*(1-sx);
t[2][1]=k*(1-sy);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
n[i][j]=0;
for(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]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
n[i][j]=0;
for(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]);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
n[i][j]=0;
for(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();
}
OUTPUT (with reference point):
EXP-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 AXIS Fx

In This Transformation x-ccordinate remains the same and y-coordinate


is negative.
x’=x
y’=-y
For a 2D Object homogenous x-Flip Equation is given by :

FLIP ABOUT ARBITRARY AXIS PASING THROUGH ORIGIN (Fa)


The 2D homogenous matric for flip about an arbitraty axis passing
through the origin and making an angle with x axis is given by the
equation :

CODE:
#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();
int i,j,m;
float k,X=320,Y=240,x[3][3],t[3][3],n[3][3];
setcolor(GREEN);
line(0,240,640,240);
line(320,0,320,480);
cout<<"NAMAN WALIA,42414803617,7M5"<<"\n";
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(i=0;i<3;i++)
for(j=0;j<3;j++)
{
if(i==j)
t[i][j]=1;
else
t[i][j]=0;

}
cout<<"Enter the reflection line (0:x-axis, 1:y-axis, 2: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(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();
}

OUTPUT:

You might also like