-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUtils.js
More file actions
353 lines (292 loc) · 7.76 KB
/
Copy pathUtils.js
File metadata and controls
353 lines (292 loc) · 7.76 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* This file is part of min.
*
* min is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* min is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with min. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) 2011-2014 Richard Pospesel, Kevin Hart, Lei Hu, Siyu Zhu, David Stalnaker,
* Christopher Sasarak, Robert LiVolsi, Awelemdy Orakwue, and Richard Zanibbi
* (Document and Pattern Recognition Lab, RIT)
*/
/*
Contains the Vector2 object which is used to create vectors of length 2 and methods for
doing math on them. Vector2s are typically used in Min to represent (x,y) coordinates.
Methods includes but not limited to:
Pointwise
SquareDistance
Distance
Subtract
Multiply
transform: Translates and scales a Vector2
*/
function Tuple(a,b,c,d,e,f,g,h)
{
this.item1 = a;
this.item2 = b;
this.item3 = c;
this.item4 = d;
this.item5 = e;
this.item6 = f;
this.item7 = g;
this.item8 = h;
}
/// RGB
function RGB(in_red, in_green, in_blue)
{
this.red = in_red;
this.green = in_green;
this.blue = in_blue;
}
// expects string in the format: #XXXXXX for hex color
RGB.parseRGB = function(in_hex_string)
{
if(in_hex_string.length != 7)
{
return new RGB(255,0,255);
}
else
{
var red = parseInt(in_hex_string.substring(1, 3), 16);
var green = parseInt(in_hex_string.substring(3, 5), 16);
var blue = parseInt(in_hex_string.substring(5, 7), 16);
if(isNaN(red) || isNaN(green) || isNaN(blue))
return new RGB(255, 0, 255);
return new RGB(red, green, blue);
}
}
/// Vector2
// simple vector 2 objecct
function Vector2(in_x, in_y)
{
this.x = in_x;
this.y = in_y;
}
parseVector2 = function(in_string)
{
var strings = in_string.split(',');
var x = parseFloat(strings[0]);
var y = parseFloat(strings[1]);
return new Vector2(x,y);
}
Vector2.prototype.Set = function(in_vector)
{
this.x = in_vector.x;
this.y = in_vector.y;
}
Vector2.prototype.magnitude = function()
{
return Math.sqrt(this.x * this.x + this.y*this.y);
}
Vector2.prototype.normalize = function()
{
var mag = this.magnitude();
this.x /= mag;
this.y /= mag;
return this;
}
Vector2.prototype.toString = function()
{
return this.x + "," + this.y;
}
Vector2.Dot = function(a, b)
{
return a.x * b .x + a.y * b.y;
}
Vector2.Pointwise = function(a, b)
{
return new Vector2(a.x * b.x, a.y * b.y);
}
Vector2.SquareDistance = function(a,b)
{
var diff_x = a.x - b.x;
var diff_y = a.y - b.y;
return diff_x * diff_x + diff_y * diff_y;
}
Vector2.Distance = function(a,b)
{
return Math.sqrt(Vector2.SquareDistance(a,b));
}
Vector2.Subtract = function(a, b)
{
return new Vector2(a.x - b.x, a.y - b.y);
}
Vector2.Add = function(a, b)
{
return new Vector2(a.x + b.x, a.y + b.y);
}
Vector2.prototype.Add = function(a)
{
this.x += a.x;
this.y += a.y;
}
Vector2.prototype.Subtract = function(a)
{
this.x -= a.x;
this.y -= a.y;
}
Vector2.Multiply = function(f, v)
{
return new Vector2(v.x * f, v.y * f);
}
Vector2.prototype.equals = function(a)
{
return this.x == a.x && this.y == a.y;
}
Vector2.Equals = function(a,b)
{
return a.x == b.x && a.y == b.y;
}
Vector2.prototype.transform = function(in_scale, in_translation)
{
return new Vector2(this.x * in_scale.x + in_translation.x, this.y * in_scale.y + in_translation.y);
}
Vector2.prototype.clone = function()
{
return new Vector2(this.x, this.y);
}
/// StringBuilder
// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
{
this.strings = new Array("");
this.append(value);
}
// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
this.strings.push(value);
return this;
}
StringBuilder.prototype.appendLine = function()
{
this.strings.push("\n");
return this;
}
// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
this.strings.length = 1;
}
// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
return this.strings.join("");
}
/// Array
Array.prototype.contains = function(value)
{
for(var k = 0; k < this.length; k++)
if(value == this[k])
return true;
return false;
}
Array.prototype.clone = function()
{
var result = new Array();
for(var k = 0; k < this.length; k++)
{
result.push(this[k]);
}
return result;
}
/// Canvas
// stolen from: http://davidowens.wordpress.com/2010/09/07/html-5-canvas-and-dashed-lines/
// pattern in an array of lengths (alternate for dash and space between dash)
// ie:
// var pattern = new Array(1, 2)
// renders as:
// - - - - - - - - -
// var pattern = new Array(3, 2)
// renders as:
// --- --- --- --- ---
CanvasRenderingContext2D.prototype.dashedLineTo = function (fromX, fromY, toX, toY, pattern)
{
// Our growth rate for our line can be one of the following:
// (+,+), (+,-), (-,+), (-,-)
// Because of this, our algorithm needs to understand if the x-coord and
// y-coord should be getting smaller or larger and properly cap the values
// based on (x,y).
var lt = function (a, b) { return a <= b; };
var gt = function (a, b) { return a >= b; };
var capmin = function (a, b) { return Math.min(a, b); };
var capmax = function (a, b) { return Math.max(a, b); };
var checkX = { thereYet: gt, cap: capmin };
var checkY = { thereYet: gt, cap: capmin };
if (fromY - toY > 0) {
checkY.thereYet = lt;
checkY.cap = capmax;
}
if (fromX - toX > 0) {
checkX.thereYet = lt;
checkX.cap = capmax;
}
this.moveTo(fromX, fromY);
var offsetX = fromX;
var offsetY = fromY;
var idx = 0, dash = true;
while (!(checkX.thereYet(offsetX, toX) && checkY.thereYet(offsetY, toY)))
{
var ang = Math.atan2(toY - fromY, toX - fromX);
var len = pattern[idx];
offsetX = checkX.cap(toX, offsetX + (Math.cos(ang) * len));
offsetY = checkY.cap(toY, offsetY + (Math.sin(ang) * len));
if (dash) this.lineTo(offsetX, offsetY);
else this.moveTo(offsetX, offsetY);
idx = (idx + 1) % pattern.length;
dash = !dash;
}
};
/// Math class extentions
Math.sign = function(f)
{
if(f > 0)
return 1;
if(f < 0)
return -1;
return 0;
}
/** Helper method to find aboslute location of our parent div **/
function findPosition(in_obj)
{
var left = 0;
var top = 0;
if(in_obj.offsetParent)
{
do
{
left += in_obj.offsetLeft;
top += in_obj.offsetTop;
}
while(in_obj = in_obj.offsetParent);
}
return [left, top];
}
/* A bounded queue defines a queue which is bounded to some
maximum value. Adding an item which would go over the boundary
results in the oldest item being removed and the new item being added to the list.
*/
BoundedQueue.prototype = new Array();
function BoundedQueue(upperBound){
console.log(upperBound);
this.upperBound = upperBound;
}
BoundedQueue.prototype.enqueue = function(item){
if(this.length == this.upperBound){
this.shift();
}
this.push(item);
}
BoundedQueue.prototype.clear = function(){
this.splice(0, this.upperBound);
}