So I have three classes. On the one hand there is the Point class:
public class Point {
public float x;
public float y;
// Every point has a list of connections to other points indicated
// by their position in the list of points in the main class.
public List<Integer> connections = new ArrayList<>();
public Point(float x, float y) {
this.x = x;
this.y = y;
}
}
On the other hand there is the Line class:
public class Line {
public Point start;
public Point end;
public Line(Point start, Point end) {
this.start = start;
this.end = end;
}
}
And the Combination class:
public class Combination {
public Line a;
public Line b;
public Combination(Line a, Line b) {
this.a = a;
this.b = b;
}
}
I want one method to return a list of all possible combinations based on lines which are generated from the given points. My current approach is the following:
public static List<Combination> findCombinations(List<Point> points) {
List<Combination> combinations = new ArrayList<>();
List<Line> lines = new ArrayList<>();
for (Point a : points) {
for (int p : a.connections) {
lines.add(new Line(a, points.get(p)));
}
}
for (Line a : lines) {
for (Line b : lines) {
if (a == b) continue;
combinations.add(new Combination(a, b));
}
}
return combinations;
}
I have a base class containing a list of points. For every instance of that class I call the method above once only. However, a lot of instances are created and this method slows down the process.
I need to find the combinations in order to test if there is a intersection for the two lines (segments) in each Combination
instance. I basically want to find all combinations of lines that intersect. To achieve this I need to get the combinations and then check where the intersection is.
The calculation of the intersecting point is not the problem.