Looking at my previous post I felt the isBehindLine function served it's purpose, but if I get a more complicated problem similar to a raycasting test with imaginary ray of arbitrary width I would need to know more information where a Point is relevant to a Segment.

The "whereAboutSegment" function will return a String this time, with more detailed info of the Point's where-a-abouts instead of just knowing if its behind a line.

 
/*-----------------------------------------------------------------
Returns a String describing where Point P is relevant to line CD:
"left","right","front" or "behind"
If Point P is a Point on line CD, an empty String is returned.
 ----------------------------------------------------------------*/
function whereAboutSegment(P:Point,C:Point,D:Point):String
{
	var F:Point;
	var G:Point;
	var s:Boolean;
	var l:Boolean;
	var r:Boolean;
	var f:Boolean;
	var a:Number=Point.distance(P,C.add(Point.polar(1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI / 2)));
	var b:Number=Point.distance(P,C.add(Point.polar(-1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI / 2)));
	s = a == b;
	if (a < b) {
		f=true;
	}
	F=C.add(Point.polar(1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI));
	G=C.add(Point.polar(-1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI));
	a=Point.distance(P,F.add(Point.polar(1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI)));
	b=Point.distance(P,G.add(Point.polar(-1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI)));
	if (a < b) {
		r=true;
	}
	F=D.add(Point.polar(1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI));
	G=D.add(Point.polar(-1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI));
	a=Point.distance(P,F.add(Point.polar(1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI )));
	b=Point.distance(P,G.add(Point.polar(-1,Math.atan2(C.y - D.y,C.x - D.x) + Math.PI)));
	if (a > b) {
		l=true;
	}
	if (s && l && r) {
		return "";
	}
	if (!f && l && r) {
		return "behind";
	}
	if (f && l && r) {
		return "front";
	}
	if (l) {
		return "left";
	}
	if (r) {
		return "right";
	}
	return "";
}
 
Leave a Comment

Thanks for visiting www.keith-hair.net