0% found this document useful (0 votes)
137 views63 pages

GRAPHICS FILE (Harshita)

The document contains 11 programs related to computer graphics and 2D transformations: 1. A line drawing program using the DDA algorithm. 2. A circle drawing program using the Bresenham's circle algorithm. 3. Another circle drawing program using the midpoint circle algorithm. 4. A line drawing program using the direct method. 5. A line drawing program using the Bresenham's line algorithm. 6. An ellipse drawing program using the midpoint ellipse algorithm. 7. A program to draw an arc of a circle. 8. A program demonstrating rotation of a point about another point. 9. A program to scale a triangle in 2D using

Uploaded by

Nitasha sharma
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)
137 views63 pages

GRAPHICS FILE (Harshita)

The document contains 11 programs related to computer graphics and 2D transformations: 1. A line drawing program using the DDA algorithm. 2. A circle drawing program using the Bresenham's circle algorithm. 3. Another circle drawing program using the midpoint circle algorithm. 4. A line drawing program using the direct method. 5. A line drawing program using the Bresenham's line algorithm. 6. An ellipse drawing program using the midpoint ellipse algorithm. 7. A program to draw an arc of a circle. 8. A program demonstrating rotation of a point about another point. 9. A program to scale a triangle in 2D using

Uploaded by

Nitasha sharma
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/ 63

PROGRAMS

1
OUTPUT:-

2
1. Program to draw a line using DDA algorithm.
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<math.h>
int round(float x)
{
return(int(x+0.5));
}
void main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:/TURBOC3/BGI ");
int xa,xb,ya,yb,steps,k;
cout<<"Enter the End point of a line";
cin>>xa>>xb>>ya>>yb;
int dx=xb-xa;
int dy=yb-ya;
float xinc,yinc,x,y;
x=xa;
y=ya;
if(abs(dx)>abs(dy))
steps=dx;
else
steps=dy;
xinc=dx/(float)steps;
yinc=dy/(float)steps;
putpixel(round(x),round(y),ORANGE);
for(k=0;k<steps;k++)
{
x=x+xinc;
y=y+yinc;
putpixel(round(x),round(y),ORANGE);
}
getch();
closegraph();
}

3
OUTPUT:-

4
2. Program to draw a circle using BRESENHAM's circle method.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
#include<math.h>
void circleplotpoints(int xc,int yc,int x,int y);
void main()
{
clrscr();
int gd=DETECT,gm;
int xc,yc,r,xy,x,y,p;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
cout<<"Enter center and radius of circle";
cin>>xc>>yc>>r;
x=0;
y=r;
p=3-(2*r);
while(x<y)
{
if(p<0)
{
p=p+4*x+6;
}
else
{
p=p+4*(x-y)+10;
y=y-1;
}
x++;
circleplotpoints(xc,yc,x,y);
}
getch();
closegraph();
}
void circleplotpoints(int xc,int yc,int x,int y)
{
putpixel(xc+x,yc+y,YELLOW);
putpixel(xc-x,yc+y,ORANGE);
putpixel(xc+x,yc-y,YELLOW);
putpixel(xc-x,yc-y,ORANGE);
putpixel(xc+y,yc+x,YELLOW);
putpixel(xc-y,yc+x,ORANGE);
putpixel(xc+y,yc-x,YELLOW);

5
6
putpixel(xc-y,yc-x,WHITE);
}

7
OUTPUT:-

