This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2dtransform.cpp
123 lines (114 loc) · 3.01 KB
/
2dtransform.cpp
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
#include "2dtransform.h"
#include "io.h"
#include <iostream>
void trf_menu(std::vector<Point2D> &poly, char d) {
float sx, sy, ang;
int x1, y1, x2, y2;
Point2D t;
switch(d) {
case '1' : trans(poly, mousePoint()); break;
case '2' :
cleardevice();
outtext((char*)"Enter scaling factor");
sx = kbf(); sy = kbf();
scale(poly, sx, sy);
break;
case '3' :
cleardevice();
mypoly(poly);
outtext((char*)"R");
mouseInput(x1, y1, x2, y2);
ang = atanf((y1)/x1) - atanf((y2)/x2);
rot(poly, ang);
break;
case '4' :
cleardevice();
outtext((char*)"Click on point relative to which to scale");
mypoly(poly);
t = mousePoint();
t = Point2D(-t.x, -t.y);
trans(poly, t);
cleardevice();
outtext((char*)"Enter scaling factor");
sx = kbf(); sy = kbf();
scale(poly, sx, sy);
t = Point2D(-t.x, -t.y);
trans(poly, t);
break;
default : break;
}
}
void trf(std::vector<Point2D> &p, std::vector<std::vector <float> > mat) {
std::vector<int> temp, em;
for(size_t k = 0; k < p.size(); k++) {
temp = em;
temp.push_back(p[k].x); temp.push_back(p[k].y); temp.push_back(1);
float t = 0;
for (int i = 0; i < 3; i ++)
{
t = 0;
for (int j = 0; j < 3; j++)
{
t += mat[i][j] * temp[j];
}
if (i == 0)
p[k].x = (int)(t+0.5);
else if (i == 1)
p[k].y = (int)(t+0.5);
}
}
}
void trans(std::vector<Point2D> &p, Point2D t) {
std::vector<std::vector <float> > mat(3);
for(int i = 0; i < 3; i++)
{
mat[i].resize(3);
mat[i][i] = 1;
}
mat[0][2] = t.x; mat[1][2] = t.y;
trf(p, mat);
}
void scale(std::vector<Point2D> &p, float sx, float sy) {
std::vector<std::vector <float> > mat(3);
for(int i = 0; i < 3; i++)
{
mat[i].resize(3);
mat[i][i] = 1;
}
mat[0][0] = sx; mat[1][1] = sy;
std::cout<<"works";
trf(p, mat);
}
void rot(std::vector<Point2D> &p, float ang) {
std::vector<std::vector <float> > mat(3);
float c, s;
c = cos(ang);
s = sin(ang);
for(int i = 0; i < 3; i++) {
mat[i].resize(3);
mat[i][i] = c;
}
mat[2][2] = 1;
mat[0][1] = -s; mat[1][0] = s;
trf(p, mat);
}
/*void rot(std::vector<int> &p, int ang, int x, int y)
{
std::vector<std::vector <float> > mat(3);
float c, s, rx, ry, pi = 3.14159;
c = cosf((ang * pi) / 180);
s = sinf((ang * pi) / 180);
rx = ((1-c) * x) + (s * y);
ry = ((1-c) * y) + (s * x);
for(int i = 0; i < 3; i++)
{
mat[i].resize(3);
mat[i][i] = c;
}
mat[2][2] = 1;
mat[0][1] = -s; mat[1][0] = s;
mat[0][2] = rx;
mat[1][2] = ry;
trf(p, mat);
}
*/