Przeglądaj źródła

lineIntersection() now returns null if intersect point is not on segment.
use project() if projected point needed.

bstouls 8 lat temu
rodzic
commit
2b440dfbcb
1 zmienionych plików z 4 dodań i 5 usunięć
  1. 4 5
      h2d/col/Segment.hx

+ 4 - 5
h2d/col/Segment.hx

@@ -63,15 +63,14 @@ class Segment {
 		}
 	}
 
-	public inline function lineIntersection( r : h2d.col.Ray, ?pt : Point, projectPoint = false ) {
-		if( !projectPoint && r.side(new Point(x, y)) * r.side(new Point(x + dx, y + dy)) > 0)
+	public inline function lineIntersection( r : h2d.col.Ray, ?pt : Point ) {
+		if( r.side(new Point(x, y)) * r.side(new Point(x + dx, y + dy)) > 0 )
 			return null;
 
 		var u = ( r.dx * (y - r.y) - r.dy * (x - r.x) ) / ( r.dy * dx - r.dx * dy );
+		if( u < 0 || u > 1 ) return null;
+
 		if( pt == null ) pt = new Point();
-		if( projectPoint ) {
-			if( u < 0 ) u = 0 else if( u > 1 ) u = 1;
-		}
 		pt.x = x + u * dx;
 		pt.y = y + u * dy;