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
|
(function(){
var vec2
if ('window' in this) {
vec2 = window.vec2
}
else {
vec2 = require('./vec2')
FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
TOP = CEILING, BOTTOM = FLOOR
function sidesToString(sides){
var s = ""
if (sides & FRONT) s += "front "
if (sides & BACK) s += "back "
if (sides & LEFT) s += "left "
if (sides & RIGHT) s += "right "
if (sides & TOP) s += "top "
if (sides & BOTTOM) s += "bottom "
return s
}
}
var Rect = function (x0,y0,x1,y1){
if (x0 instanceof vec2) {
this.x = x0
this.y = y0
}
else if (x1 === undefined) {
this.x = new vec2(x0,x0)
this.y = new vec2(y0,y0)
}
else {
this.x = new vec2(x0,x1)
this.y = new vec2(y0,y1)
}
this.translation = new vec2(0,0)
this.sides = FRONT | BACK | LEFT | RIGHT
}
Rect.prototype.clone = function(){
return new Rect( this.x.clone(), this.y.clone() )
}
Rect.prototype.assign = function(r) {
this.x.assign(r.x)
this.y.assign(r.y)
return this
}
Rect.prototype.center = function(){
return new vec2(this.x.midpoint(), this.y.midpoint())
}
Rect.prototype.area = function(){
return this.x.length() * this.y.length()
}
Rect.prototype.magnitude = function(){
return dist(this.x.a, this.y.a, this.x.b, this.y.b)
}
Rect.prototype.maxDimension = function(){
return abs(this.width) > abs(this.height) ? this.width : this.height
}
Rect.prototype.mul = function(n){
this.x.mul(n)
this.y.mul(n)
return this
}
Rect.prototype.div = function(n){
this.x.div(n)
this.y.div(n)
return this
}
Rect.prototype.translate = function(translation){
var translation = translation || this.translation
this.x.abs().add(translation.a)
this.y.abs().add(translation.b)
this.translation.a = this.translation.b = 0
return this
}
Rect.prototype.resize = function(translation, sides){
var translation = translation || this.translation
sides = sides || translation.sides
if (sides & LEFT) {
this.x.a += translation.a
}
if (sides & RIGHT) {
this.x.b += translation.a
}
if (sides & FRONT) {
this.y.a += translation.b
}
if (sides & BACK) {
this.y.b += translation.b
}
this.translation.a = this.translation.b = 0
}
Rect.prototype.contains = function(x,y){
return this.x.contains(x) && this.y.contains(y)
}
Rect.prototype.contains_point = function(p){
return this.x.contains(p.x) && this.y.contains(p.y)
}
Rect.prototype.containsDisc = function(x,y,r){
return this.x.containsDisc(x,r) && this.y.containsDisc(y,r)
}
Rect.prototype.overlaps = function(rect){
return this.x.overlaps(rect.x) && this.y.overlaps(rect.y)
}
Rect.prototype.intersects = function(r){
var corner_intersect = (this.x.b === r.x.a && this.y.b === r.y.a)
return this.x.intersects(r.x) && this.y.intersects(r.y) && ! corner_intersect
}
Rect.prototype.adjacent = function(r){
return this.x.adjacent(r.x) && this.y.adjacent(r.y)
}
Rect.prototype.eq = function(r){
return this.x.eq(r.x) && this.y.eq(r.y)
}
Rect.prototype.fits = function(v){
return this.x.length() >= v.a && this.y.length() >= v.b
}
Rect.prototype.zero = function(){
this.a.zero()
this.b.zero()
}
Rect.prototype.nearEdge = function (x, y, r) {
var edges = 0
if (x < this.x.a+r) {
edges |= LEFT
}
else if (x > this.x.b-r) {
edges |= RIGHT
}
if (y < this.y.a+r) {
edges |= FRONT
}
else if (y > this.y.b-r) {
edges |= BACK
}
return edges
}
Rect.prototype.width = function(){ return this.x.length() }
Rect.prototype.height = function(){ return this.y.length() }
Rect.prototype.delta = function(){ return new vec2( this.x.magnitude(), this.y.magnitude() ) }
Rect.prototype.expand = function(rect){
this.x.a = Math.min( this.x.a, rect.x.a )
this.x.b = Math.max( this.x.b, rect.x.b )
this.y.a = Math.min( this.y.a, rect.y.a )
this.y.b = Math.max( this.y.b, rect.y.b )
return this
}
Rect.prototype.square = function(){
var width = this.x.length()
var height = this.y.length()
var diff
if (width < height) {
diff = (height - width) / 2
this.x.a -= diff
this.x.b += diff
}
else {
diff = (width - height) / 2
this.y.a -= diff
this.y.b += diff
}
return this
}
Rect.prototype.toString = function(){
var sides = sidesToString(this.sides)
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
Rect.prototype.exactString = function(){
var sides = sidesToString(this.sides)
var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] " + sides
return s
}
Rect.prototype.serialize = function(){
return { x: this.x.serialize(), y: this.y.serialize() }
}
Rect.prototype.quantize = function(n){
this.x.quantize(n)
this.y.quantize(n)
return this
}
Rect.prototype.split = function(r){
var rz = this
var splits = []
var sides = this.sides
// bisect (or trisect) two overlapping rectangles
var x_intervals = this.x.split( r.x, LEFT, RIGHT )
var y_intervals = this.y.split( r.y, FRONT, BACK )
// generate rectangular regions by crossing the two sets of vectors
x_intervals.forEach(function(x, i){
y_intervals.forEach(function(y, i){
var rn = new Rect(x[0], y[0])
rn.id = rz.id
rn.sides = ((x[1] | y[1]) & sides)
rn.focused = rz.focused
splits.push(rn)
// cull extra walls from overlapping regions
if (r.x.contains(rn.x.a) && r.x.contains(rn.x.b)) {
if (rz.y.a == rn.y.a && r.y.containsCenter(rn.y.a)) { // top edges
rn.sides &= ~ FRONT
}
if (rz.y.b == rn.y.b && r.y.containsCenter(rn.y.b)) { // bottom edges
rn.sides &= ~ BACK
}
}
if (r.y.contains(rn.y.a) && r.y.contains(rn.y.b)) {
if (rz.x.a == rn.x.a && r.x.containsCenter(rn.x.a)) { // left edges
rn.sides &= ~ LEFT
}
if (rz.x.b == rn.x.b && r.x.containsCenter(rn.x.b) ) { // right edges
rn.sides &= ~ RIGHT
}
}
})
})
return splits
}
if ('window' in this) {
window.Rect = Rect
}
else {
module.exports = Rect
}
})()
|