-
Notifications
You must be signed in to change notification settings - Fork 1
/
Point.js
104 lines (87 loc) · 2.35 KB
/
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
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
const cos = Math.cos;
const sin = Math.sin;
class Point {
constructor(x, y, width, height, vertices = null,
toFill = true, fillStyle = null,
strokeStyle = null, lineWidth = 1) {
this.prev = null; // for LinkedList
this.x = x;
this.y = y;
this.w = width;
this.h = height;
this.toFill = toFill;
this.lineWidth = lineWidth;
this.vertices = vertices ? vertices : this.square();
this.fillStyle = fillStyle ? fillStyle : this.getRandomColor();
this.strokeStyle = strokeStyle ? strokeStyle : this.fillStyle;
}
setPrev(prev) {
this.prev = prev;
return this;
}
square() {
return [
[this.x, this.y], // top left corner
[this.x + this.w, this.y], // top right corner
[this.x + this.w, this.y + this.h], // bottom right corner
[this.x, this.y + this.h], // bottom left corner
];
}
translate(a = 0, b = 0) {
for (let vertices of this.getVertices()) {
vertices[0] += a;
vertices[1] += b;
}
}
getRandomColor() {
let ret = (0x1000000 + Math.random() * 0xffffff);
return '#' + ret.toString(16).substr(1, 6);
}
getVertices() {
return this.vertices;
}
fill() {
return this.toFill;
}
getFillStyle() {
return this.fillStyle;
}
getStrokeStyle() {
return this.strokeStyle;
}
getLineWidth() {
return this.lineWidth;
}
setFillStyle(fillStyle) {
this.fillStyle = fillStyle;
}
setStrokeStyle(strokeStyle) {
this.strokeStyle = strokeStyle;
}
rotate(rad, origin = null) {
if (rad === 0)
return;
let X, Y;
if (origin === null) {
X = this.x + this.w / 2;
Y = this.y + this.h / 2;
} else {
X = origin[0];
Y = origin[1];
}
let T = [[cos(rad), sin(rad), X - (X * cos(rad) + Y * sin(rad))],
[-sin(rad), cos(rad), Y - (Y * cos(rad) - X * sin(rad))],
[0, 0, 1]];
for (let i = 0; i < this.vertices.length; i++) {
let x = this.vertices[i][0];
let y = this.vertices[i][1];
let z = 1;
this.vertices[i] = [(x * T[0][0]) + (y * T[0][1]) + (z * T[0][2]),
(x * T[1][0]) + (y * T[1][1]) + (z * T[1][2])];
}
// {{cos(r), sin(r), x - (x cos(r) + y sin(r))},
// {-(sin(r)), cos(r), y - (y cos(r) - x sin(r))}, {0, 0, 1}}
// *{{x,x+w,x+w,x},{y,y,y+h,y+h},{1,1,1,1}}
}
}
export default Point;