# Calculating intersection between lines with GeoTools (java)

I have ArratList<Segment> (N segments - array size is N) when:

public class Segment {
Node vertex_1;
Node vertex_2;
}

public class Node {
double latitude;
double longitude;
}


And one static point - origin point. How to work with GeoTools in order to get ArrayList<boolean> (size N) where each value is true/false regarding the question:

Is the 2 lines from origin point to both edges of the segment, intersects some other segments on their way?

For example here the result is {true, false, false} because the red line from origin point to second edge of segment 1 intersects on her way segment 3.

This question is similar to Algorithm to find intersections in a polar plane on StackOverflow but the difference is that here I want to work with GeoTools instead of implementing the algorithm which involving translation of Geographic measurement units (lat/lon) into polar plane and performing some math calculation such as cross products - Not difficult but has potential for bugs, and if there is already ready open source library it's better to use it.

Because this question involving code solution, it asked also as Calculating intersection between lines with GeoTools (java) at StackOverflow.

• Please do not cross post questions (though bonus point for pointing it out in the question). Also it is not clear that you have looked at GeoTools at all to see how to apply it to your problem, if you have code then edit the question and I will reopen Commented Sep 14, 2015 at 11:36
• @iant I know GeoTools have objects like lines and points, and some functions like intersect, but I have never used this tool and struggle to find good examples how to start-the exist examples mainly concentrates on drawing maps etc. Commented Sep 14, 2015 at 11:53

Using the JTS library, you can use Coordinate (your Node) and LineString (your Segment) objects to solve the problem:

// origin point
Coordinate origin = new Coordinate(5, 0);
// segments
ArrayList<LineString> segmentList = new ArrayList();
LineString segmentA = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(0, 5), new Coordinate(5, 5)});
LineString segmentB = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(4, 3), new Coordinate(6, 3)});
LineString segmentC = new GeometryFactory().createLineString(new Coordinate[]{new Coordinate(7, 4), new Coordinate(10, 4)});
// result list
ArrayList<Boolean> resultList = new ArrayList();
for(int i = 0; i < segmentList.size(); i++){
// flag to indicate intersection
boolean intersectionResult = false;
// get current segment
LineString currentSegment = segmentList.get(i);
// get segments from segment pooints to origin
LineString startSegment = new GeometryFactory().createLineString(new Coordinate[]{origin, currentSegment.getStartPoint().getCoordinate()});
LineString endSegment = new GeometryFactory().createLineString(new Coordinate[]{origin, currentSegment.getEndPoint().getCoordinate()});
// iterate over sections
for(int j = 0; j < segmentList.size(); j++){
// ignore same section
if(i != j){
// check for intersections between segments
if(startSegment.intersects(segmentList.get(j)) || endSegment.intersects(segmentList.get(j))){
intersectionResult = true;
continue;
}
}
}
// no intersection found