Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding if line or point is near a line

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.

enter image description here

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?

like image 459
FabianCook Avatar asked Jan 27 '13 22:01

FabianCook


People also ask

How do you check if a point is above or below a line?

If the line equation is y=ax+b and the coordinates of a point is (x0,y0) then compare y0 and ax0+b, for example if y0>ax0+b then the point is above the line, etc.

How do you check if a point is on a side of a line?

Using the equation of the line ab, get the x-coordinate on the line at the same y-coordinate as the point to be sorted. If point's x > line's x, the point is to the right of the line. If point's x < line's x, the point is to the left of the line.

How do you check if two points are on the same side of a line?

You should be able to plug in the x and y values for both points, and if both make the equation y < mx + b or both make it y > mx + b, they are on the same side. If either point satisfies the equation (y = mx + b), that point is on the line.


2 Answers

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:

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!)

like image 95
Niet the Dark Absol Avatar answered Oct 21 '22 16:10

Niet the Dark Absol


You need to find the distance of a point to the line, d.

First, get the slope of a line perpendicular to the original line in question. (It's convenient to keep this as a ratio: dx,dy is the original slope, dy,-dx is the perpendicular, where dx is the difference in x's in the original line, and dy is the difference in y's of the original line.)

To test a point p1, get the intersection (p2) of the original line and the perpendicular that passes through p1. In other words, the intersection of the original line with the line p2 to (p2.x+dy, p2.y-dx)

If p2 lies between the endpoints of the original line, then the distance to the line (d) is the distance between P1 and P2.

If P2 lies outside the endpoints of the original line, then the distance to the line (d) is the shorter of the distances from P1 to the original line's endpoints.

original line: points pq1 and pq2
point to measure: p1
distance to line: d

dx = pq2.x - pq1.x
dy = pq2.y - pq1.y

p2.x = p1.x + dy // get perpendicular, arbitrary length
p2.y = p1.y - dx

px = intersection(pq1-pq2, p1-p2)

if px.x is between pq1.x and pq2.x inclusive then // check y's instead if it's near vertical
  d = distance(p1-px)
else
  d = minimum(distance(p1, pq1), distance(p1, pq2))
end if
like image 2
xpda Avatar answered Oct 21 '22 16:10

xpda