Angle between two connected polylines

Theory

Often it will be useful to know the angle between to polylines. By using the connecting point as our origin and calculating the bearings of the two polylines we can calculate the difference in the bearings to find out the angle between the polylines. There are two angles that we can calculate, the inner angle and the outer angle. The inner angle is less than 180˚.

clip_image002

Figure 1 Angle between polylines

We can calculate the angle using the following formula:

clip_image004

Application

A line segment consists to two end points. Since we have two line segments that are connected we have three points. Since there are two possible angles that can be calculated we will make the function so that the type of angle to be returned can be specified. The two angle types that can be used in this function is “inner” and “outer”. This function will return a number between 0 and 360 which will be the angle between the two polylines.

function angleBetweenPoints(latlongA,latlongB,latlongC,angleType)
{
var headingBA = calculateBearing(latlongB,latlongA);
var headingBC = calculateBearing(latlongB,latlongC);

return angleBetweenHeadings(headingBA,headingBC,angleType);
}

Listing 1 Calculate angle between three points

We’ve broken the function to calculate the angle between three points up into two parts. By doing this we increase the usability of our code. This second function allows us to calculate the angle between two headings. This function will take in two headings between 0 and 360 degrees and will take in the type of angle that should be returned. A number representing the angle in degrees will be returned,

function angleBetweenHeadings(headingBA,headingBC,angleType)
{
var angle = ((headingBA-headingBC)+360)%360;

if(angleType=="inner")
if(angle>180)
return 360-angle;
else return angle;

if(angleType=="outer")
if(angle<180)
return 360-angle;
else return angle;
}

Listing 2 Angle between two headings

Information on the calculateBearing method can be found here: http://rbrundritt.spaces.live.com/blog/cns!E7DBA9A4BFD458C5!393.entry

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s