-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_trig_rotation.cpp
238 lines (201 loc) · 6.24 KB
/
no_trig_rotation.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
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
228
229
230
231
232
233
234
235
236
237
238
#include "common/sketchbook.hpp"
// you know what projection is right? you better do cause i sure don't
constexpr
float2 project(float2 x, float2 surface)
{
return surface * surface(x)
/ surface.magnitude(); // can skip this, then angles will also scale (like complex numbers)
}
// rejection is the other part
// projection + rejection = original vector
constexpr
float2 reject(float2 x, float2 surface)
{
return x - project(x,surface);
}
// reflection simply negates the rejection
constexpr
float2 reflect(float2 x, float2 surface)
{
return project(x,surface) - reject(x,surface);
}
// rotation is just two reflections
constexpr
float2 rotate(float2 x, float2 half_angle)
{
return reflect( reflect(x, float2::i()), half_angle );
}
constexpr
float2 normalize(float2 x)
{
// return x / x.quadrance(); // fun!
return x / support::root2(x.quadrance());
}
// the tricky part is specifying angles as vectors, so here are some approximations that can help
template <size_t Exponent = 3, typename Value = float>
class protractor
{
public:
using vector = geom::vector<Value,2>;
// one approach is to approximate the circle with a regular polygon
// and then linearly interpolate on that polygon to get angles
//
// we actually just need to approximate a half circle, since
// our rotation doubles angles
// we'll generate the half circle polygon
// by starting with a flat(tau/2) angle and repeatedly bisecting it
// the number of section will always be a power of two
// since bisection is essentially multiplying them by two
// so our number of iteration/precision parameter is the exponent
using array = std::array<vector, (size_t(1)<<Exponent) + 1>;
static constexpr array circle = []()
{
using support::halfway;
array points{};
// end points of half polygon/circle
points.front() = vector::i();
points.back() = -vector::i();
auto bisect = [](auto begin, auto end,
auto& self) // ugly recursive lambda trick -_-
{
// can't squeeze a new value between two adjacent elements,
// so the array must be full already
if(end - begin <= 2)
return;
// otherwise
// we're going to put the new bisector in the middle of provided range
auto middle = halfway(begin, end);
// the bisector itself lies halfway between the two points,
// since both are on circle.
// need to normalize it to stay on the circle for subsequent bisections
*(middle) = normalize(halfway(*begin, *(end - 1)));
// do the same for the two bisected parts
self(begin, middle + 1,
self);
self(middle, end,
self);
};
if(Exponent > 0)
{
// have to handle the case of first bisection separately,
// since can't normalize vector 0 :/
auto middle = halfway(points.begin(), points.end());
*middle = vector::j();
bisect(points.begin(), middle + 1,
bisect);
bisect(middle, points.end(),
bisect);
}
return points;
}();
// fraction of full circle
static constexpr vector tau(Value factor)
{
assert(Value{0} <= factor && factor < Value{1});
Value index = factor * (protractor::circle.size() - 1);
int whole = index;
Value fraction = index - whole;
return way(circle[whole], circle[whole+1], fraction);
// linear interpolation more or less works thanks to normalizing/dividing projection
// otherwise this method is no good
}
// full circle
static constexpr vector tau()
{
return -vector::i();
}
// small angle in radians
static constexpr vector small_radian(Value value)
{
return {support::root2(Value{1} - value*value), value};
// cause sine of small angle is pretty much equal to that angle
}
// any angle in radians, not constexpr -_-
static vector radian(Value angle)
{
// using trigonometric function, very optional
return {std::cos(angle), std::sin(angle)};
}
// decides for you weather to use tau() or small_radian()
// is not necessarily smart
static vector angle(Value tau_factor)
{
const static auto tau = std::acos(-1) * 2;
if(tau_factor < (Value{0.5f}/(circle.size()-1))/10 )
return small_radian(tau_factor * tau);
else
return protractor::tau(tau_factor);
}
};
float2 angle = float2::one(1.f);
float regular_angle = 0.f;
void start(Program& program)
{
program.draw_loop = [&](auto frame, auto delta)
{
frame.begin_sketch()
.rectangle(rect{frame.size})
.fill(rgb::white(0))
;
constexpr auto half = float2::one(0.5);
// the outline of expected path (circle)
frame.begin_sketch()
.ellipse(anchored_rect2f{
float2::one(300), frame.size/2, half
})
.outline(0xff00ff_rgb)
;
// draw rotated point (should follow the circle)
frame.begin_sketch()
.ellipse(anchored_rect2f{
float2::one(10),
frame.size/2 + rotate(float2::j(150), angle),
float2::one(0.5f)
})
.fill(0xff00ff_rgb);
;
// draw the angle itself and a unit circle
frame.begin_sketch()
.line(frame.size/2, frame.size/2 + angle * 50)
.ellipse(anchored_rect2f{
float2::one(100), frame.size/2, half
})
.outline(0xffff00_rgb)
;
// draw the protractor polygon
// smaller radius than unit circle just to not overlap
{ auto sketch = frame.begin_sketch();
for(size_t i = 1; i < protractor<>::circle.size(); ++i)
{
sketch.line(frame.size/2 + protractor<>::circle[i-1]*45, frame.size/2 + protractor<>::circle[i] * 45);
}
sketch.outline(0x00ffff_rgb);
}
// ijkl just free-form move the endpoint of the angle
if(pressed(scancode::j))
angle.x() -= 1.f * delta.count();
if(pressed(scancode::l))
angle.x() += 1.f * delta.count();
if(pressed(scancode::k))
angle.y() += 1.f * delta.count();
if(pressed(scancode::i))
angle.y() -= 1.f * delta.count();
// left and right set the angle using the elaborate polygon-lerp scheme
auto move_regular_angle = [&](float direction)
{
regular_angle += direction * delta.count() * 0.1f;
regular_angle = support::wrap(regular_angle,1.f);
angle = protractor<>::tau(regular_angle);
};
if(pressed(scancode::right))
move_regular_angle(1);
if(pressed(scancode::left))
move_regular_angle(-1);
// a and d use small radian approximation
// we rotate the angle itself by another small angle
if(pressed(scancode::a))
angle = rotate(angle, protractor<>::small_radian(delta.count() * 0.1f));
if(pressed(scancode::d))
angle = rotate(angle, protractor<>::small_radian(delta.count() * -0.1f));
};
}