aboutsummaryrefslogtreecommitdiffstats
path: root/lib/rect.coffee
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rect.coffee')
-rw-r--r--lib/rect.coffee34
1 files changed, 26 insertions, 8 deletions
diff --git a/lib/rect.coffee b/lib/rect.coffee
index 67c9de7c..adc1fc36 100644
--- a/lib/rect.coffee
+++ b/lib/rect.coffee
@@ -9,8 +9,16 @@ Rect =
width: x2 - x1
height: y2 - y1
+ copy: (rect) ->
+ bottom: rect.bottom
+ top: rect.top
+ left: rect.left
+ right: rect.right
+ width: rect.width
+ height: rect.height
+
# Translate a rect by x horizontally and y vertically.
- translate: (rect, x, y) ->
+ translate: (rect, x = 0, y = 0) ->
bottom: rect.bottom + y
top: rect.top + y
left: rect.left + x
@@ -19,18 +27,17 @@ Rect =
height: rect.height
# Subtract rect2 from rect1, returning an array of rects which are in rect1 but not rect2.
- subtract: (rect1, rect2_) ->
+ subtract: (rect1, rect2) ->
# Bound rect2 by rect1
- rect2 = {}
rect2 = @create(
- Math.max(rect1.left, rect2_.left),
- Math.max(rect1.top, rect2_.top),
- Math.min(rect1.right, rect2_.right),
- Math.min(rect1.bottom, rect2_.bottom)
+ Math.max(rect1.left, rect2.left),
+ Math.max(rect1.top, rect2.top),
+ Math.min(rect1.right, rect2.right),
+ Math.min(rect1.bottom, rect2.bottom)
)
# If bounding rect2 has made the width or height negative, rect1 does not contain rect2.
- return [rect1] if rect2.width < 0 or rect2.height < 0
+ return [Rect.copy rect1] if rect2.width < 0 or rect2.height < 0
#
# All the possible rects, in the order
@@ -60,5 +67,16 @@ Rect =
rects.filter (rect) -> rect.height > 0 and rect.width > 0
+ contains: (rect1, rect2) ->
+ rect1.right > rect2.left and
+ rect1.left < rect2.right and
+ rect1.bottom > rect2.top and
+ rect1.top < rect2.bottom
+
+ equals: (rect1, rect2) ->
+ for property in ["top", "bottom", "left", "right", "width", "height"]
+ return false if rect1[property] != rect2[property]
+ true
+
root = exports ? window
root.Rect = Rect