Approximate Points of Intersection of Two Line Segments

The theory for calculating the point of intersection of two line segments is similar to the theory described in the post "Approximate Point of Intersection of Two Planes" found here:!E7DBA9A4BFD458C5!473.entry

The difference is that in order to be a point of intersection between two line segments, the calculated point must be within both of the bounding boxes in which the line segments are contained in. When can thus use the following two functions to determine the poin tof intersection of two polylines.

function SimplePolylineIntersection(latlong1,latlong2,latlong3,latlong4)
//Line segment 1 (p1, p2) var A1 = latlong2.Latitude - latlong1.Latitude;
var B1 = latlong1.Longitude - latlong2.Longitude;
var C1 = A1*latlong1.Longitude + B1*latlong1.Latitude;

//Line segment 2 (p3, p4) var A2 = latlong4.Latitude - latlong3.Latitude;
var B2 = latlong3.Longitude - latlong4.Longitude;
var C2 = A2*latlong3.Longitude + B2*latlong3.Latitude;

var determinate = A1*B2 - A2*B1;

var intersection;
if(determinate != 0)
var x = (B2*C1 - B1*C2)/determinate;
var y = (A1*C2 - A2*C1)/determinate;

var intersect = new VELatLong(y,x);

if(inBoundedBox(latlong1, latlong2, intersect) &&
inBoundedBox(latlong3, latlong4, intersect))
intersection = intersect;
else intersection = null;
else //lines are parrallel intersection = null;

return intersection;

//latlong1 and latlong2 represent two coordinates that make up the bounded box //latlong3 is a point that we are checking to see is inside the box function inBoundedBox(latlong1, latlong2, latlong3)
var betweenLats;
var betweenLons;

if(latlong1.Latitude < latlong2.Latitude)
betweenLats = (latlong1.Latitude <= latlong3.Latitude &&
latlong2.Latitude >= latlong3.Latitude);
else betweenLats = (latlong1.Latitude >= latlong3.Latitude &&
latlong2.Latitude <= latlong3.Latitude);

if(latlong1.Longitude < latlong2.Longitude)
betweenLons = (latlong1.Longitude <= latlong3.Longitude &&
latlong2.Longitude >= latlong3.Longitude);
else betweenLons = (latlong1.Longitude >= latlong3.Longitude &&
latlong2.Longitude <= latlong3.Longitude);

return (betweenLats && betweenLons);

A more complex method for calculating the point of intersection of two line segments in 3D space can be found here:!E7DBA9A4BFD458C5!439.entry


2 thoughts on “Approximate Points of Intersection of Two Line Segments

  1. Hello Ricky,
    I am a user and I am attempting to get this code to work. I was wondering if you had a translation or if you could clarify what inBoundedBox function returns? As well it seems that both of the outcomes of the if statement that calls the function result in the intersection = null?


Leave a Reply

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

You are commenting using your 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