|
|
|
@ -111,6 +111,62 @@ var Util = (function UtilClosure() {
@@ -111,6 +111,62 @@ var Util = (function UtilClosure() {
|
|
|
|
|
]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Normalize rectangle rect=[x1, y1, x2, y2] so that (x1,y1) < (x2,y2)
|
|
|
|
|
// For coordinate systems whose origin lies in the bottom-left, this
|
|
|
|
|
// means normalization to (BL,TR) ordering. For systems with origin in the
|
|
|
|
|
// top-left, this means (TL,BR) ordering.
|
|
|
|
|
Util.normalizeRect = function normalizeRect(rect) { |
|
|
|
|
var r = rect.slice(0); // clone rect
|
|
|
|
|
if (rect[0] > rect[2]) { |
|
|
|
|
r[0] = rect[2]; |
|
|
|
|
r[2] = rect[0]; |
|
|
|
|
} |
|
|
|
|
if (rect[1] > rect[3]) { |
|
|
|
|
r[1] = rect[3]; |
|
|
|
|
r[3] = rect[1]; |
|
|
|
|
} |
|
|
|
|
return r; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Returns a rectangle [x1, y1, x2, y2] corresponding to the
|
|
|
|
|
// intersection of rect1 and rect2. If no intersection, returns 'false'
|
|
|
|
|
// The rectangle coordinates of rect1, rect2 should be [x1, y1, x2, y2]
|
|
|
|
|
Util.intersect = function intersect(rect1, rect2) { |
|
|
|
|
function compare(a, b) { |
|
|
|
|
return a - b; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Order points along the axes
|
|
|
|
|
var orderedX = [rect1[0], rect1[2], rect2[0], rect2[2]].sort(compare), |
|
|
|
|
orderedY = [rect1[1], rect1[3], rect2[1], rect2[3]].sort(compare), |
|
|
|
|
result = []; |
|
|
|
|
|
|
|
|
|
rect1 = Util.normalizeRect(rect1); |
|
|
|
|
rect2 = Util.normalizeRect(rect2); |
|
|
|
|
|
|
|
|
|
// X: first and second points belong to different rectangles?
|
|
|
|
|
if ((orderedX[0] === rect1[0] && orderedX[1] === rect2[0]) || |
|
|
|
|
(orderedX[0] === rect2[0] && orderedX[1] === rect1[0])) { |
|
|
|
|
// Intersection must be between second and third points
|
|
|
|
|
result[0] = orderedX[1]; |
|
|
|
|
result[2] = orderedX[2]; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Y: first and second points belong to different rectangles?
|
|
|
|
|
if ((orderedY[0] === rect1[1] && orderedY[1] === rect2[1]) || |
|
|
|
|
(orderedY[0] === rect2[1] && orderedY[1] === rect1[1])) { |
|
|
|
|
// Intersection must be between second and third points
|
|
|
|
|
result[1] = orderedY[1]; |
|
|
|
|
result[3] = orderedY[2]; |
|
|
|
|
} else { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Util.sign = function sign(num) { |
|
|
|
|
return num < 0 ? -1 : 1; |
|
|
|
|
}; |
|
|
|
|