To calculate the line intersection points between two shapes we use the
ILcd2DLineIntersectionTopology
interface,
as illustrated in the example below:
import com.luciad.geodesy.TLcdEllipsoid;
import com.luciad.geometry.ILcd2DLineIntersectionTopology;
import com.luciad.geometry.ellipsoidal.TLcdEllipsoidalBasicBinaryTopology;
import com.luciad.shape.ILcdPoint;
import com.luciad.shape.shape2D.ILcd2DEditablePoint;
import com.luciad.shape.shape2D.TLcd2DEditablePointList;
import com.luciad.shape.shape2D.TLcdLonLatPoint;
import com.luciad.shape.shape2D.TLcdLonLatPolygon;
public class LineIntersectionTutorial {
public static void main(String[] args) {
// Create the ellipsoid.
// For this sample, it is hardcoded here.
// In practice, the ellipsoid used by all shapes in the program should be used.
// In a cartesian topology, no ellipsoid is needed.
TLcdEllipsoid ellipsoid = new TLcdEllipsoid();
// Create two shapes.
// For this sample two simple triangles are created.
// In practice, these ILcdShape instances could originate from any source.
TLcdLonLatPolygon polygon1 = new TLcdLonLatPolygon(new TLcd2DEditablePointList(
new ILcd2DEditablePoint[]{
new TLcdLonLatPoint(0.0, 0.0),
new TLcdLonLatPoint(10.0, 20.0),
new TLcdLonLatPoint(20.0, 10.0)
}, false), ellipsoid);
TLcdLonLatPolygon polygon2 = new TLcdLonLatPolygon(new TLcd2DEditablePointList(
new ILcd2DEditablePoint[]{
new TLcdLonLatPoint(11.0, 12.0),
new TLcdLonLatPoint(5.0, 4.0),
new TLcdLonLatPoint(12.0, 0.0)
}, false), ellipsoid);
// Create the topology.
ILcd2DLineIntersectionTopology topology = new TLcdEllipsoidalBasicBinaryTopology(ellipsoid);
// Find the line intersections points.
ILcdPoint[] intersectionPoints = topology.lineIntersection(polygon1, polygon2);
System.out.println("Intersection points:");
for (ILcdPoint intersectionPoint : intersectionPoints) {
System.out.println(String.format("(%.2f, %.2f)", intersectionPoint.getX(), intersectionPoint.getY()));
}
}
}