-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSweepLineClass.js
More file actions
156 lines (109 loc) · 3.5 KB
/
Copy pathSweepLineClass.js
File metadata and controls
156 lines (109 loc) · 3.5 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
class SweepLineClass {
constructor () {
this._objectNodeMap = new Map();
this._queueHead = null;
}
add( object, loVal, hiVal ) {
let hiNode = new SweepLineClass.NodeClass( this, object, SweepLineClass._HI, hiVal );
let loNode = new SweepLineClass.NodeClass( this, object, SweepLineClass._LO, loVal );
this._objectNodeMap.set( object, {loNode: loNode, hiNode: hiNode} );
}
update( object, loVal, hiVal ) {
let n = this._objectNodeMap.get( object );
if ( n ) {
n.hiNode.x = hiVal || n.hiNode.x;
this._sortNode( n.hiNode );
n.loNode.x = loVal || n.loNode.x;
this._sortNode( n.loNode );
}
}
del( object ) {
n = this._objectNodeMap.get( object );
if ( n ) {
this._deleteNode( n.hiNode );
this._deleteNode( n.loNode );
this._objectNodeMap.delete( object );
}
}
_sortNode( node ) {
function moveNode() {
// Remove node from current position in queue.
this._deleteNode( node );
// Add node to new position in queue.
if ( newLocation === null ) {
node._prev = null;
node._next = this._queueHead;
this._queueHead = node;
} else {
node._prev = newLocation;
node._next = newLocation._next;
if ( newLocation._next ) newLocation._next._prev = node;
newLocation._next = node;
}
}
// Walk the queue, moving node into the
// proper spot of the queue based on the x
// value. First check against the '_prev' queue
// node...
let newLocation = node._prev;
while (newLocation && node._x < newLocation._x ) {
newLocation = newLocation._prev;
}
if (newLocation !== node._prev) moveNode.call( this );
// ...then against the '_next' queue node.
newLocation = node;
while (newLocation._next && newLocation._next._x < node._x ) {
newLocation = newLocation._next;
}
if (newLocation !== node) moveNode.call( this );
}
_deleteNode( node ) {
if ( node._prev === null ) this._queueHead = node._next;
if ( node._prev ) node._prev._next = node._next;
if ( node._next ) node._next._prev = node._prev;
}
findCollisions( collisionFunction ) {
let collision = [];
let activeObjects = new Set();
var node = this._queueHead;
while ( node ) {
if ( node._loHi === SweepLineClass._LO ) {
let object = node._object;
for ( let ao of activeObjects ) {
//const ao = activeObjects[ o ];
if ( collisionFunction( object, ao )) {
collision.push( [ object, ao ] );
}
}
activeObjects.add( object );
} else { // node._loHi === SweepLineClass._HI
activeObjects.delete( node._object );
}
node = node._next;
}
return collision;
}
print( printFunction ) {
var n = this._queueHead;
while (n) {
printFunction( n._object, n._loHi, n._x )
n = n._next;
}
}
}
SweepLineClass._LO = false;
SweepLineClass._HI = true;
SweepLineClass.NodeClass = class {
constructor( sweepLine, object, loHi, x ) {
this._object = object;
this._parent = sweepLine;
this._loHi = loHi;
this._x = x;
this._prev = null;
this._next = null;
if ( sweepLine._queueHead ) sweepLine._queueHead.prev = this;
this._next = sweepLine._queueHead;
sweepLine._queueHead = this;
sweepLine._sortNode( this );
}
}