summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulie Lala <jules@okfoc.us>2014-10-31 10:18:39 -0400
committerJulie Lala <jules@okfoc.us>2014-10-31 10:18:39 -0400
commit2c2c70965efb50b500799ae7966a42e86bbac480 (patch)
tree0b61280a87b48837619a5c01bd7cd651f98d1a1d
parent0bf5e2b24d9037ef1cb4f7b435892b44d2070aac (diff)
fix floating point error :-P
-rw-r--r--public/assets/javascripts/rectangles/models/rect.js6
-rw-r--r--public/assets/javascripts/rectangles/models/vec2.js5
-rw-r--r--public/assets/test/intersect.html15
3 files changed, 21 insertions, 5 deletions
diff --git a/public/assets/javascripts/rectangles/models/rect.js b/public/assets/javascripts/rectangles/models/rect.js
index 00f2c55..c667cf5 100644
--- a/public/assets/javascripts/rectangles/models/rect.js
+++ b/public/assets/javascripts/rectangles/models/rect.js
@@ -169,6 +169,12 @@
var s = "[" + this.x.toString() + " " + this.y.toString() + "] " + sides
return s
}
+ Rect.prototype.exactString = function(){
+ var sides = sidesToString(this.sides)
+ var s = "[" + this.x.exactString() + " " + this.y.exactString() + "] " + sides
+ return s
+ }
+
Rect.prototype.serialize = function(){
return { x: this.x.serialize(), y: this.y.serialize() }
}
diff --git a/public/assets/javascripts/rectangles/models/vec2.js b/public/assets/javascripts/rectangles/models/vec2.js
index 49613c3..f28df54 100644
--- a/public/assets/javascripts/rectangles/models/vec2.js
+++ b/public/assets/javascripts/rectangles/models/vec2.js
@@ -200,7 +200,10 @@
}
vec2.prototype.toString = function(){
- return "[" + ~~this.a + " " + ~~this.b + "]"
+ return "[" + round(this.a) + " " + round(this.b) + "]"
+ }
+ vec2.prototype.exactString = function(){
+ return "[" + this.a + " " + this.b + "]"
}
vec2.prototype.serialize = function(){
return [ ~~this.a, ~~this.b ]
diff --git a/public/assets/test/intersect.html b/public/assets/test/intersect.html
index 5e16f2e..4e5b0bb 100644
--- a/public/assets/test/intersect.html
+++ b/public/assets/test/intersect.html
@@ -1,4 +1,5 @@
<canvas id="canvas"></canvas>
+<div id="hud"></div>
<script src="/assets/javascripts/util.js"></script>
<script src="/assets/javascripts/vendor/tube.js"></script>
@@ -73,16 +74,21 @@ function draw () {
var collinear = is_collinear( intersect, vec_b )
var long_enough_to_intersect = 0 <= t && t <= 1
+ var msg
if (long_enough_to_intersect && collinear) {
ctx.fillStyle = "#f00"
+ msg = "through"
}
else if (collinear) {
ctx.fillStyle = "#0f0"
+ msg = "to"
}
else {
ctx.fillStyle = "#000"
+ msg = "off"
}
+ hud.innerHTML = [intersect.exactString(), vec_b.exactString(), msg].join("<br>")
drawPoint(intersect)
}
@@ -105,19 +111,20 @@ function perp (va, vb) {
}
function is_collinear (p, vec) {
var on_x, on_y
+ var pa = round(p.a), pb = round(p.b)
if (vec.x.a < vec.y.a) {
- on_x = vec.x.a <= p.a && p.a <= vec.y.a
+ on_x = vec.x.a <= pa && pa <= vec.y.a
}
else {
- on_x = vec.x.a >= p.a && p.a >= vec.y.a
+ on_x = vec.x.a >= pa && pa >= vec.y.a
}
if (vec.x.b < vec.y.b) {
- on_y = vec.x.b <= p.b && p.b <= vec.y.b
+ on_y = vec.x.b <= pb && pb <= vec.y.b
}
else {
- on_y = vec.x.b >= p.b && p.b >= vec.y.b
+ on_y = vec.x.b >= pb && pb >= vec.y.b
}
return !! (on_x && on_y)