Skip to content

Commit e4153b4

Browse files
committed
line intersection utility and envelope methods
1 parent 0e41790 commit e4153b4

3 files changed

Lines changed: 101 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ Adheres to [Semantic Versioning](http://semver.org/).
66

77
## 2.0.7 (TBD)
88

9-
* TBD
9+
* Line intersection geometry utility
10+
* GeometryEnvelope is empty and contains methods
1011

1112
## [2.0.6](https://github.com/ngageoint/simple-features-java/releases/tag/2.0.6) (04-18-2022)
1213

src/main/java/mil/nga/sf/GeometryEnvelope.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
package mil.nga.sf;
22

3+
import java.io.Serializable;
4+
35
import mil.nga.sf.util.GeometryEnvelopeBuilder;
46

57
/**
68
* Geometry envelope
79
*
810
* @author osbornb
911
*/
10-
public class GeometryEnvelope {
12+
public class GeometryEnvelope implements Serializable {
13+
14+
/**
15+
* Serial Version UID
16+
*/
17+
private static final long serialVersionUID = 1L;
1118

1219
/**
1320
* Min X
@@ -467,6 +474,16 @@ public Point getCentroid() {
467474
return new Point((minX + maxX) / 2.0, (minY + maxY) / 2.0);
468475
}
469476

477+
/**
478+
* Determine if the envelope is empty
479+
*
480+
* @return true if empty
481+
* @since 2.0.7
482+
*/
483+
public boolean isEmpty() {
484+
return getXRange() <= 0.0 || getYRange() <= 0.0;
485+
}
486+
470487
/**
471488
* Determine if intersects with the provided envelope
472489
*
@@ -556,6 +573,33 @@ public GeometryEnvelope union(GeometryEnvelope envelope) {
556573
return union;
557574
}
558575

576+
/**
577+
* Determine if contains the point
578+
*
579+
* @param point
580+
* point
581+
* @return true if contains
582+
* @since 2.0.7
583+
*/
584+
public boolean contains(Point point) {
585+
return contains(point.getX(), point.getY());
586+
}
587+
588+
/**
589+
* Determine if contains the coordinate
590+
*
591+
* @param x
592+
* x value
593+
* @param y
594+
* y value
595+
* @return true if contains
596+
* @since 2.0.7
597+
*/
598+
public boolean contains(double x, double y) {
599+
return x >= getMinX() && x <= getMaxX() && y >= getMinY()
600+
&& y <= getMaxY();
601+
}
602+
559603
/**
560604
* Determine if inclusively contains the provided envelope
561605
*

src/main/java/mil/nga/sf/util/GeometryUtils.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import mil.nga.sf.Geometry;
2222
import mil.nga.sf.GeometryCollection;
2323
import mil.nga.sf.GeometryType;
24+
import mil.nga.sf.Line;
2425
import mil.nga.sf.LineString;
2526
import mil.nga.sf.MultiLineString;
2627
import mil.nga.sf.MultiPoint;
@@ -1140,6 +1141,59 @@ private static boolean pointOnPath(Point point, List<Point> points,
11401141
return onPath;
11411142
}
11421143

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+
11431197
/**
11441198
* Determine if the geometries contain a Z value
11451199
*

0 commit comments

Comments
 (0)