8
3. Program to draw a circle using mid point circle algorithm.
#include<conio.h>
#include<iostream.h>
#include<graphics.h>
void circleplotpoints(int xcenter,int ycenter,int x,int y);
void main()
{
clrscr();
int gd=DETECT, gm, xcenter, ycenter, radius;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
cout<<"Enter radius ";
cin>>radius;
cout<<"Enter center points";
cin>>xcenter>>ycenter;
int x=0;
int y=radius;
int p=1-radius;
circleplotpoints(xcenter,ycenter,x,y);
while(x<y)
{
if(p<0)
{
p=p+2*x+3;
}
else
{
p=p+2*x-2*y+5;
y--;
}
x++;
circleplotpoints(xcenter, ycenter,x,y);
}
getch();
closegraph();
}
void circleplotpoints(int xcenter, int ycenter,int x,int y)
{
putpixel(xcenter+x,ycenter+y,WHITE);
putpixel(xcenter-x,ycenter+y,YELLOW);
putpixel(xcenter+x,ycenter-y,GREEN);
putpixel(xcenter-x,ycenter-y,WHITE);
putpixel(xcenter+y,ycenter+x,YELLOW);
putpixel(xcenter+y,ycenter+x,GREEN);

9
10
putpixel(xcenter-y,ycenter-x,YELLOW);
putpixel(xcenter-y,ycenter+x,GREEN);
}

11
OUTPUT:-

12
4. Program to draw a line using direct method.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
int round(float i)
{
return (int(i+0.5));
}
void main()
{
int gd=DETECT,gm,i,x1,y1,x2,y2,x,y;
float m,b;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
cout<<"Enter the starting and ending points";
cin>>x1>>y1>>x2>>y2;
m=((float)(y2-y1))/(x2-x1);
cout<<"The value is: ";
cout<<m;
b=(y1-(m*x1));
cout<<"The value of b is: "<<b;
if(abs(m)<=1)
{
for(i=x1;i<=x2;i++)
{
y=(m*i)+b;
putpixel(round(i),round(y),GREEN);
}
}
else
{
for(i=y1;i<=y2;i++)
{
x=(i-b)/m;
putpixel(round(i),round(x),PINK);
}
}
getch();
closegraph();
}

13
OUTPUT:-

14
5. Program to draw a line using bresenhams line drawing method.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void main()
{
clrscr();
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
int xa,ya,xb,yb,x,y,xend;
cout<<"Enter the end points of line";
cin>>xa>>ya>>xb>>yb;
int dx=abs(xa-xb),dy=abs(ya-yb);
int p=2*dy-dx;
int twody=2*dy;
int twodx=2*dx;
if(xa>xb)
{
x=xb;
y=yb;
xend=xa;
}
else
{
x=xa;
y=ya;
xend=xb;
}
putpixel(x,y,GREEN);
while(x<xend)
{
x++;
if(p<0)
p=p+twody;
else
{
y++;
p=p+twody-twodx;
}
putpixel(x,y,GREEN);
}
getch();

15
16
closegraph();
}

17
OUTPUT:-

18
6. Program to draw an ellipse using midpoint ellipse algorithm.
#include<iostream.h>
#include<conio.h>
#include<graphics.h>
void main()
{
int gd=DETECT, gm;
float c,d,rx,ry,a,b,p2=0,x,y,xc,yc,ry2,rx2,p=0;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
cout<<"ENter the center of ellipse ";
cin>>rx>>ry;
cout<<"Enter the center of ellipse ";
cin>>xc>>yc;
ry2=ry*ry;
rx2=rx*rx;
x=0;
y=ry;
p=(ry2-(rx2*ry)+((0.25)*rx2));
cout<<"initial parameter for region 1 is: "<<p;
void plotpoints(float xc,float yc,float x,float y);
while((2*ry2*x)<=(2*rx2*y))
{
x++;
if(p<0)
{
p=p+2*(ry2*x)+ry2;
}
else
{
p=p+2*(ry2*x)-(2*rx2*y)+ry2;
y--;
}
plotpoints(xc,yc,x,y);
}
a=(x+0.5)*(x+0.5);
b=(y-1)*(y-1);
p2=(ry2*a)+(rx2*b)-(rx2*ry2);
cout<<"initial parameter for region 2 is: ";
cout<<p2;
while(y>0)
{
y--;

19
20
if(p2<0)
p2=p2+(2*rx2*y)+rx2;
else
p2=p2+(2*ry2*x)-(2*rx2*y)+rx2;
plotpoints(xc,yc,x,y);
}
getch();
closegraph();
}
void plotpoints(float xc,float yc,float x,float y)
{
putpixel(xc+x,yc+y,PINK);
putpixel(xc-x,yc+y,PINK);
putpixel(xc+x,yc-y,PINK);
putpixel(xc-x,yc-y,PINK);
}

