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
|
var assert = require("assert")
var vec = require("../public/assets/javascripts/rectangles/models/vec2.js")
var Rect = require("../public/assets/javascripts/rectangles/models/rect.js")
var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
var ALL = FRONT | BACK | LEFT | RIGHT
describe('rect', function(){
describe('#intersects()', function(){
var rect = new Rect(0, 0, 10, 10)
it('intersects itself', function(){
assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) ));
})
it('intersects more wide', function(){
assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) ));
})
it('intersects less wide', function(){
assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) ));
})
it('intersects more tall', function(){
assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) ));
})
it('intersects less tall', function(){
assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) ));
})
it('intersects if right-adjacent', function(){
assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) ));
})
it('intersects if bottom-adjacent', function(){
assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) ));
})
it('does not intersect if to the right', function(){
assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) ));
})
it('does not intersect if beneath', function(){
assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) ));
})
/*
it('does not intersect if corners intersect', function(){
assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) ));
})
*/
})
var rect = new Rect( new vec(1,4), new vec(1,4) )
var east_in = new Rect( new vec(2,3), new vec(1,4) )
var east_edge = new Rect( new vec(2,4), new vec(1,4) )
var east = new Rect( new vec(2,5), new vec(1,4) )
var south_in = new Rect( new vec(1,4), new vec(2,3) )
var south_edge = new Rect( new vec(1,4), new vec(2,4) )
var south = new Rect( new vec(1,4), new vec(2,5) )
var corner = new Rect( new vec(3,5), new vec(3,5) )
function sides (s) {
return s.reduce(function(prev, curr){
return prev | curr.sides
}, 0)
}
/*
console.log(s0.map(function(r){ return r.toString() }))
console.log(s1.map(function(r){ return r.toString() }))
*/
describe('#split(rect, east)', function(){
var s0 = rect.split(east)
var s1 = east.split(rect)
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect is front/back/left', function(){
assert.equal(FRONT | BACK | LEFT, sides(s0))
})
it('east is front/back/right', function(){
assert.equal(FRONT | BACK | RIGHT, sides(s1))
})
})
describe('#split(rect, east_in)', function(){
var s0 = rect.split(east_in)
var s1 = east_in.split(rect)
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect is has all sides', function(){
assert.equal(ALL, sides(s0))
})
it('east_in only has front/back', function(){
assert.equal(FRONT | BACK, sides(s1))
})
})
describe('#split(rect, east_edge)', function(){
var s0 = rect.split(east_edge)
var s1 = east_edge.split(rect)
it('has no degenerate vectors', function(){
s0.forEach(function(r){
assert.notEqual(0, r.x.magnitude())
assert.notEqual(0, r.y.magnitude())
})
s1.forEach(function(r){
assert.notEqual(0, r.x.magnitude())
assert.notEqual(0, r.y.magnitude())
})
})
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect is front/back/left', function(){
assert.equal(ALL, sides(s0))
})
it('east is front/back/right', function(){
assert.equal(FRONT | BACK | RIGHT, sides(s1))
})
})
describe('#split(rect, south)', function(){
var s0 = rect.split(south)
var s1 = south.split(rect)
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect is front/left/right', function(){
assert.equal(FRONT | LEFT | RIGHT, sides(s0))
})
it('south is back/left/right', function(){
assert.equal(BACK | LEFT | RIGHT, sides(s1))
})
})
describe('#split(rect, south_in)', function(){
var s0 = rect.split(south_in)
var s1 = south_in.split(rect)
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect is has all sides', function(){
assert.equal(ALL, sides(s0))
})
it('south_in only has left/right', function(){
assert.equal(LEFT | RIGHT, sides(s1))
})
})
describe('#split(rect, south_edge)', function(){
var s0 = rect.split(south_edge)
var s1 = south_edge.split(rect)
it('has no degenerate vectors', function(){
s0.forEach(function(r){
assert.notEqual(0, r.x.magnitude())
assert.notEqual(0, r.y.magnitude())
})
s1.forEach(function(r){
assert.notEqual(0, r.x.magnitude())
assert.notEqual(0, r.y.magnitude())
})
})
it('splits on all 4 sides', function(){
assert.equal(ALL, sides(s0) | sides(s1))
})
it('rect has all sides', function(){
assert.equal(ALL, sides(s0))
})
it('south is back/left/right', function(){
assert.equal(BACK | LEFT | RIGHT, sides(s1))
})
})
describe('#split(rect, corner)', function(){
var s0 = rect.split(corner)
var s1 = corner.split(rect)
it('rect splits on all 4 sides', function(){
assert.equal(ALL, sides(s0))
})
it('corner splits on all 4 sides', function(){
assert.equal(ALL, sides(s1))
})
var rect_map = {}
var corner_map = {}
var rect_state = s0.forEach(function(r){
rect_map[r.sides] = rect_map[r.sides] || []
rect_map[r.sides].push(r)
})
var corner_state = s1.forEach(function(r){
corner_map[r.sides] = corner_map[r.sides] || []
corner_map[r.sides].push(r)
})
it('rect contains a rect with no sides', function(){
assert.equal(1, rect_map[0].length)
})
it('corner contains a rect with no sides', function(){
assert.equal(1, corner_map[0].length)
})
it('rect and corner overlap', function(){
assert.equal(String(rect_map[0][0]), String(corner_map[0][0]))
})
})
})
|