Search for a line or point next to a line

I have this code here to find out if there is a line in a circle. (Perhaps you can use this to answer your question)

/** *@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 great for this. But now I need to find out if the point is in the area around the line. For example, the blue dots in this will return true, and the purple lines I will return. But not green lines or dots. I also need to find out if the line passes through the line.

enter image description here

Here is my code to see if a line crosses 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 with other code.

How can i do this? I could pass the line function, or I could pass only one point to it.

If the string is passed, we will run the above function. I want it to return an array. The first element of the array will return if it is next to it (in the red area), and the second element in the array will return if the segment cuts the line. Meaning, if it is just a point, then the second element will always be false.

Question

How to determine if a line or point is in the red area?

+7
source share
2 answers

Quoting my answer to this question

The first step is to find the normal projection of the point on the line. This is actually quite simple: take the distance from point 1 to the target and from point 2 to the target and name 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.

Now you can find this point and get the distance from this point to the target. If the distance is zero, then the target is exactly on the line. If the distance is less than 5, the target was less than 5 pixels away, etc.

UPDATE: a picture is worth a thousand words. Here is the diagram:

Diagram
(source: adamhaskell.net )

(Looking back, you should probably make these circles a different color ... Also, the purple line should be perpendicular to the AB line. Blame my terrible target for the blue line!)

+7
source

You need to find the distance to the point, d.

First, take the slope of the line perpendicular to the source line. (It’s convenient to keep this as a relation: dx,dy is the original slope, dy,-dx is the perpendicular, where dx is the difference x in the original row, and dy is the difference y in the original row.)

To check the point p1, we get the intersection (p2) of the original line and the perpendicular passing 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 end points of the original line, then the distance to the line (d) is the distance between P1 and P2.

If P2 lies outside the end points of the original line, then the distance to the line (d) is a short distance from P1 to the original end points.

 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 instead if it near vertical d = distance(p1-px) else d = minimum(distance(p1, pq1), distance(p1, pq2)) end if 
+2
source

All Articles