Repository navigation
Expand file tree
/
Copy pathangle.ts
More file actions
189 lines (156 loc) · 4.3 KB
/
Copy pathangle.ts
File metadata and controls
189 lines (156 loc) · 4.3 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
/*
Copyright (c) 2022 Alethea Katherine Flowers.
Published under the standard MIT License.
Full text available at: https://opensource.org/licenses/MIT
*/
import { Vec2 } from "./vec2";
export type AngleLike = Angle | number;
/**
* An angle for rotation and orientation
*/
export class Angle {
#theta_rad: number;
#theta_deg: number;
/**
* Convert radians to degrees
*/
static rad_to_deg(radians: number) {
return (radians / Math.PI) * 180;
}
/**
* Convert degrees to radians
*/
static deg_to_rad(degrees: number) {
return (degrees / 180) * Math.PI;
}
/** Round degrees to two decimal places
*
* A lot of math involving angles is done with degrees to two decimal places
* instead of radians to match KiCAD's behavior and to avoid floating point
* nonsense.
*/
static round(degrees: number): number {
return Math.round((degrees + Number.EPSILON) * 100) / 100;
}
/**
* Create an Angle
*/
constructor(radians: AngleLike) {
if (radians instanceof Angle) {
return radians;
}
this.radians = radians;
}
copy() {
return new Angle(this.radians);
}
get radians() {
return this.#theta_rad;
}
set radians(v) {
this.#theta_rad = v;
this.#theta_deg = Angle.round(Angle.rad_to_deg(v));
}
get degrees() {
return this.#theta_deg;
}
set degrees(v) {
this.#theta_deg = v;
this.#theta_rad = Angle.deg_to_rad(v);
}
static from_degrees(v: number) {
return new Angle(Angle.deg_to_rad(v));
}
/**
* Returns a new Angle representing the sum of this angle and the given angle.
*/
add(other: AngleLike) {
const sum = this.radians + new Angle(other).radians;
return new Angle(sum);
}
/**
* Returns a new Angle representing the difference between this angle and the given angle.
*/
sub(other: AngleLike) {
const diff = this.radians - new Angle(other).radians;
return new Angle(diff);
}
/**
* @returns a new Angle constrained to 0 to 360 degrees.
*/
normalize() {
let deg = Angle.round(this.degrees);
while (deg < 0) {
deg += 360;
}
while (deg >= 360) {
deg -= 360;
}
return Angle.from_degrees(deg);
}
/**
* @returns a new Angle constrained to -180 to 180 degrees.
*/
normalize180() {
let deg = Angle.round(this.degrees);
while (deg <= -180) {
deg += 360;
}
while (deg > 180) {
deg -= 360;
}
return Angle.from_degrees(deg);
}
/**
* @returns a new Angle constrained to -360 to +360 degrees.
*/
normalize720() {
let deg = Angle.round(this.degrees);
while (deg < -360) {
deg += 360;
}
while (deg >= 360) {
deg -= 360;
}
return Angle.from_degrees(deg);
}
/**
* @returns a new Angle that's reflected in the other direction, for
* example, 90 degrees ends up being -90 or 270 degrees (when normalized).
*/
negative(): Angle {
return new Angle(-this.radians);
}
get is_vertical() {
return this.degrees == 90 || this.degrees == 270;
}
get is_horizontal() {
return this.degrees == 0 || this.degrees == 180;
}
rotate_point(point: Vec2, origin: Vec2 = new Vec2(0, 0)): Vec2 {
let x = point.x - origin.x;
let y = point.y - origin.y;
const angle = this.normalize();
// shortcuts for 0, 90, 180, and 270
if (angle.degrees == 0) {
// do nothing
} else if (angle.degrees == 90) {
[x, y] = [y, -x];
} else if (angle.degrees == 180) {
[x, y] = [-x, -y];
} else if (angle.degrees == 270) {
[x, y] = [-y, x];
}
// no shortcut, do the actual math.
else {
const sina = Math.sin(angle.radians);
const cosa = Math.cos(angle.radians);
const [x0, y0] = [x, y];
x = y0 * sina + x0 * cosa;
y = y0 * cosa - x0 * sina;
}
x += origin.x;
y += origin.y;
return new Vec2(x, y);
}
}