The intersection Point of two lines is useful to know. This is a function to find it in AS3.


//---------------------------------------------------------------
//Checks for intersection of Segment if as_seg is true.
//Checks for intersection of Line if as_seg is false.
//Return intersection of Segment "AB" and Segment "EF" as a Point
//Return null if there is no intersection
//---------------------------------------------------------------
function lineIntersectLine(A:Point,B:Point,E:Point,F:Point,as_seg:Boolean=true):Point
{
	var ip:Point;
	var a1:Number;
	var a2:Number;
	var b1:Number;
	var b2:Number;
	var c1:Number;
	var c2:Number;
 
	a1= B.y-A.y;
	b1= A.x-B.x;
	c1= B.x*A.y - A.x*B.y;
	a2= F.y-E.y;
	b2= E.x-F.x;
	c2= F.x*E.y - E.x*F.y;
 
	var denom:Number=a1*b2 - a2*b1;
	if(denom == 0){
		return null;
	}
	ip=new Point();
	ip.x=(b1*c2 - b2*c1)/denom;
	ip.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(ip,B) > Point.distance(A,B)){
			return null;
		}
		if(Point.distance(ip,A) > Point.distance(A,B)){
			return null;
		}	
 
		if(Point.distance(ip,F) > Point.distance(E,F)){
			return null;
		}
		if(Point.distance(ip,E) > Point.distance(E,F)){
			return null;
		}
	}
	return ip;
}
12 Responses to “Find Intersection Point of two lines in AS3”
  1. [...] uses the “lineIntersectLine” function of the earlier [...]

  2. Thanks Keith, for the solution. Saved my time.

  3. thanks kaith for this solution i was thinking on this thing from last few days u help me so much

  4. Thanks Keith, that is a nice one. Any thoughts on intersection of *rotated* lines? The example here considers that the planes of both the lines are same. What will happen if any one or both the lines are rotated? How to get the intersection in that case? Any help will be much appreciated. ~ Shiyaz

  5. Shiyaz,

    This link might be helpful on the subject of planes and intersections. It has C++ implementations and pseudo code to help make it easier to create an AS3 implementation or into some other language, hope it helps.

    http://softsurfer.com/Archive/algorithm_0104/algorithm_0104B.htm

  6. Thanks Keith. The links were helpful. I required the intersections of rotating planes to find the reflection of a point across a moving mirror line. However, the links threw more light on the concept. I did solve my problem by using *midpoint formula* approach. Would share the pseudo code after some cleanup. ~ Shiyaz

  7. [...] sources sinon: la fonction d’intersection de segments vient de chez Keith Hair: http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/ on doit pouvoir accélérer le test sur les segment notes [...]

  8. [...] the way: the segment intersection function comes from Keith Hair’s blog (pretty nice job ): http://keith-hair.net/blog/2008/08/04/find-intersection-point-of-two-lines-in-as3/ I think something can be done to accelerate the ‘as_seg’ section [...]

  9. Thanks a million.

  10. This was really useful, thanks a million

  11. [...] intersections that have saved me more than a few hours of headbanging. In particular, his line intersection function is a marvelous paste-and-go solution, and I really feel like posting a small optimisation that [...]

  12. Thank you!

Leave a Comment

Thanks for visiting www.keith-hair.net