ok, maybe I do something wrong…
[trace] Segments intersection test
[trace] (x=465.16666666666663, y=222)
[trace] (x=611, y=66.6)
[trace] (x=250, y=222)
[trace] (x=472, y=222)
[trace] http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/
[trace] result = null
[trace] vs
[trace] https://www.kirupa.com/forum/showthread.php?361144-line-segment-intersection&p=2599918#post2599918
[trace] result = (x=465.16666666666663, y=222)
[trace] test end
[trace] Segments intersection test
[trace] (x=222, y=481.81570788839144)
[trace] (x=361, y=333)
[trace] (x=222, y=444)
[trace] (x=222, y=666)
[trace] http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/
[trace] result = null
[trace] vs
[trace] https://www.kirupa.com/forum/showthread.php?361144-line-segment-intersection&p=2599918#post2599918
[trace] result = (x=222, y=481.81570788839144)
[trace] test end
here is the full test case:
[details=Summary] public static function startTest():void
{
segmentsIntersectionTest(new Point(465.16666666666663, 222), new Point(611, 66.6), new Point(250, 222), new Point(472, 222));
segmentsIntersectionTest(new Point(222, 481.81570788839144), new Point(361, 333), new Point(222, 444), new Point(222, 666));
}
public static function segmentsIntersectionTest(p1:Point, p2:Point, p3:Point, p4:Point):void
{
trace("Segments intersection test");
trace(p1);
trace(p2);
trace(p3);
trace(p4);
trace("http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/");
trace("result = " + segmentsIntersectionKeith(p1, p2, p3, p4));
trace("vs");
trace("https://www.kirupa.com/forum/showthread.php?361144-line-segment-intersection&p=2599918#post2599918");
trace("result = " + segmentsIntersectionNew(p1, p2, p3, p4));
trace("test end");
}
public static function segmentsIntersectionKeith(p1:Point, p2:Point, p3:Point, p4:Point):Point
{
var result:Point;
var a1:Number;
var a2:Number;
var b1:Number;
var b2:Number;
var c1:Number;
var c2:Number;
a1 = p2.y-p1.y;
b1 = p1.x-p2.x;
c1 = p2.x*p1.y - p1.x*p2.y;
a2 = p4.y-p3.y;
b2 = p3.x-p4.x;
c2 = p4.x*p3.y - p3.x*p4.y;
var denom:Number = a1*b2 - a2*b1;
if(denom == 0)
return null;
result = new Point();
result.x = (b1*c2 - b2*c1) / denom;
result.y = (a2*c1 - a1*c2) / denom;
//---------------------------------------------------
//Do checks to see if intersection to endpoints
//distance is longer than actual Segments.
//Return null if it is with any.
//---------------------------------------------------
//if(as_seg){
if(Point.distance(result, p2) > Point.distance(p1, p2))
return null;
if(Point.distance(result, p1) > Point.distance(p1, p2))
return null;
if(Point.distance(result, p4) > Point.distance(p3, p4))
return null;
if(Point.distance(result, p3) > Point.distance(p3, p4))
return null;
//}
return result;
}
public static function segmentsIntersectionNew(p1:Point, p2:Point, p3:Point, p4:Point):Point
{
var v1:Point=new Point();
var v2:Point=new Point();
var intersectPoint:Point= new Point();
var d:Number;
v1.x = p2.x - p1.x;
v1.y = p2.y - p1.y;
v2.x = p4.x - p3.x;
v2.y = p4.y - p3.y;
d = v1.x * v2.y - v1.y * v2.x;
if (! d)
{
//points are collinear
return null;
}
var a:Number = p3.x - p1.x;
var b:Number = p3.y - p1.y;
var t:Number = (a * v2.y - b * v2.x) / d;
var s:Number = (b * v1.x - a * v1.y) / -d;
if (t < 0 || t > 1 || s < 0 || s > 1)
{
//line segments don't intersect
return null;
}
intersectPoint.x = p1.x + v1.x * t;
intersectPoint.y = p1.y + v1.y * t;
return intersectPoint;
}[/details]