summaryrefslogtreecommitdiff
path: root/test/02-test-rect.js
blob: 39693f5a9616f8cbdaef257f12ae8094366b335f (plain)
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
var assert = require("assert")
var vec = require("../public/assets/javascripts/rectangles/models/vec2.js")
var Rect = require("../public/assets/javascripts/rectangles/models/rect.js")
var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
var ALL = FRONT | BACK | LEFT | RIGHT

describe('rect', function(){
  describe('#intersects()', function(){
		var rect = new Rect(0, 0, 10, 10)

    it('intersects itself', function(){
      assert.equal(true, rect.intersects( new Rect(0, 0, 10, 10) ));
    })
    it('intersects more wide', function(){
      assert.equal(true, rect.intersects( new Rect(0, 0, 5, 10) ));
    })
    it('intersects less wide', function(){
      assert.equal(true, rect.intersects( new Rect(0, 0, 15, 10) ));
    })
    it('intersects more tall', function(){
      assert.equal(true, rect.intersects( new Rect(0, 0, 10, 5) ));
    })
    it('intersects less tall', function(){
      assert.equal(true, rect.intersects( new Rect(0, 0, 10, 15) ));
    })
    it('intersects if right-adjacent', function(){
      assert.equal(true, rect.intersects( new Rect(10, 0, 20, 10) ));
    })
    it('intersects if bottom-adjacent', function(){
      assert.equal(true, rect.intersects( new Rect(0, 10, 10, 20) ));
    })
    it('does not intersect if to the right', function(){
      assert.equal(false, rect.intersects( new Rect(20, 0, 40, 10) ));
    })
    it('does not intersect if beneath', function(){
      assert.equal(false, rect.intersects( new Rect(0, 20, 10, 40) ));
    })
  /*
    it('does not intersect if corners intersect', function(){
      assert.equal(false, rect.intersects( new Rect(10, 10, 20, 20) ));
    })
  */

  })

	var rect       = new Rect( new vec(1,4), new vec(1,4) )

	var east_in    = new Rect( new vec(2,3), new vec(1,4) )
	var east_edge  = new Rect( new vec(2,4), new vec(1,4) )
	var east       = new Rect( new vec(2,5), new vec(1,4) )

	var south_in   = new Rect( new vec(1,4), new vec(2,3) )
	var south_edge = new Rect( new vec(1,4), new vec(2,4) )
	var south      = new Rect( new vec(1,4), new vec(2,5) )

	var corner     = new Rect( new vec(3,5), new vec(3,5) )

  function sides (s) {
    return s.reduce(function(prev, curr){
      return prev | curr.sides
    }, 0)
  }

  describe('#split(rect, east)', function(){
    var s0 = rect.split(east)
    var s1 = east.split(rect)
    it('splits on all 4 sides', function(){
      assert.equal(ALL, sides(s0) | sides(s1))
    })
    it('rect is front/back/left', function(){
      assert.equal(FRONT | BACK | LEFT, sides(s0))
    })
    it('east is front/back/right', function(){
      assert.equal(FRONT | BACK | RIGHT, sides(s1))
    })
  })

  describe('#split(rect, east_in)', function(){
    var s0 = rect.split(east_in)
    var s1 = east_in.split(rect)
    it('splits on all 4 sides', function(){
      assert.equal(ALL, sides(s0) | sides(s1))
    })
    it('rect is has all sides', function(){
      assert.equal(ALL, sides(s0))
    })
    it('east_in only has front/back', function(){
      assert.equal(FRONT | BACK, sides(s1))
    })
  })

  describe('#split(rect, east_edge)', function(){
    var s0 = rect.split(east_edge)
    var s1 = east_edge.split(rect)

    console.log("\n")
    console.log(rect+"")
    console.log(east_edge+"")
    console.log(s0.map(function(r){ return r.toString() }))
    console.log(s1.map(function(r){ return r.toString() }))

    it('splits on all 4 sides', function(){
      assert.equal(ALL, sides(s0) | sides(s1))
    })
    it('rect is front/back/left', function(){
      assert.equal(FRONT | BACK | LEFT, sides(s0))
    })
    it('east is front/back/right', function(){
      assert.equal(FRONT | BACK | RIGHT, sides(s1))
    })
  })

})
/*
*/