[OpenLayers-Users] Point in parrallel with line formula

Chris Adams chris at genieknows.com
Wed Apr 29 15:19:17 EDT 2009


Here's a C++ function (Should be easy to convert to JavaScript)

/* Taken from Robert Sedgewick, Algorithms in C++ */

/*  returns whether, in traveling from the first to the second
	to the third point, we turn counterclockwise (+1) or not (-1) */
 
int ccw( Point p0, Point p1, Point p2 )
{
	int dx1, dx2, dy1, dy2;
	
	dx1 = p1.x - p0.x; dy1 = p1.y - p0.y;
	dx2 = p2.x - p0.x; dy2 = p2.y - p0.y;
	
	if (dx1*dy2 > dy1*dx2)
		return +1;
	if (dx1*dy2 < dy1*dx2)
		return -1;
	if ((dx1*dx2 < 0) || (dy1*dy2 < 0))
		return -1;
	if ((dx1*dx1 + dy1*dy1) < (dx2*dx2 + dy2*dy2))
		return +1;
	return 0;
}


>From this post on GameDev.net

http://www.gamedev.net/community/forums/topic.asp?topic_id=457450

Note it will also return 0 if the point is precisely on the line. 

The deals with turn directions, so it depends on the ordering of the points on the line.

To get around this, you'll want to swap p0 and p1, if if p1 is below p0. (If p1's latitude is smaller than p0's)


newbie wrote:
> Dear All,
>            Just say I got a pair of lat and long p1 and p2 and I draw a line
> based on both the points. So now I want to know if a particular point in the
> parallel range of say 50m either on left or right sides of the line. How can
> I decide this what is the formula please ?
>   





More information about the Users mailing list