summaryrefslogtreecommitdiff
path: root/assets/javascripts/rectangles/models/rect.js
blob: 2d48e130885b5101b1dcedf36bd6dc2a8c3a408a (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
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
var FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8,
		FRONT_BACK = FRONT | BACK, LEFT_RIGHT = LEFT | RIGHT

function sidesToString(sides){
	var s = ""
	if (sides & FRONT) s += "front "
	if (sides & BACK)  s += "back "
	if (sides & LEFT)  s += "left "
	if (sides & RIGHT) s += "right "
	return s
}

window.Rect = (function(){
	var Rect = function (x0,y0,x1,y1){
		if (x0 instanceof vec2) {
			this.x = x0
			this.y = y0
		}
		else if (x1 === undefined) {
			this.x = new vec2(x0,x0)
			this.y = new vec2(y0,y0)
		}
		else {
			this.x = new vec2(x0,x1)
			this.y = new vec2(y0,y1)
		}
		this.translation = new vec2(0,0)
		this.sides = FRONT | BACK | LEFT | RIGHT
	}
	Rect.prototype.clone = function(){
		return new Rect( this.x.clone(), this.y.clone() )
	}
	Rect.prototype.center = function(){
		return new vec2(this.x.midpoint(), this.y.midpoint())
	}
	Rect.prototype.area = function(){
		return this.x.length() * this.y.length()
	}

	Rect.prototype.mul = function(n){
		this.x.mul(n)
		this.y.mul(n)
		return this
	}
	Rect.prototype.div = function(n){
		this.x.div(n)
		this.y.div(n)
		return this
	}

	Rect.prototype.translate = function(translation){
		var translation = translation || this.translation
		this.x.abs().add(translation.a)
		this.y.abs().add(translation.b)
		this.translation.a = this.translation.b = 0
		return this
	}
	Rect.prototype.contains = function(x,y){
		return this.x.contains(x) && this.y.contains(y)
	}
	Rect.prototype.containsDisc = function(x,y,r){
		return this.x.containsDisc(x,r) && this.y.containsDisc(y,r)
	}
	Rect.prototype.intersects = function(r){
		return this.x.intersects(r.x) && this.y.intersects(r.y)
	}
	Rect.prototype.width = function(){ return this.x.length() }
	Rect.prototype.height = function(){ return this.y.length() }
	Rect.prototype.toString = function(){
		var sides = sidesToString(this.sides)
		var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
		return s
	}
	Rect.prototype.quantize = function(n){
		this.x.quantize(n)
		this.y.quantize(n)
		return this
	}
	Rect.prototype.split = function(r){
		var rz = this
		var splits = []
		var split_contains = 0
		var x_intervals = [], y_intervals = []
		var sides = this.sides

		// Split vertically
		if (this.x.contains(r.x.a) && r.x.contains(this.x.b)) {
			x_intervals.push([ new vec2( this.x.a,    r.x.a ), LEFT  ])
			x_intervals.push([ new vec2(    r.x.a, this.x.b ), RIGHT ])
			split_contains |= RIGHT
		}

		else if (r.x.contains(this.x.a) && this.x.contains(r.x.b)) {
			x_intervals.push([ new vec2( this.x.a,    r.x.b ), LEFT  ])
			x_intervals.push([ new vec2(    r.x.b, this.x.b ), RIGHT ])
			split_contains |= LEFT
		}
	
		else if (this.x.contains(r.x.a) && this.x.contains(r.x.b)) {
			x_intervals.push([ new vec2( this.x.a,    r.x.a ), LEFT  ])
			x_intervals.push([ new vec2(    r.x.a, 	  r.x.b ), 0     ])
			x_intervals.push([ new vec2(    r.x.b, this.x.b ), RIGHT ])
			split_contains |= LEFT | RIGHT
		}

		else { // if (r.x.contains(this.x.a) && r.x.contains(r.x.b)) {
			x_intervals.push([ new vec2( this.x.a, this.x.b ), LEFT | RIGHT ])
			split_contains |= LEFT | RIGHT
		}

		// Split horizontally
		if (this.y.contains(r.y.a) && r.y.contains(this.y.b)) {
			y_intervals.push([ new vec2( this.y.a,    r.y.a ), FRONT ])
			y_intervals.push([ new vec2(    r.y.a, this.y.b ), BACK  ])
			split_contains |= BACK
		}

		else if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) {
			y_intervals.push([ new vec2( this.y.a,    r.y.b ), FRONT ])
			y_intervals.push([ new vec2(    r.y.b, this.y.b ), BACK  ])
			split_contains |= FRONT
		}
	
		else if (this.y.contains(r.y.a) && this.y.contains(r.y.b)) {
			y_intervals.push([ new vec2( this.y.a,    r.y.a ), FRONT ])
			y_intervals.push([ new vec2(    r.y.a, 	  r.y.b ), 0     ])
			y_intervals.push([ new vec2(    r.y.b, this.y.b ), BACK  ])
			split_contains |= FRONT | BACK
		}

		else { // if (r.y.contains(this.y.a) && this.y.contains(r.y.b)) {
			y_intervals.push([ new vec2( this.y.a, this.y.b ), FRONT | BACK ])
			split_contains |= FRONT | BACK
		}

		x_intervals.forEach(function(x){
			y_intervals.forEach(function(y){
				var rn = new Rect(x[0], y[0])
				rn.id = rz.id
				rn.sides = ((x[1] | y[1]) & sides)
				if (r.intersects(rn)) {
					rn.sides = 0
				}
				rn.focused = rz.focused
				splits.push(rn)
			})
		})
		return splits
	}
	
	return Rect

})()