I have this code here to find out if a line is in a circle. (Maybe you can use this to base your answer on)
/**
*@param l1 Line point 1, containing latitude and longitude
*@param l2 Line point 2, containing latitude and longitude
*@param c Center of circle, containing latitude and longitud
*@param r Radius of the circle
**/
Maps.ui.inCircle = function(l1, l2, c, r){
var a = l1.lat() - l2.lat()
var b = l1.lng() - l2.lng()
var x = Math.sqrt(a*a + b*b)
return (Math.abs((c.lat() - l1.lat()) * (l2.lng() - l1.lng()) - (c.lng() - l1.lng()) * (l2.lat() - l1.lat())) / x <= r);
}
This works perfectly for that. But now I need to find out if a point is in the area around a line. For example, the blue dots in this would return true, and the purple lines I would return true. But not the green lines or dots. Also I need to find out whether a line cuts through the line.

Here is my code to see if a line intersects this line:
function getLineIntersaction(y1,x1,y2,x2, y3,x3,y4,x4){
if (Math.max(X1,X2) < Math.min(X3,X4)) // This means no same coordinates
return false;
m1 = (y1-y2)/(x1-x2);
m2 = (y3-y4)/(x3-x4);
c1 = y1-m1x1;
c2 = y3-m2x3;
if(m1=m2)//segments are parallel.
return false;
var x = (c1-c2)/(m2-m1);
if(!isNaN(x) && isFinite(x)){
if( x < Math.max(Math.min(x1,x2),math.min(x3,x4)) || x > Math.min(Math.max(x1,x2),Math.max(x3,x4)))
return false;
else
return true;
}
return false;
}
So this needs to be integrated in with the other code.
How can I do this? I could pass the function a line or I could pass it just a single point.
If a line is passed then we will run the above function. I want it to return an array. The first item in the array will return if it is near it (in the red area) and the second item in the array will return if the segment cuts the line. Meaning if it is just a point then the second item will always be false.
QUESTION
How can I tell if a line or point lays within the red area?
Quoting my answer to this question
The first step is to find the normal projection of the point onto the line. This is actually quite simple: take the distance from point 1 to the target, and point 2 to the target, and call them D1 and D2 respectively. Then calculate
D1+(D2-D1)/2. This is the distance to the projected point on the line from point 1.You can now find that point, and get the distance from that point to the target. If the distance is zero, then the target is exactly on the line. If the distance is less than 5, then the target was less than 5px away, and so on.
EDIT: A picture is worth a thousand words. Here’s a diagram:
(source: adamhaskell.net)
(In hindsight, probably should have made those circles a different colour… Also, the purple line is supposed to be perpendicular to line AB. Blame my terrible aim with the blue line!)