forked from WhatPumpkin/Sburb-Legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Path.js
52 lines (39 loc) · 1.25 KB
/
Path.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
var Sburb = (function(Sburb){
/////////////////////////////////////
//Path class
/////////////////////////////////////
//constructor
Sburb.Path = function(){
this.points = [];
}
//add a point to the path
Sburb.Path.prototype.push = function(point){
this.points.push(point);
}
//Check if the given points are in the path, favouring positively
Sburb.Path.prototype.queryBatchPos = function(queries,results){
for(var query in queries){
if(!queries.hasOwnProperty(query)) continue;
results[query] = results[query] || this.query(queries[query]);
}
}
//Check if the given points are in the path, favouring negatively
Sburb.Path.prototype.queryBatchNeg = function(queries,results){
for(var query in queries){
if(!queries.hasOwnProperty(query)) continue;
results[query] = results[query] && !this.query(queries[query]);
}
}
//Check if the given point is in the path
Sburb.Path.prototype.query = function(pt){
for(var c = false, i = -1, l = this.points.length, j = l-1; ++i < l;j=i){
var ptA = this.points[i];
var ptB = this.points[j];
((ptA.y <= pt.y && pt.y < ptB.y) || (ptB.y <= pt.y && pt.y < ptA.y))
&& (pt.x < (ptB.x - ptA.x) * (pt.y - ptA.y) / (ptB.y - ptA.y) + ptA.x)
&& (c = !c);
}
return c;
}
return Sburb;
})(Sburb || {});