summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2014-07-22 16:23:57 -0400
committerJules Laplace <jules@okfoc.us>2014-07-22 16:23:57 -0400
commit725cc3cd23890d5369ae20c27c97465c34168913 (patch)
treea2e398ab6694ad53a9d5d03decfe41203f9badca
parente2b1d4ca8c7a83b41fe4c0fc6c4d9490a6736c68 (diff)
starting math tests
-rw-r--r--package.json3
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js40
-rw-r--r--public/assets/javascripts/rectangles/models/vec2.js19
-rw-r--r--test/test-rect.js63
-rw-r--r--test/test-vec2.js48
-rw-r--r--views/partials/scripts.ejs8
6 files changed, 168 insertions, 13 deletions
diff --git a/package.json b/package.json
index ca45bff..b4ac400 100644
--- a/package.json
+++ b/package.json
@@ -39,6 +39,7 @@
"grunt-contrib-watch": "~0.5.3",
"grunt-contrib-clean": "~0.5.0",
"grunt-contrib-copy": "~0.5.0",
- "grunt-dentist": "~0.3.4"
+ "grunt-dentist": "~0.3.4",
+ "mocha": "~1.20.1"
}
}
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index 315adef..f91e759 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -1,5 +1,25 @@
(function(){
+ var vec2
+ if ('window' in this) {
+ vec2 = window.vec2
+ }
+ else {
+ vec2 = require('./vec2')
+ FRONT = 0x1, BACK = 0x2, LEFT = 0x4, RIGHT = 0x8, FLOOR = 0x10, CEILING = 0x20
+ TOP = CEILING, BOTTOM = FLOOR
+ 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 "
+ if (sides & TOP) s += "top "
+ if (sides & BOTTOM) s += "bottom "
+ return s
+ }
+ }
+
var Rect = function (x0,y0,x1,y1){
if (x0 instanceof vec2) {
this.x = x0
@@ -77,6 +97,12 @@
Rect.prototype.intersects = function(r){
return this.x.intersects(r.x) && this.y.intersects(r.y)
}
+ Rect.prototype.adjacent = function(r){
+ return this.x.adjacent(r.x) && this.y.adjacent(r.y)
+ }
+ Rect.prototype.eq = function(r){
+ return this.x.eq(r.x) && this.y.eq(r.y)
+ }
Rect.prototype.nearEdge = function (x, y, r) {
var edges = 0
if (x < this.x.a+r) {
@@ -173,6 +199,18 @@
if (r.intersects(rn)) {
rn.sides = 0
}
+ // if (r.x.b == rn.x.a) {
+// rn.sides &= ~LEFT
+// }
+// if (rn.x.b == r.x.a) {
+// rn.sides &= ~RIGHT
+// }
+// if (r.y.b == rn.y.a) {
+// rn.sides &= ~FRONT
+// }
+// if (rn.y.b == r.y.a) {
+// rn.sides &= ~BACK
+// }
rn.focused = rz.focused
splits.push(rn)
})
@@ -183,7 +221,7 @@
if ('window' in this) {
window.Rect = Rect
}
- else if ('module' in this) {
+ else {
module.exports = Rect
}
diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js
index 9233aec..9c6fd99 100644
--- a/public/assets/javascripts/rectangles/models/vec2.js
+++ b/public/assets/javascripts/rectangles/models/vec2.js
@@ -80,16 +80,22 @@
return clamp(n, this.a+r, this.b-r)
}
vec2.prototype.intersects = function(v){
- if (this.a < v.a) {
- return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)
- }
- else if (this.a == v.a) {
+ if (this.a == v.a) { // || this.b == v.b || this.a == v.b || this.b == v.a) {
return true
}
+ else if (this.a < v.a) {
+ return (v.a < this.b && this.b <= v.b) || (this.a < v.b && v.b <= this.b)
+ }
else if (this.a > v.a) {
return (this.a < v.b && v.b <= this.b) || (v.a < this.b && this.b <= v.b)
}
}
+ vec2.prototype.adjacent = function(v){
+ if (this.a == v.a || this.b == v.b || this.a == v.b || this.b == v.a) {
+ return true
+ }
+ return false
+ }
vec2.prototype.union = function(v){
if (this.intersects(v)) {
return new vec2( min(this.a,v.a), max(this.b, v.b) )
@@ -115,8 +121,7 @@
if ('window' in this) {
window.vec2 = vec2
}
- else if ('module' in this) {
+ else {
module.exports = vec2
}
-
-})() \ No newline at end of file
+})()
diff --git a/test/test-rect.js b/test/test-rect.js
new file mode 100644
index 0000000..7dd7ae6
--- /dev/null
+++ b/test/test-rect.js
@@ -0,0 +1,63 @@
+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) ));
+ })
+ */
+
+ })
+ describe('#split()', function(){
+ var rect = new Rect( 0, 0, 10, 10)
+ var east = new Rect( 5, 0, 15, 10)
+ var east_in = new Rect( 5, 0, 10, 10)
+ var east_edge = new Rect(10, 0, 20, 10)
+ var south = new Rect( 0, 5, 10, 15)
+ var south_in = new Rect( 0, 5, 10, 10)
+ var south_edge = new Rect( 0, 10, 10, 15)
+ var corner = new Rect( 5, 5, 15, 15)
+
+ it('splits east', function(){
+ var splits = rect.split(east)
+ var splits2 = east.split(rect)
+ console.log("\n")
+ console.log(rect+"")
+ console.log(splits.map(function(r){ return r.toString() }))
+ })
+ })
+})
diff --git a/test/test-vec2.js b/test/test-vec2.js
new file mode 100644
index 0000000..054d37b
--- /dev/null
+++ b/test/test-vec2.js
@@ -0,0 +1,48 @@
+var assert = require("assert")
+var vec2 = require("../public/assets/javascripts/rectangles/models/vec2.js")
+
+describe('vec2', function(){
+ describe('#intersects()', function(){
+ var vec = new vec2(0, 10)
+
+ it('intersects itself', function(){
+ assert.equal(true, vec.intersects( new vec2(0, 10) ));
+ })
+ it('intersects w/ same startpoint (shorter)', function(){
+ assert.equal(true, vec.intersects( new vec2(0, 5) ));
+ })
+ it('intersects w/ same startpoint (longer)', function(){
+ assert.equal(true, vec.intersects( new vec2(0, 15) ));
+ })
+ it('intersects w/ same endpoint (shorter)', function(){
+ assert.equal(true, vec.intersects( new vec2(5, 10) ));
+ })
+ it('intersects w/ same endpoint (longer)', function(){
+ assert.equal(true, vec.intersects( new vec2(-5, 10) ));
+ })
+ it('intersects when contained', function(){
+ assert.equal(true, vec.intersects( new vec2(-5, 15) ));
+ })
+ it('does not intersect when before', function(){
+ assert.equal(false, vec.intersects( new vec2(-10, -5) ));
+ })
+ it('does not intersect when after', function(){
+ assert.equal(false, vec.intersects( new vec2(15, 20) ));
+ })
+ it('contains itself', function(){
+ assert.equal(true, vec.contains( 0 ));
+ assert.equal(true, vec.contains( 5 ));
+ assert.equal(true, vec.contains( 10 ));
+ })
+ it('does not contain before or after', function(){
+ assert.equal(false, vec.contains( -5 ));
+ assert.equal(false, vec.contains( 15 ));
+ })
+ it('intersects when only startpoint matches', function(){
+ assert.equal(true, vec.intersects( new vec2(-5, 0) ));
+ })
+ it('intersects when only endpoint matches', function(){
+ assert.equal(true, vec.intersects( new vec2(10, 15) ));
+ })
+ })
+})
diff --git a/views/partials/scripts.ejs b/views/partials/scripts.ejs
index 0dea452..1bf1f98 100644
--- a/views/partials/scripts.ejs
+++ b/views/partials/scripts.ejs
@@ -19,13 +19,13 @@
<script type="text/javascript" src="/assets/javascripts/rectangles/util/wheel.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/util/mouse.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/util/keys.js"></script>
-<script type="text/javascript" src="/assets/javascripts/rectangles/models/room.js"></script>
-<script type="text/javascript" src="/assets/javascripts/rectangles/models/wall.js"></script>
-<script type="text/javascript" src="/assets/javascripts/rectangles/models/tree.js"></script>
-<script type="text/javascript" src="/assets/javascripts/rectangles/models/rect.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/vec2.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/vec3.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/models/mat4.js"></script>
+<script type="text/javascript" src="/assets/javascripts/rectangles/models/rect.js"></script>
+<script type="text/javascript" src="/assets/javascripts/rectangles/models/tree.js"></script>
+<script type="text/javascript" src="/assets/javascripts/rectangles/models/room.js"></script>
+<script type="text/javascript" src="/assets/javascripts/rectangles/models/wall.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/engine/rooms/_rooms.js"></script>
<script type="text/javascript" src="/assets/javascripts/rectangles/engine/rooms/builder.js"></script>