What is the best way to find which pair of adjacent points in an arraylist a single point X is closest to in Java? -
I have 2D points in an array list (i.e. A, B, C, D), this includes an arbitrary number Which may store a line string together.
Suppose that I had a point X falling between point B and C, how can I ensure that the output of the function is an array list with an "array" (or just point A Or point B is specified for "B" and "C") given the following two cases:
1-Point is close to B in comparison to X, but Yet between the B and C, the 2-point is closer to C than the X, but still It falls between V and B3 - Point is close to C compared to X (maybe because the line segment is arrayed like a string or something like the array), but the function should be returned Either point C and D or C and B, which is based on the nearest of pointX.
My current code is as follows, let's say that pointX goes into a function that does the following:
point-point A = faucet; Point point B = null; Int minDistance = Integer.MAX_VALUE; (Int i = 0; i & lt; pointlist.size (); i ++) {int distance = pointlist.get (i) .getDistanceToPoint (xPoint); If (distance & lt; minDistance) {if (pointB! = Null) {pointB = pointA; } MinDistance = Distance; PointA = pointlist.get (i); } And {pointB = pointA; PointA = pointlist.get (i); }}
Found above case # 1, but not the case # 2. What is the best way to do this that PointA equals "B", and is equal to point B "C"? If there is a better way of doing this, then I am open to it.
Is it just two points that are close to the point? It was just another and the other closest point, if the array of points was made to shape U shape or some shape, then it was the closest to two digits, but it is not necessary that they are adjacent to each other, if the function Returned those two points which is wrong, instead it should be withdrawn to the nearest first point, then the second point that is near that point. Unknowingly The user may have specified to start with the point "4" or a specific index, such that if 6th point was the second nearest point, then the result Returns the 6th point with 5th or 7th, depending on the basis of that line segment.
Congratulations on comment thread after 14 It has come to know that in fact a set of connected line segments Some point is to find line segments near the X. The set is stored only as the top coordinate, but it is essentially irrelevant.
I am going to use the segment
because: Java, and also because if you want to solve a problem uses datastrok to match your problem Want to find a line? Use a line class: Therefore, let's use a dedicated class, in which to represent a line segment instead of an ArrayList with 2 digits:
class segment {point p1, P2; Public Section (Point A, Point B) {p1 = a; P2 = b; }} ArrayList & lt; Segment & gt; Segment = new arreelist & lt; Segment & gt; (); (Int i = 0, final = number. Size () - 1; i last; i ++) {segment.ed (new segment (numberGate (i), number.get (i + 1 ))}} <, Then the nearest segment: Segment searchclose segments (Arrayist & LeTtonum; Segment> Segment, point goal) {Double dist, minidist = double.mx_avar; Segment And lieutenant = s;}} minDistSegment; (Segments: Segments) {dist = s.distanceTo (target); if (dist & lt; minDist) {minDist = dist; minDistSegment = s;}} return minDistSegment;}
Now let us simply Segment distance to point
, for which ( a ) requires linear algebra to be applied, and then ( b ) to ensure that the launch is located within the starting / end point of the segment.
There are actually many sites for which you have such a code, so I am not including it here I am (very far in every conceivable language), but really a specific Ain sole right metric to determine the proximity of a point to Segment the NT.
Comments
Post a Comment