Skip to content

Commit 37dcfce

Browse files
author
Wouter Commandeur
committed
fix checking of segment intersection no more rounding needed.
1 parent 8436bfe commit 37dcfce

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

src/geom/Line.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -375,20 +375,17 @@ Phaser.Line.intersectsPoints = function (a, b, e, f, asSegment, result) {
375375
return null;
376376
}
377377

378-
/*
379-
Round to 3 decimals here, due to javascript floating point is 'broken'
380-
http://stackoverflow.com/questions/11832914/round-to-at-most-2-decimal-places-in-javascript
381-
See workaround explanation there in accepted answer there..
382-
*/
383-
result.x = Math.round( ((((b1 * c2) - (b2 * c1)) / denom)+0.00001)*1000 ) / 1000;
384-
result.y = Math.round( ((((a2 * c1) - (a1 * c2)) / denom)+0.00001)*1000 ) / 1000;
378+
result.x = ((b1 * c2) - (b2 * c1)) / denom;
379+
result.y = ((a2 * c1) - (a1 * c2)) / denom;
385380

386381
if (asSegment)
387382
{
388-
if ( result.x < Math.min(a.x, b.x) || result.x > Math.max(a.x, b.x) ||
389-
result.y < Math.min(a.y, b.y) || result.y > Math.max(a.y, b.y) ||
390-
result.x < Math.min(e.x, f.x) || result.x > Math.max(e.x, f.x) ||
391-
result.y < Math.min(e.y, f.y) || result.y > Math.max(e.y, f.y) ) {
383+
var u_b = ((f.y-e.y)*(b.x-a.x) - (f.x-e.x)*(b.y- a.y));
384+
var ua = (((f.x-e.x)*(a.y-e.y)) - (f.y-e.y)*(a.x-e.x)) / u_b;
385+
var ub = (((b.x- a.x)*(a.y- e.y)) - ((b.y-a.y)*(a.x- e.x))) / u_b;
386+
if (ua >=0 && ua<=1 && ub >=0 && ub <=1) {
387+
return result;
388+
} else {
392389
return null;
393390
}
394391
}

0 commit comments

Comments
 (0)