forked from zahinekbal/codeWith-hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransformation_in_2D_triangle.cpp
More file actions
131 lines (116 loc) · 2.8 KB
/
transformation_in_2D_triangle.cpp
File metadata and controls
131 lines (116 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include<iostream>
#include<graphics.h>
#include<math.h>
using namespace std;
main(){
char choice;
int gd= DETECT, gm;
int x1,y1,x2,y2,x3,y3;
initgraph(&gd, &gm, "");
// Entering coordinates of the triangle
cout <<"Enter first coordinates of triangle:"<< endl;
cin>>x1>>y1;
cout<<"Enter second coordinates of triangle:"<< endl;
cin>>x2>>y2;
cout<<"Enter third coordinates of triangle:"<< endl;
cin>>x3>>y3;
// Printing the lines
line(x1,y1,x2,y2);
line(x2,y2,x3,y3);
line(x3,y3,x1,y1);
do
{
cout<<endl;
cout<<"Transformations"<<endl;
cout<<"1. Translation"<<endl;
cout<<"2. Rotation"<<endl;
cout<<"3. Shearing"<<endl;
cout<<"4. Scaling"<<endl;
cout<<"5. Exit"<<endl;
cout<<"Enter your choice: "<<endl;
cin>>choice;
switch(choice)
{
// Translation in 2D Triangle
case '1':
int tx, ty;
cout<<"Enter tanslation vector: "<<endl;
cin>>tx>>ty;
setcolor(RED);
line(x1+tx, y1+ty, x2+tx, y2+ty);
line(x2+tx, y2+ty, x3+tx, y3+ty);
line(x3+tx, y3+ty, x1+tx, y1+ty);
getch();
closegraph();
break;
// Rotaion in 2D Triangle
case '2':
int x,y,a1,b1,a2,b2,a3,b3;
float Angle;
cout<<"Enter the angle for rotation:"<<endl;
cin >> Angle;
Angle=(Angle*3.14)/180;
a1=x2+(x1-x2)*cos(Angle)-(y1-y2)*sin(Angle);
b1=y2+(x1-x2)*sin(Angle)+(y1-y2)*cos(Angle);
a2=x2+(x2-x2)*cos(Angle)-(y2-y2)*sin(Angle);
b2=y2+(x2-x2)*sin(Angle)+(y2-y2)*cos(Angle);
a3=x2+(x3-x2)*cos(Angle)-(y3-y2)*sin(Angle);
b3=y2+(x3-x2)*sin(Angle)+(y3-y2)*cos(Angle);
setcolor(YELLOW);
line(a1, b1, a2, b2);
line(a2, b2, a3, b3);
line(a3, b3, a1, b1);
getch();
closegraph();
break;
// Scaling in 2D Triangle
case '3':
int sh;
cout<<"Enter scaling factor: "<<endl;
cin>>sh;
x1 = x1 + y1*sh;
x2 = x2 + y2*sh;
x3 = x3 + y3*sh;
setcolor(GREEN);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
break;
// Shearing in 2D Triangle
case '4':
int sx, sy;
cout<<"Enter scaling factor: "<<endl;
cin>>sx>>sy;
x1 = x1 * sx;
x2 = x2 * sx;
x3 = x3 * sx;
y1 = y1 * sy;
y2 = y2 * sy;
y3 = y3 * sy;
setcolor(RED);
line(x1, y1, x2, y2);
line(x2, y2, x3, y3);
line(x3, y3, x1, y1);
getch();
closegraph();
break;
case '5':
exit(0);
default: cout<<"Wrong choice...!!"<< endl;
break;
}
}
while(choice!=5 && choice!= getchar());
}
/*
EXAMPLE INPUT
Enter first coordinates of triangle: 50 150
Enter second coordinates of triangle: 150 200
Enter third coordinates of triangle: 250 300
Translation Vector: 100 100
Angle of rotaion: 30
Shearing Factor: 2
Scaling Factor: 2 2
*/