-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
227 lines (188 loc) · 5.24 KB
/
util.js
File metadata and controls
227 lines (188 loc) · 5.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
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
'use strict';
/* Point between a and b */
function midpoint(a, b) {
return new Vec2( (a.x+b.x)/2.0, (a.y+b.y)/2.0 );
}
/* Intersection with ray and segment */
function inter(ray,segment){
ray.a = ray.origin;
ray.b = ray.dir;
segment.a = segment.origin;
segment.b = segment.dir;
var r_px = ray.a.x;
var r_py = ray.a.y;
var r_dx = ray.b.x-ray.a.x;
var r_dy = ray.b.y-ray.a.y;
var s_px = segment.a.x;
var s_py = segment.a.y;
var s_dx = segment.b.x-segment.a.x;
var s_dy = segment.b.y-segment.a.y;
var r_mag = Math.sqrt(r_dx*r_dx+r_dy*r_dy);
var s_mag = Math.sqrt(s_dx*s_dx+s_dy*s_dy);
if(r_dx/r_mag==s_dx/s_mag && r_dy/r_mag==s_dy/s_mag)
return null;
var T2 = (r_dx*(s_py-r_py) + r_dy*(r_px-s_px))/(s_dx*r_dy - s_dy*r_dx);
var T1 = (s_px+s_dx*T2-r_px)/r_dx;
if( (T1<0) || (T2<0 || T2>1) )
return null;
return {
p : new Vec2(r_px+r_dx*T1, r_py+r_dy*T1),
param: T1,
m : segment
};
}
/* FPS info */
class FPS {
constructor() {
this.last = 0;
this.frames_sum = 0;
this.frame_no = 0;
this.frame = 0;
this.fps_avg = 0;
this.frame_count = 30.0;
this.delta = 0;
}
begin() {
this.frame++;
this.delta = (Date.now() - this.last) / 1000;
this.last = Date.now();
let fps = (1 / this.delta);
if(this.frame_no != this.frame_count) {
this.frames_sum += fps;
this.frame_no++;
}
else {
this.frames_sum /= this.frame_count;
this.fps_avg = this.frames_sum;
this.frames_sum = this.frame_no = 0;
}
}
get_time() {
return this.frame / 60.0;
}
draw() {
ctx.font = '12px Arial';
ctx.fillStyle = 'red';
ctx.fillText("FPS:"+(this.fps_avg).toFixed(2), 0, 12);
}
};
class Grid {
constructor(dist_x, dist_y, fsize, colx, coly) {
this.fsize = fsize;
this.dist_x = dist_x;
this.dist_y = dist_y;
this.colx = colx;
this.coly = coly;
this.bar_x = Math.trunc(cv.width / this.dist_x);
this.bar_y = Math.trunc(cv.height / this.dist_y);
}
draw() {
ctx.save();
/* Center */
ctx.translate(cv.width * 0.5, cv.height * 0.5);
ctx.strokeStyle = 'gray';
/* Axis */
ctx.beginPath();
ctx.moveTo(-cv.width, 0);
ctx.lineTo(cv.width, 0);
ctx.moveTo(0, -cv.height);
ctx.lineTo(0, cv.height);
ctx.stroke();
/* Numbers */
ctx.font = this.fsize+'px Arial';
ctx.fillStyle = this.colx;
ctx.strokeStyle = 'gray';
for(var x=-(this.bar_x/2.0)+1;x<(this.bar_x/2.0);++x) {
ctx.beginPath();
ctx.moveTo(x*this.dist_x, 5);
ctx.lineTo(x*this.dist_x, -5);
ctx.stroke();
var m = ctx.measureText(x*this.dist_x).width / 2.0;
if(x!=0)
ctx.fillText(x*this.dist_x, x*this.dist_x-m, -10);
ctx.closePath();
}
ctx.fillStyle = this.coly;
for(var y=-(this.bar_y/2.0)+1;y<(this.bar_y/2.0);++y) {
ctx.beginPath();
ctx.moveTo(-5, y*this.dist_y);
ctx.lineTo(5, y*this.dist_y);
ctx.stroke();
var m = this.fsize / 3.0;
if(y!=0)
ctx.fillText(y*-this.dist_y, 10, y*this.dist_y+m);
ctx.closePath();
}
ctx.restore();
}
};
class Vec2 {
constructor(x, y) {
this.x = x, this.y = y;
return this;
}
add(v) {
if(typeof(v) === "object") this.x += v.x, this.y += v.y;
if(typeof(v) === "number") this.x += v, this.y += v;
return this;
}
sub(v) {
if(typeof(v) === "object") this.x -= v.x, this.y -= v.y;
if(typeof(v) === "number") this.x -= v, this.y -= v;
return this;
}
mul(v) {
if(typeof(v) === "object") this.x *= v.x, this.y *= v.y;
if(typeof(v) === "number") this.x *= v, this.y *= v;
return this;
}
div(v) {
if(typeof(v) === "object") this.x /= v.x, this.y /= v.y;
if(typeof(v) === "number") this.x /= v, this.y /= v;
return this;
}
cross(x) {
return this.x * x.y - this.y * x.x;
}
as_string() {
return this.x + ' ' + this.y;
}
rotate(x) {
var ca = Math.cos(x);
var sa = Math.sin(x);
this.x = ca * this.x - sa * this.y;
this.y = sa * this.x + ca * this.y;
}
gt(v) {
return this.x >= v.x && this.y >= v.y;
}
dot(v) {
return this.x * v.x + this.y * v.y;
}
normalize() {
let l = this.length();
if(l == 0){
return this;
}
this.x = this.x / l;
this.y = this.y / l;
return this;
}
length() {
return Math.sqrt(this.x**2 + this.y**2);
}
angle(v) {
let cos_theta = this.dot(v) / (this.length() * v.length());
let angle = Math.acos(cos_theta);
return angle;
}
copy() {
return new Vec2(this.x, this.y);
}
trunc() {
return new Vec2(Math.trunc(this.x), Math.trunc(this.y));
}
cmp(v) {
return this.x == v.x && this.y == v.y;
}
};