| 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
 | 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.intersects 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.intersects 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.intersects subtractRect, complementRect
context "Rect overlaps",
  should "detect that a rect overlaps itself", ->
    rect = Rect.create 2, 2, 4, 4
    assert.isTrue Rect.rectsOverlap rect, rect
  should "detect that non-overlapping rectangles do not overlap on the left", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 0, 2, 1, 4
    assert.isFalse Rect.rectsOverlap rect1, rect2
  should "detect that non-overlapping rectangles do not overlap on the right", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 5, 2, 6, 4
    assert.isFalse Rect.rectsOverlap rect1, rect2
  should "detect that non-overlapping rectangles do not overlap on the top", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 2, 0, 2, 1
    assert.isFalse Rect.rectsOverlap rect1, rect2
  should "detect that non-overlapping rectangles do not overlap on the bottom", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 2, 5, 2, 6
    assert.isFalse Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles on the left", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 0, 2, 2, 4
    assert.isTrue Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles on the right", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 4, 2, 5, 4
    assert.isTrue Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles on the top", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 2, 4, 4, 5
    assert.isTrue Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles on the bottom", ->
    rect1 = Rect.create 2, 2, 4, 4
    rect2 = Rect.create 2, 0, 4, 2
    assert.isTrue Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles when second rectangle is contained in first", ->
    rect1 = Rect.create 1, 1, 4, 4
    rect2 = Rect.create 2, 2, 3, 3
    assert.isTrue Rect.rectsOverlap rect1, rect2
  should "detect overlapping rectangles when first rectangle is contained in second", ->
    rect1 = Rect.create 1, 1, 4, 4
    rect2 = Rect.create 2, 2, 3, 3
    assert.isTrue Rect.rectsOverlap rect2, rect1
 |