diff options
| -rw-r--r-- | lib/rect.coffee | 34 | ||||
| -rw-r--r-- | tests/unit_tests/rect_test.coffee | 232 | 
2 files changed, 258 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 diff --git a/tests/unit_tests/rect_test.coffee b/tests/unit_tests/rect_test.coffee new file mode 100644 index 00000000..cfb26b05 --- /dev/null +++ b/tests/unit_tests/rect_test.coffee @@ -0,0 +1,232 @@ +require "./test_helper.js" +extend(global, require "../../lib/rect.js") + +context "Rect", +  should "set rect properties correctly", -> +    [x1, y1, x2, y2] = [1, 2, 3, 4] +    rect = Rect.create x1, y1, x2, y2 +    assert.equal rect.left, x1 +    assert.equal rect.top, y1 +    assert.equal rect.right, x2 +    assert.equal rect.bottom, y2 +    assert.equal rect.width, x2 - x1 +    assert.equal rect.height, y2 - y1 + +  should "translate rect horizontally", -> +    [x1, y1, x2, y2] = [1, 2, 3, 4] +    x = 5 +    rect1 = Rect.create x1, y1, x2, y2 +    rect2 = Rect.translate rect1, x + +    assert.equal rect1.left + x, rect2.left +    assert.equal rect1.right + x, rect2.right + +    assert.equal rect1.width, rect2.width +    assert.equal rect1.height, rect2.height +    assert.equal rect1.top, rect2.top +    assert.equal rect1.bottom, rect2.bottom + +  should "translate rect vertically", -> +    [x1, y1, x2, y2] = [1, 2, 3, 4] +    y = 5 +    rect1 = Rect.create x1, y1, x2, y2 +    rect2 = Rect.translate rect1, undefined, y + +    assert.equal rect1.top + y, rect2.top +    assert.equal rect1.bottom + y, rect2.bottom + +    assert.equal rect1.width, rect2.width +    assert.equal rect1.height, rect2.height +    assert.equal rect1.left, rect2.left +    assert.equal rect1.right, rect2.right + +context "Rect subtraction", +  context "unchanged by rects outside", +    should "left, above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -2, -2, -1, -1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "left", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -2, 0, -1, 1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "left, below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -2, 2, -1, 3 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right, above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 2, -2, 3, -1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 2, 0, 3, 1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right, below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 2, 2, 3, 3 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 0, -2, 1, -1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 0, 2, 1, 3 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +  context "unchanged by rects touching", +    should "left, above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -1, -1, 0, 0 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "left", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -1, 0, 0, 1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "left, below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create -1, 1, 0, 2 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right, above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 1, -1, 2, 0 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 1, 0, 2, 1 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "right, below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 1, 1, 2, 2 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "above", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 0, -1, 1, 0 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +    should "below", -> +      rect1 = Rect.create 0, 0, 1, 1 +      rect2 = Rect.create 0, 1, 1, 2 + +      rects = Rect.subtract rect1, rect2 +      assert.equal rects.length, 1 +      rect = rects[0] +      assert.isTrue Rect.equals rect1, rect + +  should "have nothing when subtracting itself", -> +    rect = Rect.create 0, 0, 1, 1 +    rects = Rect.subtract rect, rect +    assert.equal rects.length, 0 + +  should "not overlap subtracted rect", -> +    rect = Rect.create 0, 0, 3, 3 +    for x in [-2..2] +      for y in [-2..2] +        for width in [1..3] +          for height in [1..3] +            subtractRect = Rect.create x, y, (x + width), (y + height) +            resultRects = Rect.subtract rect, subtractRect +            for resultRect in resultRects +              assert.isFalse Rect.contains subtractRect, resultRect + +  should "be contained in original rect", -> +    rect = Rect.create 0, 0, 3, 3 +    for x in [-2..2] +      for y in [-2..2] +        for width in [1..3] +          for height in [1..3] +            subtractRect = Rect.create x, y, (x + width), (y + height) +            resultRects = Rect.subtract rect, subtractRect +            for resultRect in resultRects +              assert.isTrue Rect.contains rect, resultRect + +  should "contain the  subtracted rect in the original minus the results", -> +    rect = Rect.create 0, 0, 3, 3 +    for x in [-2..2] +      for y in [-2..2] +        for width in [1..3] +          for height in [1..3] +            subtractRect = Rect.create x, y, (x + width), (y + height) +            resultRects = Rect.subtract rect, subtractRect +            resultComplement = [Rect.copy rect] +            for resultRect in resultRects +              resultComplement = Array::concat.apply [], +                (resultComplement.map (rect) -> Rect.subtract rect, resultRect) +            assert.isTrue (resultComplement.length == 0 or resultComplement.length == 1) +            if resultComplement.length == 1 +              complementRect = resultComplement[0] +              assert.isTrue Rect.contains subtractRect, complementRect | 
