|
21 | 21 | import mil.nga.sf.Geometry; |
22 | 22 | import mil.nga.sf.GeometryCollection; |
23 | 23 | import mil.nga.sf.GeometryType; |
| 24 | +import mil.nga.sf.Line; |
24 | 25 | import mil.nga.sf.LineString; |
25 | 26 | import mil.nga.sf.MultiLineString; |
26 | 27 | import mil.nga.sf.MultiPoint; |
@@ -1140,6 +1141,59 @@ private static boolean pointOnPath(Point point, List<Point> points, |
1140 | 1141 | return onPath; |
1141 | 1142 | } |
1142 | 1143 |
|
| 1144 | + /** |
| 1145 | + * Get the point intersection between two lines |
| 1146 | + * |
| 1147 | + * @param line1 |
| 1148 | + * first line |
| 1149 | + * @param line2 |
| 1150 | + * second line |
| 1151 | + * @return intersection point or null if no intersection |
| 1152 | + * @since 2.0.7 |
| 1153 | + */ |
| 1154 | + public static Point intersection(Line line1, Line line2) { |
| 1155 | + return intersection(line1.startPoint(), line1.endPoint(), |
| 1156 | + line2.startPoint(), line2.endPoint()); |
| 1157 | + } |
| 1158 | + |
| 1159 | + /** |
| 1160 | + * Get the point intersection between end points of two lines |
| 1161 | + * |
| 1162 | + * @param line1Point1 |
| 1163 | + * first point of the first line |
| 1164 | + * @param line1Point2 |
| 1165 | + * second point of the first line |
| 1166 | + * @param line2Point1 |
| 1167 | + * first point of the second line |
| 1168 | + * @param line2Point2 |
| 1169 | + * second point of the second line |
| 1170 | + * @return intersection point or null if no intersection |
| 1171 | + * @since 2.0.7 |
| 1172 | + */ |
| 1173 | + public static Point intersection(Point line1Point1, Point line1Point2, |
| 1174 | + Point line2Point1, Point line2Point2) { |
| 1175 | + |
| 1176 | + Point intersection = null; |
| 1177 | + |
| 1178 | + double a1 = line1Point2.getY() - line1Point1.getY(); |
| 1179 | + double b1 = line1Point1.getX() - line1Point2.getX(); |
| 1180 | + double c1 = a1 * (line1Point1.getX()) + b1 * (line1Point1.getY()); |
| 1181 | + |
| 1182 | + double a2 = line2Point2.getY() - line2Point1.getY(); |
| 1183 | + double b2 = line2Point1.getX() - line2Point2.getX(); |
| 1184 | + double c2 = a2 * (line2Point1.getX()) + b2 * (line2Point1.getY()); |
| 1185 | + |
| 1186 | + double determinant = a1 * b2 - a2 * b1; |
| 1187 | + |
| 1188 | + if (determinant != 0) { |
| 1189 | + double x = (b2 * c1 - b1 * c2) / determinant; |
| 1190 | + double y = (a1 * c2 - a2 * c1) / determinant; |
| 1191 | + intersection = new Point(x, y); |
| 1192 | + } |
| 1193 | + |
| 1194 | + return intersection; |
| 1195 | + } |
| 1196 | + |
1143 | 1197 | /** |
1144 | 1198 | * Determine if the geometries contain a Z value |
1145 | 1199 | * |
|
0 commit comments