From c0fe6750cc0462adbc2165ac7f8c9cf1e0aea925 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 30 Oct 2014 17:59:44 -0400 Subject: intersection test --- public/assets/test/intersect.html | 132 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 public/assets/test/intersect.html (limited to 'public/assets/test/intersect.html') diff --git a/public/assets/test/intersect.html b/public/assets/test/intersect.html new file mode 100644 index 0000000..3f6d110 --- /dev/null +++ b/public/assets/test/intersect.html @@ -0,0 +1,132 @@ + + + + + + + + + + -- cgit v1.2.3-70-g09d2 From 946dad6823a9206d1005f14f9ab1174234682e92 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 30 Oct 2014 18:27:33 -0400 Subject: z --- public/assets/test/intersect.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public/assets/test/intersect.html') diff --git a/public/assets/test/intersect.html b/public/assets/test/intersect.html index 3f6d110..cdce645 100644 --- a/public/assets/test/intersect.html +++ b/public/assets/test/intersect.html @@ -77,7 +77,7 @@ function draw () { if (long_enough_to_intersect && collinear) { ctx.fillStyle = "#f00" } - else if (is_on_line) { + else if (collinear) { ctx.fillStyle = "#0f0" } else { -- cgit v1.2.3-70-g09d2 From 3cf89843698a52029be64e085051b19471d61174 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 30 Oct 2014 18:32:45 -0400 Subject: agh --- public/assets/test/intersect.html | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'public/assets/test/intersect.html') diff --git a/public/assets/test/intersect.html b/public/assets/test/intersect.html index cdce645..0a8526e 100644 --- a/public/assets/test/intersect.html +++ b/public/assets/test/intersect.html @@ -71,7 +71,7 @@ function draw () { intersect.a = vec_a.x.a + ( vec_a.y.a - vec_a.x.a ) * t intersect.b = vec_a.x.b + ( vec_a.y.b - vec_a.x.b ) * t - var collinear = is_collinear( intersect.b, vec_b ) + var collinear = is_collinear( intersect, vec_b ) var long_enough_to_intersect = 0 <= t && t <= 1 if (long_enough_to_intersect && collinear) { @@ -103,23 +103,23 @@ function drawPoint (p) { function perp (va, vb) { return (va.y.a - va.x.a) * (vb.y.b - vb.x.b) - (va.y.b - va.x.b) * (vb.y.a - vb.x.a) } -function is_collinear (n, vec) { +function is_collinear (p, vec) { var on_x, on_y if (vec.x.a < vec.y.a) { - on_x = vec.x.a <= n && n <= vec.y.a + on_x = vec.x.a <= p.a && p.a <= vec.y.a } else { - on_x = vec.x.a >= n && n >= vec.y.a + on_x = vec.x.a >= p.a && p.a >= vec.y.a } if (vec.x.b < vec.y.b) { - on_y = vec.x.b <= n && n <= vec.y.b + on_y = vec.x.b <= p.b && p.b <= vec.y.b } else { - on_y = vec.x.b >= n && n >= vec.y.b + on_y = vec.x.b >= p.b && p.b >= vec.y.b } - + return !! (on_x && on_y) } -- cgit v1.2.3-70-g09d2 From 0bf5e2b24d9037ef1cb4f7b435892b44d2070aac Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Thu, 30 Oct 2014 18:35:36 -0400 Subject: tab --- public/assets/test/intersect.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'public/assets/test/intersect.html') diff --git a/public/assets/test/intersect.html b/public/assets/test/intersect.html index 0a8526e..5e16f2e 100644 --- a/public/assets/test/intersect.html +++ b/public/assets/test/intersect.html @@ -101,7 +101,7 @@ function drawPoint (p) { ctx.fillRect(x, y, r*2, r*2) } function perp (va, vb) { - return (va.y.a - va.x.a) * (vb.y.b - vb.x.b) - (va.y.b - va.x.b) * (vb.y.a - vb.x.a) + return (va.y.a - va.x.a) * (vb.y.b - vb.x.b) - (va.y.b - va.x.b) * (vb.y.a - vb.x.a) } function is_collinear (p, vec) { var on_x, on_y -- cgit v1.2.3-70-g09d2 From 2c2c70965efb50b500799ae7966a42e86bbac480 Mon Sep 17 00:00:00 2001 From: Julie Lala Date: Fri, 31 Oct 2014 10:18:39 -0400 Subject: fix floating point error :-P --- public/assets/javascripts/rectangles/models/rect.js | 6 ++++++ public/assets/javascripts/rectangles/models/vec2.js | 5 ++++- public/assets/test/intersect.html | 15 +++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) (limited to 'public/assets/test/intersect.html') 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 @@ +
@@ -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("
") 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) -- cgit v1.2.3-70-g09d2