-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWHUtil.js
More file actions
130 lines (112 loc) · 3.24 KB
/
WHUtil.js
File metadata and controls
130 lines (112 loc) · 3.24 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
import { Polygon } from "./Polygon.js";
export class WHUtil {
static DTOR = 0.017453292519943295;
static RTOD = 57.29577951308232;
static target = [
[-60, -60, -40, -60],
[-60, -60, -60, -40],
[-60, 60, -40, 60],
[-60, 60, -60, 40],
[60, -60, 40, -60],
[60, -60, 60, -40],
[60, 60, 40, 60],
[60, 60, 60, 40],
];
static randInt(number = Number.MAX_SAFE_INTEGER) {
return Math.floor(Math.random() * number);
}
static setColor(context, color) {
context.fillStyle = color;
context.strokeStyle = color;
}
static createPolygon(points) {
let x = [];
let y = [];
for (let i = 0; i < points.length; i++) {
x.push(points[i][0]);
y.push(points[i][1]);
}
return new Polygon(x, y, points.length);
}
static symPolygon(numPoints, radius, rotation) {
let polygon = new Polygon();
for (let i = 0; i < numPoints; i++) {
let n4 = rotation + (360 / numPoints) * i * WHUtil.DTOR;
polygon.addPoint(radius * Math.cos(n4), radius * Math.sin(n4));
}
return polygon;
}
static drawTarget(context, xOffset, yOffset) {
for (let i = 0; i < WHUtil.target.length; i++) {
context.moveTo(
xOffset + WHUtil.target[i][0],
yOffset + WHUtil.target[i][1],
);
context.lineTo(
xOffset + WHUtil.target[i][2],
yOffset + WHUtil.target[i][3],
);
}
}
static scaleVector(n, n2) {
return n / Math.hypot(n, n2);
}
static findAngle(x1, y1, x2, y2) {
return Math.atan2(y1 - y2, x1 - x2) * WHUtil.RTOD;
}
static findAngleRad(x1, y1, x2, y2) {
return Math.atan2(y1 - y2, x1 - x2);
}
static drawBoundCircle(context, x, y, radius, color, color2) {
context.beginPath();
context.fillStyle = color2;
context.arc(x, y, radius, 0, 2 * Math.PI);
context.fill();
context.strokeStyle = color;
context.arc(x, y, radius, 0, 2 * Math.PI);
context.stroke();
}
static drawCenteredCircle(context, x, y, radius) {
context.beginPath();
context.lineWidth = 1;
context.arc(x, y, radius, 0, 2 * Math.PI);
context.stroke();
}
static drawScaledPoly(context, polygon, scale) {
if (scale == 1) {
polygon.drawPolygon(context);
return;
}
context.beginPath();
let i = polygon.xpoints[0] * scale;
let j = polygon.ypoints[0] * scale;
for (let b = 1; b < polygon.npoints; b++) {
let k = polygon.xpoints[b] * scale;
let m = polygon.ypoints[b] * scale;
context.moveTo(i, j);
context.lineTo(k, m);
i = k;
j = m;
}
context.moveTo(i, j);
context.lineTo(polygon.xpoints[0] * scale, polygon.ypoints[0] * scale);
context.stroke();
}
static distanceFrom(x1, y1, x2, y2) {
return Math.hypot(x1 - x2, y1 - y2);
}
// get an RGB value from a named color
// https://css-tricks.com/converting-color-spaces-in-javascript/
static nameToRGB(name) {
// Create fake div
let fakeDiv = document.createElement("div");
fakeDiv.style.color = name;
document.body.appendChild(fakeDiv);
// Get color of div
let cs = window.getComputedStyle(fakeDiv),
pv = cs.getPropertyValue("color");
// Remove div after obtaining desired color value
document.body.removeChild(fakeDiv);
return pv;
}
}