-
Notifications
You must be signed in to change notification settings - Fork 0
/
point.js
41 lines (34 loc) · 840 Bytes
/
point.js
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
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
add(point) {
return new Point(this.x + point.x, this.y + point.y);
}
subtract(point) {
return new Point(this.x - point.x, this.y - point.y);
}
scalarDivision(number) {
return new Point(this.x/number, this.y/number);
}
moveTo(ctx) {
ctx.moveTo(this.x, this.y)
}
lineTo(ctx) {
ctx.lineTo(this.x, this.y)
}
rotate(degree, center) {
let radians = this.degreeToRadians(degree);
let centered = this.subtract(center);
let result = new Point(
centered.x * Math.cos(radians) - centered.y * Math.sin(radians),
centered.x * Math.sin(radians) + centered.y * Math.cos(radians)
);
result = result.add(center);
return result;
}
degreeToRadians(degree) {
return Math.PI/180 * degree;
}
}