-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeometry.cpp
More file actions
227 lines (186 loc) · 5.14 KB
/
Copy pathGeometry.cpp
File metadata and controls
227 lines (186 loc) · 5.14 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include "Geometry.h"
void Point::Scale(double scaler) {
double r = sqrt(pow(x, 2) + pow(y, 2));
double theta = atan(x * y);
r *= scaler;
x = r * cos(theta);
y = r * sin(theta);
}
inline bool operator < (const Point& l, const Point& r) {
return l.x < r.x && l.y < r.y;
}
inline bool operator > (const Point& l, const Point& r) {
return l.x > r.x && l.y > r.y;
}
inline bool operator <= (const Point& l, const Point& r) {
return l.x <= r.x && l.y <= r.y;
}
inline bool operator >= (const Point& l, const Point& r) {
return l.x >= r.x && l.y >= r.y;
}
std::ostream& operator << (std::ostream& os, const Point& p) {
os << "(" << p.x << ", " << p.y << ")";
return os;
}
Matrix::Matrix(Point pMin, Point pMax, std::vector<int> dimensions) {
xDist = pMax.x - pMin.y;
yDist = pMax.y - pMin.y;
startMin = pMin;
startMax = pMax;
d = { dimensions[0], dimensions[1], dimensions[0] * dimensions[1] };
data.resize(d[2]);
double xSpacing = xDist / (d[0] - 1);
double ySpacing = yDist / (d[1] - 1);
for (int i = 0; i < d[0]; i++) {
xList.push_back(i * xSpacing + pMin.x);
}
for (int i = 0; i < d[1]; i++) {
yList.push_back(i * ySpacing + pMin.y);
}
for (int i = 0; i < d[2]; i++) {
data.emplace_back(Point(xList[i % d[0]], yList[floor(i / d[0])]));
}
}
inline bool operator < (const Segment& l, const Segment& r) {
return l.p1 < r.p1&& l.p2 < r.p2;
}
std::vector<double> NHSelect(double targetX, double x) {
int n;
double h, nCap = pow(10, 7);
char nhSelect;
std::vector<double> out;
std::cout << "N or H: ";
std::cin >> nhSelect;
if (tolower(nhSelect) == 'n') {
std::cout << "N: ";
std::cin >> n;
if (n > nCap) {
std::cout << "N > " << nCap << " -- N reset to " << nCap << std::endl;
n = nCap;
}
else if (n <= 0) {
std::cout << "N <= 0, reset to " << nCap << std::endl;
n = nCap;
}
h = (targetX - x) / static_cast<double>(n);
std::cout << "H = " << h << std::endl;
}
else if (tolower(nhSelect) == 'h') {
std::cout << "H: ";
std::cin >> h;
n = (targetX - x) / h;
if (n > nCap) {
std::cout << "N > " << nCap << " -- N reset to " << nCap << std::endl;
n = nCap;
h = (targetX - x) / static_cast<double>(n);
std::cout << "H = " << h << std::endl;
}
else if (n <= 0) {
std::cout << "N <= 0, reset to " << nCap << std::endl;
n = nCap;
h = (targetX - x) / static_cast<double>(n);
std::cout << "H = " << h << std::endl;
}
}
else if (nhSelect == 'm') {
n = pow(10, 8);
h = (targetX - x) / static_cast<double>(n);
std::cout << std::endl << "Max N selected " << std::endl << "N = " << n << std::endl << "H = " << h << std::endl;
}
else {
std::cout << "Invalid char" << std::endl;
out = NHSelect(targetX, x);
}
std::cout << std::endl;
out.push_back(n);
out.push_back(h);
return out;
}
std::vector<Point> Approximate(std::vector<double> in, bool isTesting) {
int n;
double h, tRadius;
Point startPoint;
if (isTesting) {
tRadius = 1 / 5000;
n = pow(10, 7);
h = 1 / n;
}
else {
startPoint = Point(in[0], in[1]);
std::vector<double> k;
double targetX = in[2], xDist = abs(targetX - startPoint.x);
tRadius = xDist / 5000;
std::vector<double> nh = NHSelect(targetX, startPoint.x);
n = static_cast<int> (nh[0]);
h = nh[1];
}
Point iterativePoint = startPoint;
std::vector<Point> pointList = { startPoint };
int j = 0;
for (int i = 0; i < n; i++) {
iterativePoint += Point(h, slope(iterativePoint.x, iterativePoint.y) * h);
if (!pointList[j].inRadius(iterativePoint, tRadius)) {
pointList.push_back(iterativePoint);
j++;
}
}
if (pointList[pointList.size() - 1] != iterativePoint) {
pointList.push_back(iterativePoint);
}
return pointList;
}
std::vector<double> StartSelect() {
double startX, startY, targetX;
// questions
std::cout << "Start X: ";
std::cin >> startX;
std::cout << "Start Y: ";
std::cin >> startY;
std::cout << "Target X: ";
std::cin >> targetX;
// Out construction
std::vector<double> out = { startX, startY, targetX };
// Out
return out;
}
void SlopeField::GenSegments(std::string string) { // Also gens bounds
double r = std::min(xDist / (2 * d[0] - 1), yDist / (2 * d[1] - 1));
Point pLow = data[0], pHigh = data[0];
std::vector<double> k;
Slope slope(string);
for (int i = 0; i < d[2]; i++) {
/* - - - - - - - - *
* k explanation: *
* * <---> * <---> * (diagram) *
* | k | k | ~~ k = xDist *
* * = p1 | * = p2 *
* * = data[i] *
* - - - - - - - - */
k.push_back(r / sqrt(1 + pow(slope.GetSlope(data[i].x, data[i].y), 2)));
// Defining points in segment
Point pL(data[i].x - k[i], data[i].y - slope.GetSlope(data[i].x, data[i].y) * k[i]);
Point pR(data[i].x + k[i], data[i].y + slope.GetSlope(data[i].x, data[i].y) * k[i]);
// Evaluating bounds
for (Point pCurr : {pL, pR}) {
// pLow
if (pLow.x > pCurr.x) {
pLow.x = pCurr.x;
}
if (pLow.y > pCurr.y) {
pLow.y = pCurr.y;
}
// pHigh
if (pHigh.x < pCurr.x) {
pHigh.x = pCurr.x;
}
if (pHigh.y < pCurr.y) {
pHigh.y = pCurr.y;
}
}
// Constructing Segment list
segments.push_back(Segment(pL, pR));
}
// Constructing bounds list
bounds.push_back(pLow);
bounds.push_back(pHigh);
}