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 "";
}
 
Vote in HexoSearch
2 Responses to “Know where a Point is relevant to a Segment”
  1. Nice one. I already reformed and used the intersect functions (circle on line, line on line) and will use this one too if you don’t mind.

    I was wondering where you find those functions. Do you make them yourself or do you check some math examples.

  2. Frank,
    I came up with this myself. Your Left and and Right ears allow you to know where a sound is located in relation to your position.
    Both your ears compare distance and angle of the sound to know if the sound is behind, front, left, right (or top and bottom) of the center point between them.
    …well that’s how I think it works. Do a web search on how an Owl hears and locate objects in space, that’s even more interesting.

    As for the circle and line intersection functions, there’s plenty of information online in various programming languages that where easy enough to translate into an AS3 one. After figuring out one thing figuring out other problems become clearer.

Leave a Comment

Thanks for visiting www.keith-hair.net