21
OUTPUT:-

22
7. Program to draw an arc of a circle.
#include<graphics.h>
#include<iostream>
#include<dos.h>
#include<conio.h>
int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\BGI");
arc(200,200,0,90,100);
getch();
closegraph();
}

23
OUTPUT:-

24
8.Program to Illustrate the rotation of a point about another point.
#include <iostream>
#include <complex>

using namespace std;

typedef complex<double> point;


#define x real()
#define y imag()

#define PI 3.1415926535897932384626

void displayPoint(point P)
{
cout << "(" << P.x << ", " << P.y << ")" << endl;
}

point rotate(point P, point Q, double theta)


{
return (P-Q) * polar(1.0, theta) + Q;
}

int main()
{
point P(4.0, 3.0);
point Q(2.0, 2.0);

double theta = PI/2;

point P_rotated = rotate(P, Q, theta);


cout << "The point P on rotating 90 degrees anti-clockwise about Q becomes:";
cout << "P_rotated"; displayPoint(P_rotated);

return 0;
}

25
OUTPUT:-

26
9. Program to scale a triangle in 2-D.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void main()
{
clrscr();
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,x3,y3,sx,sy,a[3][3],b[3][3],c[3][3],i,j,k;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
printf("Enter the coordinate of the endpoints of the triangle");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("Enter the values of sx,sy");
scanf("%d %d",&sx,&sy);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
b[0][0]=x1;
b[1][0]=y1;
b[2][0]=1;
b[0][1]=x2;
b[1][1]=y2;
b[2][1]=1;
b[0][2]=x3;
b[1][2]=y3;
b[2][2]=1;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=(i==j);
a[0][0]=sx;
a[1][1]=sy;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
x1=c[0][0];
y1=c[1][0];
x2=c[0][1];
y2=c[1][1];

27
28
x3=c[0][2];
y3=c[1][2];
setcolor(PINK);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}

29
OUTPUT:-

