Bresenham’s Line Drawing
EX
NO.1(A)
Aim:
To implement Bresenham’s line
drawing Algorithm for drawing lines.
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1: Start
Step 2: Get the values of the end points as(x1,
y1) &(x2, y2)
Step 3: Assign x=x1, y=y1;
Step 4: Compute dx=x2-x1
Step 5: Compute dy=y2-y1
Step 6: Assign sx=x2-x1, sy=y2-y1
Step 7: If dy>dx then interchange the values of
dx and dy and assign exch=1
Step 8: Compute p=2xdy-dx
Step 9: Put a pixel on(x,y)
Step 10: If exch=1, y=sy else x=x+sx
Step 11: If p>0 and exch =1, x=x+sx else
y=y+sy, p=p-2xdx
Step 12: Compute p=p+2xdy
Step 13: Do steps (9) t0 (12) for dx times
Step 14: Stop
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int
signs(int m);
void bres_line(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int x1,x2,y1,y2;
initgraph(&gd,&gm,"");
cout<<"enter starting value of
X-Axis----->";
cin>>x1;
cout<<"enter starting value of
Y-Axis----->";
cin>>y1;
cout<<"enter ending value of
X-Axis----->";
cin>>x2;
cout<<"enter ending value of
y-Axis----->";
cin>>y2;
bres_line(x1,y1,x2,y2);
getch();
closegraph();
}
void bres_line(int x1,int y1,int x2,int y2)
{
int x,y,sx,sy,p,temp,exch=0,i,dx,dy;
x=x1;
y=y1;
dy=abs(y1-y2);
dx=abs(x1-x2);
sx=signs(x2-x1);
sy=signs(y2-y1);
if(dy>dx)
{
temp=dx;
dx=dy;
dy=temp;
exch=1;
}
p=2*dy-dx;
for(i=0;i<=dx;i++)
{putpixel(x,y,15);
if(exch==1)
y+=sy;
else
x+=sx;
if(p>=0)
{
if(exch==1)
x+=sx;
else
y+=sy;p=p-2*dx;}
p=p+2*dy;
}
}
int signs(int m)
{
if(m<0)
return(-1);
else if(m>0)
return(1);
else
return(0);
}
Input:
Enter starting value of X-Axis----->100
Enter starting value of Y-Axis----->100
Enter ending value of X-Axis----->200
Enter ending value of Y-Axis----->200
Output:
Result:
Thus the
program to draw line using Bresenham’s line drawing Algorithm was executed
successfully.
Circle Drawing
EX NO 1(B)
Aim:
To write a program to draw a circle using Bresenham’s circle drawing
Algorithm.
Functions
used:
Circle()
The function circle() is used to draw
a circle using(x,y) as centre point.
Syntax:
circle (x,y,radius)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Putpixel ()
The function putpixel() is used to place
a pixel at particular coordinate
Syntax:
Putpixel(x,y,color)
Algorithm:
Step 1: Start
Step 2: Get the center point as (xc,yc),get the
radius as r.
Step 3: Assign y=r,x=0
Step 4: Calculate p=3-2r
Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and
y=y-1
Step 6: Increment x by 1
Step 7: Do steps (9) to (16)
Step 8: Repeat steps (5) to (9) until x<=y
Step 9: Put a pixel on (xc+x,yc+y,15);
Step 10: Put a pixel on (xc+x,yc-y,15);
Step 11: Put a pixel on (xc-x,yc+y,15);
Step 12: Put a pixel on (xc-x, yc-y, 15);
Step 13: Put a pixel on (xc+y,yc+x,15);
Step14: Put a pixel on (xc+y, yc-x, 15);
Step 15: Put a pixel on (xc-y, yc+x, 15);
Step 16: Put a pixel on (xc-y, yc-x, 15);
Step 17: Stop.
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void my_circle(int,int,int);
void plot(int,int,int,int);
void main()
{
int gd=DETECT,gm;
int xc,yc,r;
initgraph(&gd,&gm,"");
cout<<"enter X-Axis----->";
cin>>xc;
cout<<"enter Y-Axis----->";
cin>>yc;
cout<<"enter radius----->";
cin>>r;
my_circle(xc,yc,r);
getch();
closegraph();
}
void my_circle(int xc,int yc,int r)
{
int x,y,p;
p=3-2*r;
x=0;
y=r;
while(x<=y)
{
if(p<0)
p=p+(4*x+6);
else
{
p=p+10+4*(x-y);
y=y-1;
}
plot(xc,yc,x,y);
x=x+1;
}
}
void plot(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,15);
putpixel(xc+x,yc-y,15);
putpixel(xc-x,yc+y,15);
putpixel(xc-x,yc-y,15);
putpixel(xc+y,yc+x,15);
putpixel(xc+y,yc-x,15);
putpixel(xc-y,yc+x,15);
putpixel(xc-y,yc-x,15);
}
Input:
Enter
X-Axis----->:100
Enter
Y-Axis----->:200
Enter
radius----->: 40
Output:
Result:
Thus the program to draw circle using Bresenham’s circle drawing
Algorithm was executed successfully.
Ellipse Drawing
EX NO 1(C)
Aim:
To write a program to draw a ellipse using Bresenham’s ellipse drawing
Algorithm.
Functions
used:
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Putpixel ()
The
function putpixel() is used to place a pixel at particular coordinate
Syntax:
Putpixel(x,y,color)
Algorithm:
Step 1: Start
Step 2: Get the center point as(x1, y1)
Step 3: Get the length of semi-major, semi-minor
axes as r1 & r2
Step 4: Calculate t=pi/180
Step 5: Initialise i=0;
Step 6: Compute d=i*t
Step 7: Compute x=x1+y1*sin(d), y=y1+r2*cos(d).
Step 8: Put a pixel on(x,y)
Step 9: Increment I by 1
Step 10: Repeat steps(6) to (9) until i<360
Step 11: Stop
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
void main()
{
int gd=DETECT,gm,x1=0,y1=0,r1=0,r2=0;
float x=0,y=0,t,d,i;
initgraph(&gd,&gm,"");
cout<<"enter the center co-or:";
cin>>x1>>y1;
cout<<"enter the radius1:";
cin>>r1;
cout<<"enter radius2:";
cin>>r2;
for(i=0;i<360;i++)
{
t=3.14/180;
d=i*t;
x=x1+ceil(r1*sin(d));
y=y1+ceil(r2*cos(d));
putpixel(x,y,15);
}
getch();
closegraph();
}
Input:
Enter the center co-or:100 150
Enter the radius1:40
Enter radius2:20
Output:
Result:
Thus the program to draw ellipse using Bresenham’s ellipse drawing
Algorithm was executed successfully.
Implementation
of Line, Circle and ellipse Attributes
EXNO(2)
Aim:
To write a program to draw a Line,
Circle and ellipse Attributes
Functions
used:
Circle()
The
function circle() is used to draw a circle using(x,y) as centre point.
Syntax:
circle (x,y,radius)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Putpixel ()
The function putpixel() is used to place
a pixel at particular coordinate
Syntax:
Putpixel(x,y,color)
Algorithm:
Step 1: Start
Step 2: Get the center point as (xc,yc),get the
radius as r.
Step 3: Assign y=r,x=0
Step 4: Calculate p=3-2r
Step 5: If p<0,p=p+4x+6 else p=p+10+4(x-y) and
y=y-1
Step 6: Increment x by 1
Step 7:stop
Program
#include
<graphics.h>
#include <stdlib.h>#include <stdio.h>
#include <conio.h>
#define ROUND(a) ((int)(a+0.5))
//Line drawing
void lineBres(int xa,int ya,int xb,int yb)
{
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twoDy=2*dy,twoDyDx=2*(dy-dx);
int x,y,xEnd;
/* Determine which point to use as start,which as end */
if(xa>xb)
{
x=xb;
y=yb;
xEnd=xa;
}
else
{
x=xa;
y=ya;
xEnd=xb;
}
putpixel(x,y,15);
while(x<xEnd)
{
x++;
if(p<0)
p+=twoDy;
else
{
y++;
p+=twoDyDx;
}
putpixel(x,y,15);
}
}
//Circle drawing
void circleMidPoint(int xCenter,int yCenter,int radius)
{
int x=0;
int y=radius;
int p=1-radius;
void circlePlotPoints(int,int,int,int);
/*plot first set of points*/
circlePlotPoints(xCenter,yCenter,x,y);
while(x<y)
{
x++;
if(p<0)
p+=2*x+1;
else
{
y–;
p+=2*(x-y)+1;
}
circlePlotPoints(xCenter,yCenter,x,y);
}
}
void circlePlotPoints(int xCenter,int yCenter,int x,int y)
{
putpixel(xCenter+x,yCenter+y,WHITE);
putpixel(xCenter-x,yCenter+y,WHITE);
putpixel(xCenter+x,yCenter-y,WHITE);
putpixel(xCenter-x,yCenter-y,WHITE);
putpixel(xCenter+y,yCenter+x,WHITE);
putpixel(xCenter-y,yCenter+x,WHITE);
putpixel(xCenter+y,yCenter-x,WHITE);
putpixel(xCenter-y,yCenter-x,WHITE);
}//Ellipse drawing
void ellipsemidpoint(int xcenter,int ycenter,int rx,int ry)
{
int rx2=rx*rx;
int ry2=ry*ry;
int tworx2=2*rx2;
int twory2=2*ry2;
int p,x=0,y=ry,px=0;
int py=tworx2*y;
void ellipseplotpoints(int,int,int,int);
ellipseplotpoints(xcenter,ycenter,x,y);
p=ROUND(ry2-(rx2*ry)+(0.25*rx2));
while(px<py)
{
x++;
px+=twory2;
if(p<0)
p+=ry2+px;
else
{
y–;
py-=tworx2;
p+=ry2+px-py;
}
ellipseplotpoints(xcenter,ycenter,x,y);
}
p=ROUND(ry2*(x+0.5)*(x+0.5)+rx2*(y-1)*(y-1)-rx2*ry2);
while(y>0)
{
y–;
py-=tworx2;
if(p>0)
p+=rx2-py;
else
{
x++;
px+=twory2;
p+=rx2-px+px;
}
ellipseplotpoints(xcenter,ycenter,x,y);
}
}
void ellipseplotpoints(int xcenter,int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,5);
putpixel(xcenter-x,ycenter+y,5);
putpixel(xcenter+x,ycenter-y,5);
putpixel(xcenter-x,ycenter-y,5);
}
void main()
{
int ch,c;
co:
clrscr();
printf(“\n\t\tBRESENHAM BDRAWINGS\n”);
printf(“\n\t\t1-Line drawing”);
printf(“\n\t\t2-Circle drawing”);
printf(“\n\t\t3-Ellipse drawing”);
printf(“\n\t\t4-Exit”);
printf(“\nEnter your choice :”);
scanf(“%d”,&ch);
int gdriver = DETECT, gmode;
initgraph(&gdriver, &gmode, ” “);
switch(ch)
{
case 1:
int x1,y1,x2,y2;
printf(“Enter the starting co-ordinates: “);
scanf(“%d %d”,&x1,&y1);
printf(“Enter the ending co-ordinates: “);
scanf(“%d %d”,&x2,&y2);
lineBres(x1,y1,x2,y2);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
if (c==1)
goto co;
break;
case 2:
int xc1,yc1,r;
printf(“Enter the centre co-ordinates: “);
scanf(“%d %d”,&xc1,&yc1);
printf(“Enter the radius: “);
scanf(“%d”,&r);
circleMidPoint(xc1,yc1,r);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
goto co;
break;
case 3:
int xc,yc,rx,ry;
printf(“Enter the value of xcenter and ycenter co-ordinates: “);
scanf(“%d %d”,&xc,&yc);
printf(“Enter the radius of x and y: “);
scanf(“%d %d”,&rx,&ry);
ellipsemidpoint(xc,yc,rx,ry);
getch();
printf(“\n\n\n\n\n\n\n\n\nDo u continue(1-yes,0-no):”);
scanf(“%d”,&c);
if (c==1)
goto co;
break;
case 4: break;
}
}
output
Result:
Thus the program to draw. Implementation of Line, Circle and ellipse
Attributes was executed successfully.
2D TRANSFORMATION
EX NO 3
Aim:
To perform the 2D transformation
such as translation, rotation, scaling, shearing,
Reflection
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step1. Declare the variables xa,ya,xa1,ya1 of
array type.
Step2.Declare the variables
gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3. Initialise the graphics function.
Step4. Input the number of points.
Step5. Input the value of co-ordinate according to
number of points.
Step6. Using switch statement selects the option
to perform
translation, rotation, scaling, reflection and shearing.
Step7. Translation:
a).input
the translation vector
b).add
the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using
the function line,display the object before and after translation.
Step8. Rotation:
a).
input the rotation angle
b).
using formula theta=(theta*3.14)/180
c).input
the value of reference point
d).
calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the
object before and after rotation.
Step9. Scaling:
a).input the scaling factor and reference
point
b).calculate new coordinate point using
formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the
object before and after scaling.
Step10. Shearing:
a).input the shearing value and
reference point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display
the object before and after shearing.
Step11. Reflection:
a).display the object before
reflection using the function line
b). display the object after reflection
using the function line
Step12. Stop.
Program:
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<stdlib.h>
void main()
{
int
gd,gm,n,i,xa[10],ya[10],op,tx,ty,xa1[10],ya1[10],theta,xf,yf,rx,ry,sx,sy,shx,shy,xref,yref;
char d;
gd=DETECT;
initgraph(&gd,&gm,"");
cout<<"enter the no of points";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"enter the
coordinates"<<i+1;
cin>>xa[i]>>ya[i];
}
do
{
cout<<"menu";
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.shearing\n5.reflection\n6.exit";
cin>>op;
switch(op)
{
case 1:
cout<<"enter the translation vector";
cin>>tx>>ty;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+tx;
ya1[i]=ya[i]+ty;
}
cout<<"before translation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after translation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the reference
points";
cin>>xf>>yf;
for(i=0;i<n;i++)
{
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta);
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta);
}
cout<<"before rotation";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after rotation";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor";
cin>>sx>>sy;
cout<<"enter the reference point";
cin>>rx>>ry;
for(i=0;i<n;i++)
{
xa1[i]=xa[i]*sx+rx*(1-sx);
ya1[i]=ya[i]*sy+ry*(1-sy);
}
cout<<"before scaling";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after scaling";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 4:
cout<<"enter the shear value";
cin>>shx>>shy;
cout<<"enter the reference point";
cin>>xref>>yref;
cout<<"enter the shear direction x or
y";
cin>>d;
if(d=='x')
{
for(i=0;i<n;i++)
{
xa1[i]=xa[i]+shx*(ya[i]-yref);
ya1[i]=ya[i];
}
}
cout<<"before shearing";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after shearing";
for(i=0;i<n;i++)
{
line(xa1[i],ya1[i],xa1[(i+1)%n],ya1[(i+1)%n]);
}
getch();
cleardevice();
break;
case 5:
cout<<"before reflection";
for(i=0;i<n;i++)
{
line(xa[i],ya[i],xa[(i+1)%n],ya[(i+1)%n]);
}
cout<<"after reflection";
for(i=0;i<n;i++)
{
line(ya[i],xa[i],ya[(i+1)%n],xa[(i+1)%n]);
}
getch();
cleardevice();
break;
case 6:
exit(0);
break;
}
}while(op!=6);
}
Input &
Output:
enter the no of points:3
enter the coordinates 1:50 150
enter the coordinates 2:50 50
enter the coordinates 3:75 150
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit1
enter the translation vector:30 40
Before translation After
Translation
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 2
enter the rotation angle:40
enter the reference points:100 100
before rotation after rotation
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 3
Enter the scaling factor: 3 4
Enter the reference points: 30 40
Before scaling
after
scaling
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 4
Enter the shear value: 3 4
Enter the reference point: 20 30
Enter the shear direction x or y: X
Before shearing After shearing
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 5
Before reflection after reflection
menu
1. translation
2. rotation
3. scaling
4.shearing
5.reflection
6.exit 6
Result:
Thus the program for 2D transformation was executed successfully
Composite 2D Transformations
Exno(4)
Aim:
To perform
the Composite 2D Transformations
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step1. Declare the variables xa,ya,xa1,ya1 of
array type.
Step2.Declare the variables
gd,gm,n,i,op,tx,ty,xf,yf,rx,ry.
Step3. Initialise the graphics function.
Step4. Input the number of points.
Step5. Input the value of co-ordinate according to
number of points.
Step6. Using switch statement selects the option
to perform
translation, rotation, scaling, reflection and shearing.
Step7.
a).input
the translation vector
b).add
the translation vectors with the coordinates
xa1[i]=xa[i]=tx, ya1[i]=ya[i]=ty,
c).using
the function line,display the object before and after translation.
Step8.
a).
input the rotation angle
b).
using formula theta=(theta*3.14)/180
c).input
the value of reference point
d).
calculate new coordinate point using formula
xa1[i]=xf+(xa[i]-xf)*cos(theta)-(ya[i]-yf)*sin(theta),
ya1[i]=yf+(xa[i]-xf)*sin(theta)-(ya[i]-yf)*cos(theta),
e). using the function line,display the
object before and after rotation.
Step9.
a).input the scaling factor and reference
point
b).calculate new coordinate point using
formula
xa1[i]=(xa[i]*sx+rx*(1-sx),
ya1 [i] = (ya[i]*sy+ry*(1-sy)
c). using the function line, display the
object before and after scaling.
Step10.: Composite
2D Transformations
a).input the shearing value and reference
point.
b). input the shear direction x or y
i).if direction x
xa1[i]=xa[i]+shx*(ya[i]-yref)
ii).otherwise
ya1[i]=ya[i]+shy*(xa[i]-xref)
iii). using the function line, display
the object before and after shearing.
Step11.
a).display the object before
reflection using the function line
b). display the object after reflection
using the function line
Step12. Stop.
Program
#include<process.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void disp(int n,float c[][3])
{
float maxx,maxy;
int i;
maxx=getmaxx();
maxy=getmaxy();
maxx=maxx/2;
maxy=maxy/2;
i=0;
while(i<n-1)
{
line(maxx+c[i][0],maxy-c[i][1],maxx+c[i+1][0],maxy-c[i+1][1]);
i++;
}
i=n-1;
line(maxx+c[i][0],maxy-c[i][1],maxx+c[0][0],maxy-c[0][1]);
setcolor(GREEN);
line(0,maxy,maxx*2,maxy);
line(maxx,0,maxx,maxy*2);
setcolor(WHITE);
}
void mul(int n,float b[][3],float c[][3],float a[][3])
{
int i,j,k;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
a[i][j]=0;
for(i=0;i<n;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
a[i][j] = a[i][j] + (c[i][k] * b[k][j]);
}
}
void translation(int n,float c[][3],float tx,float ty)
{
int i;
for(i=0;i<n;i++)
{
c[i][0]=c[i][0]+tx;
c[i][1]=c[i][1]+ty;
}
}
}
void scaling(int n,float c[][3],float sx,float sy)
{
float b[10][3],a[10][3];
int i=0,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=sx;
b[1][1]=sy;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void rotation(int n,float c[][3],float ra)
{
int i=0,j;
float b[10][3],xp,yp,a[10][3];
xp=c[0][0];
yp=c[0][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
b[i][j]=0;
b[0][0]=b[1][1]=cos(ra*3.14/180);
b[0][1]=sin(ra*3.14/180);
b[1][0]=-sin(ra*3.14/180);
b[2][0]=(-xp*cos(ra*3.14/180))+(yp*sin(ra*3.14/180))+xp;
b[2][1]=(-xp*sin(ra*3.14/180))-(yp*cos(ra*3.14/180))+yp;
b[2][2]=1;
mul(n,b,c,a);
setcolor(RED);
disp(n,a);
}
void main()
{
int i,j,k,cho,n,gd=DETECT,gm;
float c[10][3],tx,ty,sx,sy,ra;
initgraph(&gd,&gm,”d:\\turboc~1\\bgi”);
printf(“\nEnter the number of vertices : “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\nEnter the co-ordinates of the %d vertex :”,i+1);
scanf(“%f%f”,&c[i][0],&c[i][1]);
c[i][2]=1;
}
do
{
clrscr();
cleardevice();
printf(“\n\t\t\t * * * MENU * * *”);
printf(“\n\t 1) TRANSLATION”);
printf(“\n\t 2) SCALING “);
}while(cho!=4);
getch();
closegraph();
}
OUTPUT:
Before scaling after
scaling
Result:
Thus the program for Composite 2D
transformation was executed successfully
Cohen-Sutherland
Clipping& Windowing
EXNO 5
Aim:
To implement Cohen-Sutherland clipping& WindowingAlgorithm.
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Setcolor().
This function changes the drawing
colour.
Syntax:
Setcolor(value of the color)
Settextstyle().
The function
settextstyle() is used to change the style of the text.
Syntax:
Settextstyle(font,direction,charsize)
Where font is the
constant value or the font filename, direction is the number either 0 or 1,
which makes the output to display in horizontal, or in vertical direction,
charsize is the character size or magnification factor and it varies from 1 to
10.
Outtext().
This function display a text message on upper left
of the screen
Syntax:
Outtext(“message”);
Algorithm:
Step 1. Create a class sulc with functions
drawwindow, drawline, setcode, visibility and
reset endpoint.
Step 2. Using the function line set the parameters
to draw window.
Step 3. Using the function defined in class sulc,
setcode is used to save the line inside the
window and to the line outside the window.
Step 4. Using the function visibility
i).check the code to know the points inside or outside the window.
ii).if the code value is zero the point is inside the window.
Step 5. Using the function reset end point
i). if the code value for the line is outside the window.
ii).reset the endpoint to the boundary of the window.
Step 6. Initialize the graphics functions
Step 7. Declare the variables x1, x2, y1, y2 of
array type.
Step 8. Get the value of two endpoints x1, y1 and
x2, y2 to draw the line.
Step 9. Using the object c, display the window
before clipping.
Step 10. Using the function setcode, visibility
display the clipped window only with lines
inside the window class was displayed after clipping.
Program:
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdlib.h>
typedef struct coord
{
int x,y;
char code[4];
}pt;
class sulc
{
public:
void drawwindow();
void drawline(pt p1,pt p2,int c1);
pt setcode(pt p);
int visibility(pt p1,pt p2);
pt resetendpt(pt p1,pt p2);
};
void sulc::drawwindow()
{
setcolor(WHITE);
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void sulc::drawline(pt p1,pt p2,int c1)
{
setcolor(c1);
line(p1.x,p1.y,p2.x,p2.y);
}
pt sulc::setcode(pt p)
{
pt ptemp;
if(p.y<100)
ptemp.code[0]='1';
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1';
else
ptemp.code[1]='0';
if(p.y>450)
ptemp.code[2]='1';
else
ptemp.code[2]='0';
if(p.y<150)
ptemp.code[3]='1';
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;
return(ptemp);
}
int sulc::visibility(pt p1,pt p2)
{
int i,flag=0;
for(i=0;i<4;i++)
{
if((p1.code[i]!='0')||(p2.code[i]!='0'))
flag=1;
}
if(flag==0)
return(0);
for(i=0;i<4;i++)
{
if((p1.code[i]==p2.code[i])&&(p1.code[i]=='1'))
flag=0;}
if(flag==0)
return(1);
return(2);
}
pt sulc::resetendpt(pt p1,pt p2)
{
pt temp;
int x,y,i;
float m,k;
if(p1.code[3]=='1')
x=150;
if(p1.code[2]=='1')
x=450;
if((p1.code[3]=='1')||(p1.code[2]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(p1.y+(m*(x-p1.x)));
temp.y=k;
temp.x=x;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
if(p1.code[0]=='1')
y=100;
if(p1.code[1]=='1')
y=350;
if((p1.code[0]=='1')||(p1.code[1]=='1'))
{
m=(float)(p2.y-p1.y)/(p2.x-p1.x);
k=(float)p1.x+(float)(y-p1.y)/m;
temp.x=k;
temp.y=y;
for(i=0;i<4;i++)
temp.code[i]=p1.code[i];
if(temp.y<=350 &&temp.y>=100)
return(temp);
}
else
return(p1);
}
void main()
{
int gd=DETECT,gm,v;
sulc c1;
pt p1,p2,ptemp;
initgraph(&gd,&gm,"");
int x1[10],y1[10],x2[10],y2[10];
cleardevice();
int i,n;
settextstyle(4,0,4);
outtext("cohen sutherland line
clipping");
cout<<"\n\n enter the no.of
lines:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"\n\n enter
end-point1(x1,y1):";
cin>>x1[i]>>y1[i];
cout<<"\n\n enter
end-point2(x2,y2):";
cin>>x2[i]>>y2[i];
}
cleardevice();
settextstyle(0,0,3);
outtext("before clipping");
c1.drawwindow();
for(i=0;i<n;i++)
{
p1.x=x1[i];
p1.y=y1[i];
p2.x=x2[i];
p2.y=y2[i];
c1.drawline(p1,p2,15);
}
getch();
cleardevice();
settextstyle(0,0,3);
outtext("after clipping");
for(i=0;i<n;i++)
{
p2.x=x2[i];
p2.y=y2[i];
p1=c1.setcode(p1);
p2=c1.setcode(p2);
v=c1.visibility(p1,p2);
switch(v)
{
case 0:
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
case 1:
c1.drawwindow();
break;
case 2:
p1=c1.resetendpt(p1,p2);
p2=c1.resetendpt(p2,p1);
c1.drawwindow();
c1.drawline(p1,p2,15);
break;
}
}
getch();
closegraph();
}
Input:
Enter the no.of lines: 1
Enter end-point1(x1,y1):30 40
Enter end-point1(x2,y2):300 400
Output:
Before
clipping
After clipping
Result:
Thus the program to implement Cohen-Sutherland
clipping& Windowing is
executed
successfully
Sutherland – Hodgeman Polygon clipping
Algorithm
EX NO. (6)
Aim:
To implement Sutherland – Hodgeman Polygon clipping Algorithm
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Setcolor().
This function changes the drawing
colour.
Syntax:
Setcolor(value of the color)
Settextstyle().
The function
settextstyle() is used to change the style of the text.
Syntax:
Settextstyle(font,direction,charsize)
Where font is the
constant value or the font filename, direction is the number either 0 or 1,
which makes the output to display in horizontal, or in vertical direction,
charsize is the character size or magnification factor and it varies from 1 to
10.
Outtext().
This function display a text message on upper left
of the screen
Syntax:
Outtext(“message”);
Algorithm:
Step 1. Create a class sulc with functions
drawwindow, drawline, setcode, visibility and
reset endpoint.
Step 2. Using the function line set the parameters
to draw window.
Step 3. Using the function defined in class sulc,
setcode is used to save the line inside the
window and to the line outside the window.
Step 4. Using the function visibility
i).check the code to know the points inside or outside the window.
ii).if the code value is zero the point is inside the window.
Step 5. Using the function reset end point
i). if the code value for the line is outside the window.
ii).reset the endpoint to the boundary of the window.
Step 6. Initialize the graphics functions
Step 7. Declare the variables x1, x2, y1, y2 of
array type.
Step 8. Get the value of two endpoints x1, y1 and
x2, y2 to draw the line.
Step 9. Using the object c, display the window
before clipping.
Step 10. Using the function setcode, visibility
display the clipped window only with lines
inside the window class was displayed after clipping.
Program:
#include<conio.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
int pixels[2][4];
float xn1,xn2,yn1,yn2,x3,y3,m;
int xmin,ymin,xmax,ymax,x1,y1,x2,y2;
int choice,ed[20],num;
void su_co(int x1,int y1,int x2,int y2,int xmin,int ymin,int xmax,int ymax)
{
int i,j,f1;
for(i=0;i<2;i++)
for(j=0;j<4;j++)
pixels[i][j]=0;
if(y1>ymax)
pixels[0][0]=1;
if(y1<ymin)
pixels[0][1]=1;
if(x1>xmax)
pixels[0][2]=1;
if(x1<xmin)
pixels[0][3]=1;
if(y2>ymax)
pixels[1][0]=1;
if(y2<ymin)
pixels[1][1]=1;
if(x2>xmax)
pixels[1][2]=1;
if(x2<xmin)
pixels[1][3]=1;
for(j=0;j<4;j++)
{
if(pixels[0][j]==0&&pixels[1][j]==0)
continue;
if(pixels[0][j]==1&&pixels[1][j]==1)
{
f1=3;
break;
}
f1=2;
}
switch(f1)
{
case 1:line(320+x1,240-y1,320+x2,240-y2);
break;
case 3:printf(“\n\n\t\”LINE IS NOT VISIBLE..\”");
break;
case 2:m=(y2-y1)/(x2-x1);
xn1=x1;
yn1=y1;
xn2=x2;
yn2=y2;
if(pixels[0][0]==1)
{
xn1=x1+(ymax-y1)/m;
yn1=ymax;3
}
if(pixels[0][1]==1)
{
xn1=x1+(ymin-y1)/m;
yn1=ymin;
}
if(pixels[0][2]==1)
{
yn1=y1+(xmax-x1)*m;
xn1=xmax;
}
if(pixels[0][3]==1)
{
yn1=y1+(xmin-x1)*m;
xn1=xmin;
}
if(pixels[1][0]==1)
{
xn2=x2+(ymax-y2)/m;
yn2=ymax;
}
if(pixels[1][1]==1)
{
xn2=x2+(ymin-y2)/m;
yn2=ymin;
}
if(pixels[1][2]==1)
{
yn2=y2+(xmax-x2)*m;
xn2=xmax;
}
if(pixels[1][3]==1)
{
yn2=y2+(xmin-x2)*m;
xn2=xmin;
}
line(320+xn1,240-yn1,320+xn2,240-yn2);
break;
}
}
void cohen()
{
clearviewport();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
line(320+x1,240-y1,320+x2,240-y2);
getch();
cleardevice();
line(320+xmin,240-ymin,320+xmin,240-ymax);
line(320+xmin,240-ymax,320+xmax,240-ymax);
line(320+xmax,240-ymax,320+xmax,240-ymin);
line(320+xmax,240-ymin,320+xmin,240-ymin);
su_co(x1,y1,x2,y2,xmin,ymin,xmax,ymax);
getch();
}
void main()
{
int gd=DETECT,gm,i,j;
initgraph(&gd,&gm,”..\\bgi”);
printf(“\n\n\t\t\”Enter the coordinate of the Clipping Window\”");
printf(“\n\n\t\t\”Enter X(min),Y(min)\”:=”);
scanf(“%d%d”,&xmin,&ymin);
printf(“\n\n\t\t\”Enter X(max),Y(max)\”:=”);
scanf(“%d%d”,&xmax,&ymax);
printf(“\n\n\t\tEnter the coordinates of the Line”);
printf(“\n\n\t\t Enter X(1) & Y(1):”);
scanf(“%d%d”,&x1,&y1);
printf(“\n\n\t\t Enter X(2) & Y(2):”);
scanf(“%d%d”,&x2,&y2);
clrscr();
cohen();
}
Output:
Result
Sutherland –
Hodgeman Polygon clipping Algorithm is
executed
3D-
Transformation
EXNO(7)
Aim:
To perform 3D
transformations such as translation, rotation and scaling.
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1. Create a class cube with function draw
cube.
Step 2. Use the function draw cube to draw a cube
using eight points by means of
functions line.
Step 3. Declare the variables x1, y1, x2, y2, x3,
y3, in array type which of data type int.
Step 4.Declare the variables
theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5.Initialise graphics functions.
Step 6.Input the first point in the cube.
Step 7.Input the size of the edge.
Step 8.Create an object to call the function.
Step 9.Using switch operation select the operation
to perform translation, rotation,scaling.
Step 10.Translation
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
c).using
the function line, display the object before and after translation.
Step11. Rotation:
a).
input the rotation angle
b).
using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d).
if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
if the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
e).calculate the points using the
formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f).
using the function line,display the object before and after rotation.
Step12. Scaling:
a).input the scaling factor and
reference point
b).calculate coordinate point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d). using the function line, display the
object before and after scaling.
Step13. Stop.
Program:
#include<iostream.h>
#include<graphics.h>
#include<math.h>
#include<conio.h>
#include<stdlib.h>
class cube
{
public:
void drawcube(int x1[],int y1[])
{
int i;
for(i=0;i<4;i++)
{
if(i<3)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[0],y1[0],x1[3],y1[3]);
}
for(i=4;i<8;i++)
{
if(i<7)
line(x1[i],y1[i],x1[i+1],y1[i+1]);
line(x1[4],y1[4],x1[7],y1[7]);
}
for(i=0;i<4;i++)
{
line(x1[i],y1[i],x1[i+4],y1[i+4]);
}
}
};
void main()
{
int i,x1[8],y1[8],x2[8],y2[8],z1[8],x3[8],y3[8],z3[8],x4[8],y4[8],theta,op,ch,tx,ty,tz,sx,sy,sz,xf,yf,zf,x,y,z,size;
int driver=DETECT;
int mode;
initgraph(&driver,&mode,"");
cout<<"enter the points on the
cube:";
cin>>x>>y>>z;
cout<<"enter the size of the edge:";
cin>>size;
x1[0]=x1[3]=x;
x1[1]=x1[2]=x+size;
x1[4]=x1[7]=x;
x1[5]=x1[6]=x+size;
y1[0]=y1[1]=y;
y1[2]=y1[3]=y+size;
y1[4]=y1[5]=y;
y1[6]=y1[7]=y+size;
z1[1]=z1[2]=z1[3]=z1[0]=z ;
z1[4]=z1[5]=z1[6]=z1[7]=z-size;
for(i=0;i<8;i++)
{
x2[i]=x1[i]+z1[i]/2;
y2[i]=y1[i]+z1[i]/2;
}
cube c;
getch();
cleardevice();
do
{
cout<<"menu"<<endl;
cout<<"\n1.translation\n2.rotation\n3.scaling\n4.exit\n";
cout<<"enter the choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"enter the translation
vector:";
cin>>tx>>ty>>tz;
for(i=0;i<8;i++)
{
x3[i]=x1[i]+tx;
y3[i]=y1[i]+ty;
z3[i]=z1[i]+tz;
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before translation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after translation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 2:
cout<<"enter the rotation angle:";
cin>>theta;
theta=(theta*3.14)/180;
cout<<"enter the
direction"<<endl;
cout<<"1.rotation about x
axis"<<endl<<"2.rotation about y
axis"<<endl<<"3.rotation about z axis";
cin>>op;
if(op==1)
{
for(i=0;i<8;i++)
{
x3[i]=x1[i];
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta);
z3[i]=y1[i]*sin(theta)+z1[i]*cos(theta);
}
}
else
if(op==2)
{
for(i=0;i<8;i++)
{
y3[i]=y1[i];
x3[i]=z1[i]*cos(theta)-x1[i]*sin(theta);
x3[i]=z1[i]*sin(theta)+x1[i]*cos(theta);
}
}
else
if(op==3)
{
for(i=0;i<8;i++)
{
z3[i]=z1[i];
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta);
y3[i]=x1[i]*sin(theta)+y1[i]*cos(theta);
}
}
else
cout<<"enter correct option";
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before rotation";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after rotation";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 3:
cout<<"enter the scaling factor:";
cin>>sx>>sy>>sz;
cout<<"enter the reference
point:";
cin>>xf>>yf>>zf;
for(i=0;i<8;i++)
{
x3[i]=xf+(x1[i]*sx)+xf*(1-sx);
y3[i]=yf+(y1[i]*sy)+yf*(1-sy);
z3[i]=zf+(z1[i]*sz)+zf*(1-sz);
}
for(i=0;i<8;i++)
{
x4[i]=x3[i]+z3[i]/2;
y4[i]=y3[i]+z3[i]/2;
}
cleardevice();
cout<<"before scaling";
c.drawcube(x2,y2);
getch();
cleardevice();
cout<<"after scaling";
c.drawcube(x4,y4);
getch();
cleardevice();
break;
case 4:
exit(0);
break;
}
}
while(op!=4);
getch();
}
Input
Enter the point in the cube: 100 100 100
Enter the size of the edge: 50
Menu
1. translation
2. rotation
3. scaling
4. exit
Enter the choice:1
Enter the translation vector: 5,10,15
Before translation After
translation
Result:
Thus the program for 3D transformation was executed successfully.
Composite 3D
transformations
EXno(8)
Aim:
To perform. Composite 3D transformations
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step 1. Create a class cube with function draw
cube.
Step 2. Use the function draw cube to draw a cube
using eight points by means of
functions line.
Step 3. Declare the variables x1, y1, x2, y2, x3,
y3, in array type which of data type int.
Step 4.Declare the variables
theta,op,ch,tx,ty,sx,sy,sz,lz+xy,zf,i,x,y,z.
Step 5.Initialise graphics functions.
Step 6.Input the first point in the cube.
Step 7.Input the size of the edge.
Step 8.Create an object to call the function.
Step 9.Using switch operation select the operation
to perform translation, rotation,scaling.
Step 10.
a).input the translation vectortx,ty,tz.
b).calculate points using formula
x3[i]=x1[i]+tx.
y3[i]=y1[i]+ty
z3[i]=z1[i]+tz.
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
c).using
the function line, display the object before and after translation.
Step11.
a).
input the rotation angle
b).
using formula theta=(theta*3.14)/180
c).input the direction in x,y,z axis
d).
if the direction is along x axis,
x3[i]=x1[i].
y3[i]=y1[i]*cos(theta)-z1[i]*sin(theta),
y3[i]=y1[i]*sin(theta)-z1[i]*cos(theta),
if the direction is along yaxis,
y3[i]=y1[i].
z3[i]=z1[i]*cos(theta)-x1[i]*sin(theta),
x3[i]=z1[i]*sin(theta)-x1[i]*cos(theta),
if the direction is along z axis,
z3[i]=z1[i].
x3[i]=x1[i]*cos(theta)-y1[i]*sin(theta),
y3[i]=x1[i]*sin(theta)-y1[i]*cos(theta),
e).calculate the points using the
formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
f).
using the function line,display the object before and after rotation.
Step12.
a).input the scaling factor and
reference point
b).calculate coordinate point using formula
x3[i]=xf+(x1[i]*sx+xf*(1-sx),
y3 [i] =yf+ (y1[i]*sy+yf*(1-sy)
z3 [i] =zf+ (z1[i]*sz+zf*(1-sz)
c). calculate the points using the formula
x4[i]=x3[i]+z3[i]/2
y4[i]=y3[i]+z3[i]/2
d). using the function line, display the
object before and after scaling.
Step13. Stop.
Program:
#include<stdio.h>#include<conio.h>
#include<math.h>
#include<stdlib.h>
#include<dos.h>
#include<iostream.h>
#include<graphics.h>
int main(void)
{
int gdriver=DETECT,gmode;
int xmax,ymax,tx,ty,tz,i,n,x[20],y[20],z[20],a[20],b[20],choice;
initgraph(&gdriver,&gmode,”h:\cplus”);
xmax=getmaxx();
ymax=getmaxy();
line(xmax/2,0,xmax/2,ymax/2);
line(xmax/2,ymax/2,xmax,ymax/2);
line(xmax/2,ymax/2,0,ymax);
cout<<”\nEnter the no. of sides:”;
cin>>n;
void draw(int x[],int y[],int z[],int n);
cout<<”\n\n\n Enter your choice\n\n 1.Translation \n\n 2.rotation”;
cin>>choice;
switch(choice)
{
case 1: cout<<”you have selected translation:\n”;
for(i=0;i<n;i++)
{
cin>>x[i]>>y[i]>>z[i];
x[i]+=xmax/2;
y[i]=ymax/2-y[i];
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);
}
cout<<”Given the translation vector:”;
cin>>tx>>ty>>tz;
for(i=0;i<n;i++)
{
x[i]=x[i]+tx;
y[i]=y[i]+ty;
z[i]=z[i]+tz;
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);
}
break;
case 2: cout<<”you have chosen rotation:”;
{
int a[10],b[10],th;
cout<<”\n\n Enter the coordinates”;
for(int i=0;i<n;i++)
{
cin>>x[i]>>y[i]>>z[i];
x[i]+=xmax/2;
y[i]=ymax/2-y[i];
a[i]=x[i]-z[i];
b[i]=y[i]-z[i];
}
for(i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(x[i]-z[i],y[i]-z[i],x[(i+1)%n]-z[(i+1)%n],y[(i+1)%n]-z[(i+1)%n]);
line(x[i],y[i],x[i]-z[i],y[i]-z[i]);
}
cout<<”Enter the angle:”;
cin>>th;
int t=th*3.14/180;
for(int tta=0;tta<t;tta+=3.14)
{
for(i=1;i<n;i++)
{
int xx=x[i];
x[i]=x[0]+(x[i]-x[0])*cos(t)-(y[i]-y[0])*sin(t);
y[i]=y[0]+(xx-x[0])*sin(t)+(y[i]-y[0])*cos(t);
xx=a[i];
a[i]=a[0]+(a[i]-a[0])*cos(t)-(b[i]-b[0])*sin(t);
b[i]=b[0]+(xx-a[0])*sin(t)+(b[i]-b[0])*cos(t);
}
for(int i=0;i<n;i++)
{
line(x[i],y[i],x[(i+1)%n],y[(i+1)%n]);
line(a[i],b[i],a[(i+1)%n],b[(i+1)%n]);
line(x[i],y[i],a[i],b[i]);
}
delay(500);
}
break;
}
case 3: break;
}
getch();
closegraph();
return 0;
}
Output:
Enter the choice:1
Enter the translation vector: 5,10,25
Before translation After
translation
Result:
Thus the program for Composite 3D
transformations as executed successfully.
Drawing three
dimensional objects and Scenes
EXNO(9)
Aim:
To write
program to visualize the Drawing three dimensional objects and Scenes
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step
1. Create a class parallel.
Step 2. Create a
constructor parallel
i).initialize graphics
Step 3. Create a
function initialize
i).draw the x,y,z axis
Step 4. Create a
function projection
i).get the reference angle alpha as j.
ii).assign the value of k as 45.
iii).compute the following
fi=(3.14/180)*k;
a1=(3.14/180)*j;
z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
iv).Calculate
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
v).compute the following
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
vi).
calculate
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
vii).compute the values to screen coordinate value.
viii).join the coordinate using line function
ix).display the projected object.
Program
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
class parallel
{
public:
int a,k;
int gd,gm;
int px[8],py[8],pz[8],px1[8],py1[8],pz1[8],px3[8],py3[8];
parallel();
void initialize();
void drawobj();
void proj(int);
};
parallel::parallel()
{
gd=DETECT;
initgraph(&gd,&gm,"");
}
void parallel::initialize()
{
px1[1]=100,py1[1]=100,pz1[1]=0;
px1[2]=200,py1[2]=100,pz1[2]=0;
px1[3]=200,py1[3]=200,pz1[3]=0;
px1[4]=100,py1[4]=200,pz1[4]=0;
px1[5]=100,py1[5]=100,pz1[5]=100;
px1[6]=200,py1[6]=100,pz1[6]=100;
px1[7]=200,py1[7]=200,pz1[7]=100;
px1[8]=100,py1[8]=200,pz1[8]=100;
}
void parallel::drawobj()
{
setcolor(WHITE);
line(px1[1],py1[1],px1[2],py1[2]);
line(px1[2],py1[2],px1[3],py1[3]);
line(px1[3],py1[3],px1[4],py1[4]);
line(px1[4],py1[4],px1[8]+50,py1[8]+50);
line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);
line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);
line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);
line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);
line(px1[1],py1[1],px1[5]+50,py1[5]+50);
line(px1[2],py1[2],px1[6]+50,py1[6]+50);
line(px1[3],py1[3],px1[7]+50,py1[7]+50);
line(px1[4],py1[4],px1[1],py1[1]);
getch();
}
void parallel::proj(int j)
{
cleardevice();
int z,xp,yp;
int dx,dy;
float a1,fi,i,i1,i2;
k=45;
fi=(3.14/180)*k;
a1=(3.14/180)*j;
z=pz1[1];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[1]=px1[1]+i1;
py3[1]=py1[1]+i2;
px3[2]=px1[2]+i1;
py3[2]=py1[2]+i2;
px3[3]=px1[3]+i1;
py3[3]=py1[3]+i2;
px3[4]=px1[4]+i1;
py3[4]=py1[4]+i2;
z=pz1[5];
i=z/tan(a1);
i1=floor(i*cos(fi));
i2=floor(i*sin(fi));
px3[5]=px1[5]+i1;
py3[5]=py1[5]+i2;
px3[6]=px1[6]+i1;
py3[6]=py1[6]+i2;
px3[7]=px1[7]+i1;
py3[7]=py1[7]+i2;
px3[8]=px1[8]+i1;
py3[8]=py1[8]+i2;
cout<<"enter the projected
object";
line(px3[1],py3[1],px3[2],py3[2]);
line(px3[2],py3[2],px3[3],py3[3]);
line(px3[3],py3[3],px3[4],py3[4]);
line(px3[4],py3[4],px3[1],py3[1]);
line(px3[5],py3[5],px3[6],py3[6]);
line(px3[6],py3[6],px3[7],py3[7]);
line(px3[7],py3[7],px3[8],py3[8]);
line(px3[8],py3[8],px3[5],py3[5]);
line(px3[1],py3[1],px3[5],py3[5]);
line(px3[2],py3[2],px3[6],py3[6]);
line(px3[3],py3[3],px3[7],py3[7]);
line(px3[4],py3[4],px3[8],py3[8]);
}
void main()
{
parallel p;
int a;
char c='y';
clrscr();
cleardevice();
while(c=='y'||c=='y')
{
cleardevice();
p.initialize();
p.drawobj();
cout<<"enter the reference
angle\ny";
cin>>a;
p.proj(a);
cout<<"do u want to
continue(y/n)";
cin>>c;
}
cleardevice();
}
Output:
Result:
Thus the
program to visualize the Drawing three dimensional objects and Scenes
was executed successfully.
Generating
Fractal images
EX NO(10)
Aim:
To write
program to draw the Generating
Fractal images
Functions
used:
Line()
The function line() is used to draw a
line from(x1,y1)to (x2,y2)
Syntax:
line (x1,y1,x2,y2)
initgraph().
This function takes thee arguments and
they are
i).the video driver to be used (gd).
ii).the graphics mode (gm).
iii).the path name.
Syntax:
Initgraph(gd,gm,path)
Algorithm:
Step1:Pres()
i).Initialise graphics.
Step 2: Initialize()
i).Draw x,y,z axis
ii).Number all axis with regular interval 1 to 8
step 3:Draw-obj
i). Get the value of each p6 of 3D object
ii).Convert the value to screen coordinate values
iii).Join the coordinate using the function.
Step 4:Proj()
i).Get the reference point in z-axis as j
ii).Get the position of the view plane as k
iii).Assign the values for Zprp=j and Zrp=k
iv).Compute
z=pz1[1];
u=(zprp-zvp)/(zprp-z);
v).Calculate
px3[1]=px1[1]*u;
py3[1]=py1[1]*u;
px3[2]=px1[2]*u;
py3[2]=py1[2]*u;
px3[3]=px1[3]*u;
py3[3]=py1[3]*u;
px3[4]=px1[4]*u;
py3[4]=py1[4]*u;
vi).Compute
z=pz1[5];
u=(zprp-zvp)/(zprp-z);
vii).Calculate
px3[5]=px1[5]*u;
py3[5]=py1[5]*u;
px3[6]=px1[6]*u;
py3[6]=py1[6]*u;
px3[7]=px1[7]*u;
py3[7]=py1[7]*u;
px3[8]=px1[8]*u;
py3[8]=py1[8]*u;
viii).Compute
the values to screen coordinate value.
ix).Join the coordinate
using line function.
x).Display the projected object.
Program:
#include<iostream.h>.
#include<graphics.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
class pres
{
public:
int a,k;
int gd,gm;
int
px[8],py[8],pz[8],px1[8],py1[8],pz3[8],px3[8],py3[8],pz1[8];
pres();
void initialize();
void drawobj();
void proj(int,int);
};
pres::pres()
{
gd=DETECT;
initgraph(&gd,&gm,"");
}
void pres::initialize()
{
px1[1]=200,py1[1]=200,pz1[1]=100;
px1[2]=300,py1[2]=200,pz1[2]=100;
px1[3]=300,py1[3]=300,pz1[3]=100;
px1[4]=200,py1[4]=300,pz1[4]=100;
px1[5]=200,py1[5]=200,pz1[5]=200;
px1[6]=300,py1[6]=200,pz1[6]=200;
px1[7]=300,py1[7]=300,pz1[7]=200;
px1[8]=200,py1[8]=300,pz1[8]=200;
}
void pres::drawobj()
{
setcolor(WHITE);
line(px1[1],py1[1],px1[2],py1[2]);
line(px1[2],py1[2],px1[3],py1[3]);
line(px1[3],py1[3],px1[4],py1[4]);
line(px1[4],py1[4],px1[8]+50,py1[8]+50);
line(px1[8]+50,py1[8]+50,px1[5]+50,py1[5]+50);
line(px1[5]+50,py1[5]+50,px1[6]+50,py1[6]+50);
line(px1[6]+50,py1[6]+50,px1[7]+50,py1[7]+50);
line(px1[7]+50,py1[7]+50,px1[8]+50,py1[8]+50);
line(px1[1],py1[1],px1[5]+50,py1[5]+50);
line(px1[2],py1[2],px1[6]+50,py1[6]+50);
line(px1[3],py1[3],px1[7]+50,py1[7]+50);
line(px1[4],py1[4],px1[1],py1[1]);
getch();
}
void pres::proj(int j,int k)
{
cleardevice();
int z,xp,yp;
float zprp,zvp,u;
zprp=j;zvp=k;
z=pz1[1];
u=(zprp-zvp)/(zprp-z);
z=pz1[1];
px3[1]=px1[1]*u;
py3[1]=py1[1]*u;
px3[2]=px1[2]*u;
py3[2]=py1[2]*u;
px3[3]=px1[3]*u;
py3[3]=py1[3]*u;
px3[4]=px1[4]*u;
py3[4]=py1[4]*u;
z=pz1[5];
u=(zprp-zvp)/(zprp-z);
px3[5]=px1[5]*u;
py3[5]=py1[5]*u;
px3[6]=px1[6]*u;
py3[6]=py1[6]*u;
px3[7]=px1[7]*u;
py3[7]=py1[7]*u;
px3[8]=px1[8]*u;
py3[8]=py1[8]*u;
cleardevice();
cout<<"enter the projected
object";
line(px3[1],py3[1],px3[2],py3[2]);
line(px3[2],py3[2],px3[3],py3[3]);
line(px3[3],py3[3],px3[4],py3[4]);
line(px3[4],py3[4],px3[1],py3[1]);
line(px3[5],py3[5],px3[6],py3[6]);
line(px3[6],py3[6],px3[7],py3[7]);
line(px3[7],py3[7],px3[8],py3[8]);
line(px3[8],py3[8],px3[5],py3[5]);
line(px3[1],py3[1],px3[5],py3[5]);
line(px3[2],py3[2],px3[6],py3[6]);
line(px3[3],py3[3],px3[7],py3[7]);
line(px3[4],py3[4],px3[8],py3[8]);
}
void main()
{
pres p;
int a,b;
clrscr();
cleardevice();
cout<<"prespective
projection"<<endl;
p.initialize();
p.drawobj();
cout<<"enter the reference point
inz-axis"<<endl;
cin>>a;
cout<<"enter the positionof view
plane"<<endl;
cin>>b;
p.proj(a,b);
getch();
}
Output:
Enter the values for h,s and r: 0.5 0.6 0.7
R=0.28
G=0.28
B=0.7
Result:Thus
the program to draw the Generating Fractal images object was executed
successfully.
No comments:
Post a Comment