0

I am wondering how you would write a java method finding the closest Double to a given list of Segments. I can't figure out how to move on...Any suggestion or code demonstration will be very helpful.

For example: Let's say I have a line segment -> [x1,x2]. What I need is to return X if it lies on this line segment. Otherwise, return x1 or x2 if the given value, in this case "X" is the nearest value to x1 or to x2. Example: [2.1, 4.2]. x=1. The result should be 2.1 because is closer to 1. Thanks in advance. Here is my code:

public class LineSegment {

private Point p1;
private Point p2;

public Point getP1() {
    return p1;
}

public Point getP2() {
    return p2;
}

public LineSegment(Point p1, Point p2) {
    this.p1 = p1;
    this.p2 = p2;
}

// Find X or the closest value to X
private Double findNearestX(Double x, List<LineSegment> lSeg) {
    Double max = Double.MAX_VALUE;
    double closest;

    // Get X in case it's inside the list
    for (LineSegment ls : lSeg)
        if (lSeg.contains(x))
            return x;

    // otherwise...what to do next!

    return null;
}

}

Capfer
  • 829
  • 9
  • 22
  • How do you define "closest double?" – markspace Apr 26 '17 at 20:36
  • I didn't mean "what does your code do right now," I meant mathematically. I can't help with your algorithm unless I know what sort of "closest" match you want (in my mind, there could be several). Try to think about the problem abstractly instead of in code. – markspace Apr 26 '17 at 20:52
  • OK, and is that what `contains()` above should return? You should be able to at least write that method, I think. Add the explanation you just gave in your comment to the question, it will help others. – markspace Apr 26 '17 at 21:25
  • markspace, please help if with the algoritm. I really can't figure out how to do that. Two weeks already thinking nothing done! – Capfer Apr 26 '17 at 21:33
  • Please edit your question. I seriously still not sure; I think you'll have better luck with all the info and examples in your question. – markspace Apr 26 '17 at 21:48
  • I've edited my code. Is there any suggestion? Please, I really need a code demonstration or algorithm. – Capfer Apr 26 '17 at 22:42
  • Could you add a few more examples of input and expected output? – Simon Apr 26 '17 at 22:50
  • Simon, Here is another example: line segments [0.1, 0.3], Let say X=0.5. In this case the nearest/closest value to X is 0.3. Another example: line segment [3, 9], X=6. The result is 9 because it's the closest value to 6. – Capfer Apr 26 '17 at 23:26
  • If the "closest" is defined as the subtraction (absolute value) of the search and the values in the segments just compare the values and return the cell with the minimum difference. Also you'll need to define how to compare a line segment to a double (are you comparing the two points and what is in a Point? – twain249 Apr 27 '17 at 00:58
  • twain249, My Point class contains one field, private double x with one parameter constructor public Point(double x){this.x = x; } and getters and setters. – Capfer Apr 27 '17 at 08:04

1 Answers1

0

Seems you are considering 1D problem and your Point is just one coordinate.

Make a list (or array) A[] of segment ends together with start/end attribute (Coord, IsStart)

Sort it by Coord field

Using binary search (adapted for inexact coincidence like Delphi version here), find position of X in this list, get two neigbour indexes, choose the closest by coordinate. Result - iBest index

If X is inside segment wit closest end, return X, else end coordinate:

 if (A[iBest].IsStart and X >= A[iBest].Coord) or 
    ((not A[iBest].IsStart) and X <= A[iBest].Coord))
        return X
 else
        return A[iBest].Coord)
Community
  • 1
  • 1
MBo
  • 77,366
  • 5
  • 53
  • 86