30
10. Program to translate a point in 2-D.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void translate(int *p,int *q, int r,int s)
{
int i,j,k,a[3][3],b[3][1],c[3][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=(i==j);
a[0][2]=r;
a[1][2]=s;
b[0][0]=*p;
b[1][0]=*q;
b[2][0]=1;
for(i=0;i<3;i++)
for(j=0;j<1;j++)
{
c[i][j]=0;
for(k=0;k<1;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
*p=c[0][0];
*q=c[1][0];
}
void main()
{
clrscr();
int gd=DETECT,gm;
int xy,x,y,x1,y1,tx,ty;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
printf("Enter the coordinate of the point");
scanf("%d %d",&x,&y);
printf("Enter the values of tx,ty");
scanf("%d %d", &tx,&ty);
putpixel(x,y,PINK);
translate(&x,&y,tx,ty);
putpixel(x,y,PINK);
getch();
closegraph();
}

31
OUTPUT:-

32
11. Program to translate a line in 2-D.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
void translate(int *p,int *q,int r,int s)
{
int i,j,k,a[3][3],b[3][1],c[3][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=(i==j);
a[0][2]=r;
a[1][2]=s;
b[0][0]=*p;
b[1][0]=*q;
b[2][0]=1;
for(i=0;i<3;i++)
for(j=0;j<1;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
*p=c[0][0];
*q=c[1][0];
}
void main()
{
clrscr();
int gd=DETECT,gm;
int x,y,x1,y1,tx,ty,x2,y2;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
printf("Enter the endpoints of the line");
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
printf("Enter the values of tx,ty");
scanf("%d %d",&tx,&ty);
line(x1,y1,x2,y2);
translate(&x1,&y1,tx,ty);
translate(&x2,&y2,tx,ty);
setcolor(PINK);
line(x1,y1,x2,y2);
getch();

33
34
closegraph();
}

35
OUTPUT:-

36
12. Program to translate a triangle in 2-D.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void translate (int *p,int *q,int r,int s)
{
int i,j,k,a[3][3],b[3][1],c[3][1];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
a[i][j]=(i==j);
a[0][2]=r;
a[1][2]=s;
a[0][0]=*p;
a[1][0]=*q;
b[2][0]=1;
for(i=0;i<3;i++)
for(j=0;j<1;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
*p=c[0][0];
*q=c[1][0];
}
void main()
{
clrscr();
int gd=DETECT,gm;
int x,y,x1,y1,x2,y2,x3,y3,tx,ty;
initgraph(&gd,&gm,"c:/TURBOC3/BGI");
printf("Enter the coordinate of the endpoints of the triangle");
scanf("%d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3);
printf("Enter the values of tx,ty");
scanf("%d%d",&tx,&ty);
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
translate(&x1,&y1,tx,ty);
translate(&x2,&y2,tx,ty);
translate(&x3,&y3,tx,ty);
setcolor(GREEN);

37
38
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
getch();
closegraph();
}

39
OUTPUT:-

40
13. Program to show Sutherland Hogman Algorithm.
#include<iostream>
using namespace std;
const int MAX_POINTS = 20;
int x_intersect(int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int num = (x1*y2 - y1*x2) * (x3-x4) -
(x1-x2) * (x3*y4 - y3*x4);
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
return num/den;
}
int y_intersect(int x1, int y1, int x2, int y2,
int x3, int y3, int x4, int y4)
{
int num = (x1*y2 - y1*x2) * (y3-y4) -
(y1-y2) * (x3*y4 - y3*x4);
int den = (x1-x2) * (y3-y4) - (y1-y2) * (x3-x4);
return num/den;
}
void clip(int poly_points[][2], int &poly_size,
int x1, int y1, int x2, int y2)
{
int new_points[MAX_POINTS][2], new_poly_size = 0;
for (int i = 0; i < poly_size; i++)
{
int k = (i+1) % poly_size;
int ix = poly_points[i][0], iy = poly_points[i][1];
int kx = poly_points[k][0], ky = poly_points[k][1];
int i_pos = (x2-x1) * (iy-y1) - (y2-y1) * (ix-x1);
int k_pos = (x2-x1) * (ky-y1) - (y2-y1) * (kx-x1);
if (i_pos < 0 && k_pos < 0)
{
new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}

else if (i_pos >= 0 && k_pos < 0)


{

new_points[new_poly_size][0] = x_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);

41
42
new_points[new_poly_size][1] = y_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_poly_size++;

new_points[new_poly_size][0] = kx;
new_points[new_poly_size][1] = ky;
new_poly_size++;
}

else if (i_pos < 0 && k_pos >= 0)


{
new_points[new_poly_size][0] = x_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_points[new_poly_size][1] = y_intersect(x1,
y1, x2, y2, ix, iy, kx, ky);
new_poly_size++;
}

else
{
}
}

poly_size = new_poly_size;
for (int i = 0; i < poly_size; i++)
{
poly_points[i][0] = new_points[i][0];
poly_points[i][1] = new_points[i][1];
}
}
void suthHodgClip(int poly_points[][2], int poly_size,
int clipper_points[][2], int clipper_size)
{
for (int i=0; i<clipper_size; i++)
{
int k = (i+1) % clipper_size;

clip(poly_points, poly_size, clipper_points[i][0],


clipper_points[i][1], clipper_points[k][0],
clipper_points[k][1]);
}
for (int i=0; i < poly_size; i++)
cout << '(' << poly_points[i][0] <<

43
44
", " << poly_points[i][1] << ") ";
}

int main()
{
int poly_size = 3;
int poly_points[20][2] = {{100,150}, {200,250},
{300,200}};

int clipper_size = 4;
int clipper_points[][2] = {{150,150}, {150,200},
{200,200}, {200,150} };

suthHodgClip(poly_points, poly_size, clipper_points,


clipper_size);

return 0;
}

45
OUTPUT:-

46
14. Program to illustrate ViewPort Transformation.
#include <iostream>
using namespace std;
void WindowtoViewport(int x_w, int y_w, int x_wmax,
int y_wmax, int x_wmin, int y_wmin,
int x_vmax, int y_vmax, int x_vmin,
int y_vmin)
{
int x_v, y_v;
float sx, sy;
sx = (float)(x_vmax - x_vmin) / (x_wmax - x_wmin);
sy = (float)(y_vmax - y_vmin) / (y_wmax - y_wmin);
x_v = x_vmin + (float)((x_w - x_wmin) * sx);
y_v = y_vmin + (float)((y_w - y_wmin) * sy);
cout<< "The point on viewport: ("<<x_v <<","<< y_v<<")" ;
}
int main()
{
int x_wmax = 80, y_wmax = 80, x_wmin = 20, y_wmin = 40;
int x_vmax = 60, y_vmax = 60, x_vmin = 30, y_vmin = 40;
int x_w = 30, y_w = 80;
WindowtoViewport(30, 80, 80, 80, 20, 40, 60, 60, 30, 40);
}

47
OUTPUT:-

48
15.Program to show Point clipping algorithm.
#include <bits/stdc++.h>
using namespace std;
void pointClip(int XY[][2], int n, int Xmin, int Ymin,int Xmax, int Ymax)
{
cout << "Point inside the viewing pane:" << endl;
for (int i = 0; i < n; i++)
{
if ((XY[i][0] >= Xmin) && (XY[i][0] <= Xmax))
{
if ((XY[i][1] >= Ymin) && (XY[i][1] <= Ymax))
cout <<"[" << XY[i][0] <<","<<XY[i][1]<<"] ";
}
}

cout<<"\n"<< endl;
cout << "Point outside the viewing pane:"<<endl;
for (int i = 0; i < n; i++)
{
if ((XY[i][0] < Xmin) || (XY[i][0] > Xmax))
cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
if ((XY[i][1] < Ymin) || (XY[i][1] > Ymax))
cout << "[" << XY[i][0] << "," << XY[i][1] << "] ";
}
}

int main()
{
int XY[6][2] = {{10, 10}, {-10, 10}, {400, 100}, {100, 400}, {400, 400}, {100,
40}};
int Xmin = 0;
int Xmax = 350;
int Ymin = 0;
int Ymax = 350;
pointClip(XY, 6, Xmin, Ymin, Xmax, Ymax);
return 0;
}

49
OUTPUT:-

50
16. Program to show Cohen Sutherland Line Clipping.
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<graphics.h>
#include<dos.h>
using namespace std;
typedef struct coordinate
{
int x,y;
char code[4];
}PT;
void drawwindow();
void drawline(PT p1,PT p2);
PT setcode(PT p);
int visibility(PT p1,PT p2);
PT resetendpt(PT p1,PT p2);
int main()
{
int gd=DETECT,v,gm;
PT p1,p2,p3,p4,ptemp;
cout<<"\nEnter x1 and y1\n";
cin>>p1.x>>p1.y;
cout<<"\nEnter x2 and y2\n";
cin>>p2.x>>p2.y;
initgraph(&gd,&gm,"c:\\turboc3\\bgi");
drawwindow();
delay(500);
drawline(p1,p2);
delay(500);
cleardevice();
delay(500);
p1=setcode(p1);
p2=setcode(p2);
v=visibility(p1,p2);
delay(500);
switch(v)
{
case 0: drawwindow();
delay(500);
drawline(p1,p2);
break;
case 1: drawwindow();

51
52
delay(500);
break;
case 2: p3=resetendpt(p1,p2);
p4=resetendpt(p2,p1);
drawwindow();
delay(500);
drawline(p3,p4);
break;
}
delay(5000);
closegraph();
}
void drawwindow()
{
line(150,100,450,100);
line(450,100,450,350);
line(450,350,150,350);
line(150,350,150,100);
}
void drawline(PT p1,PT p2)
{
line(p1.x,p1.y,p2.x,p2.y);
}
PT setcode(PT p) //for setting the 4 bit code
{
PT ptemp;
if(p.y<100)
ptemp.code[0]='1'; //Top
else
ptemp.code[0]='0';
if(p.y>350)
ptemp.code[1]='1'; //Bottom
else
ptemp.code[1]='0';
if(p.x>450)
ptemp.code[2]='1'; //Right
Else
ptemp.code[2]='0';
if(p.x<150)
ptemp.code[3]='1'; //Left
else
ptemp.code[3]='0';
ptemp.x=p.x;
ptemp.y=p.y;

53
54
return(ptemp);
}
int 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 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')

55
56
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];
return(temp);
}
else
return(p1);
}

57
OUTPUT:-

58
17.Program to show 3D transformations:- translation, rotation,

scaling.
#include <iostream>
#include <cmath>
using namespace std;
typedef struct {
float x;
float y;
float z;
}
Point;
Point points;
float temp = 0;
void showPoint(){
cout<<"("<<points.x<<","<<points.y<<","<<points.z<<")"<<endl;
}
void translate(float tx, float ty, float tz){
points.x += tx;
points.y += ty;
points.z += tz;
cout<<"After Translation, new point is :";
showPoint();
}
void rotatex(float angle){
angle = angle * M_PI / 180.0;
temp = points.y;
points.y = points.y * cos(angle) - points.z * sin(angle);
points.z = temp * sin(angle) + points.z * cos(angle);
cout<<"After rotation about x, new point is: ";
showPoint();
}
void rotatey(float angle){
angle = (angle * M_PI) / 180.0;
temp = points.z;
points.z = points.z * cos(angle) - points.x * sin(angle);
points.x = temp * sin(angle) + points.x * cos(angle);
cout<<"After rotation about y, new point is: ";
showPoint();

59
60
}
void rotatez(float angle){
angle = angle * M_PI / 180.0;
temp = points.x;
points.x = points.x * cos(angle) - points.y * sin(angle);
points.y = temp * sin(angle) + points.y *cos(angle);
cout<<"After rotation about z, new point is: ";
showPoint();
}
void scale(float sf, float xf, float yf, float zf){
points.x = points.x * sf + (1 - sf) * xf;
points.y = points.y * sf + (1 - sf) * yf;
points.z = points.z * sf + (1 - sf) * zf;
cout<<"After scaling, new point is: ";
showPoint();
}
int main()
{
float tx = 0, ty = 0, tz = 0;
float sf = 0, xf = 0, yf = 0, zf = 0;
int choose;
float angle;
cout<<"Enter the initial point you want to transform:";
cin>>points.x>>points.y>>points.z;
cout<<"Choose the following: "<<endl;
cout<<"1. Translate"<<endl;
cout<<"2. Rotate about X axis"<<endl;
cout<<"3. Rotate about Y axis"<<endl;
cout<<"4. Rotate about Z axis"<<endl;
cout<<"5. Scale"<<endl;
cin>>choose;
switch(choose){
case 1:
cout<<"Enter the value of tx, ty and tz: ";
cin>>tx>>ty>>tz;
translate(tx, ty, tz);
break;
case 2:
cout<<"Enter the angle: ";
cin>>angle;
rotatex(angle);
break;
case 3:
cout<<"Enter the angle: ";

61
62
cin>>angle;
rotatey(angle);
break;
case 4:
cout<<"Enter the angle: ";
cin>>angle;
rotatez(angle);
break;
case 5:
cout<<"Enter the value of sf, xf, yf and zf: ";
cin>>sf>>xf>>yf>>zf;
scale(sf, xf, yf, zf);
break;
default:
break;
}
return 0;
}

63

You might also like