我有 3 条线定义为线 A、线 B 和线 C,并且想计算线 B 和 C 与 A 之间的交点。从 JTS 有一个函数LineIntersector应该有助于实现这一点。我需要帮助将此函数应用于线条以找到交点,即。像 computeIntersection(line A, line B) 之类的东西。谢谢!
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.algorithm.*;
public class PointTest {
public static void main(String[] args){
// We have to have an even number of arguments - to have coordinate pairs for points.
if (args.length % 2 == 1) {
System.out.println("Wrong input. You did not enter a list of coordinage pairs. Try again.");
}
else {
int i=0;
String[] coordA = {"12", "2", "12", "13", "12", "19"};
String[] coordB = {"2", "10", "10", "10", "21", "11"};
String[] coordC = {"1","1", "9","9", "20", "20"};
// Create a new empty array of coordinates.
//Coordinate[] coordinates = new Coordinate[args.length/2];
Coordinate[] coordinatesA = new Coordinate[coordA.length/2];
Coordinate[] coordinatesB = new Coordinate[coordB.length/2];
Coordinate[] coordinatesC = new Coordinate[coordC.length/2];
// Go through the args and add each point as a Coordinate object to the coordinates array.
//Geometry g1 = new GeometryFactory().createLineString(coordinatesA);
//System.out.println(g1);
while (i < coordA.length) {
// transform string arguments into double values
double x = Double.parseDouble(coordA[i]);
double y = Double.parseDouble(coordA[i+1]);
double xx = Double.parseDouble(coordB[i]);
double yy = Double.parseDouble(coordB[i+1]);
double xxx = Double.parseDouble(coordC[i]);
double yyy = Double.parseDouble(coordC[i+1]);
// create a new Coordinate object and add it to the coordinates array
Coordinate newCoord = new Coordinate(x,y);
coordinatesA[i/2] = newCoord;
Coordinate newCoordB = new Coordinate(xx,yy);
coordinatesB[i/2] = newCoordB;
Coordinate newCoordC = new Coordinate(xxx,yyy);
coordinatesC[i/2] = newCoordC;
//System.out.println(newCoordB.toString());
i=i+2;
} // while
// Create a new Geometry from the array of coordinates.
LineString lineA = new GeometryFactory().createLineString(coordinatesA);
LineString lineB = new GeometryFactory().createLineString(coordinatesB);
LineString lineC = new GeometryFactory().createLineString(coordinatesC);
System.out.println("Line A is "+ lineA);
System.out.println("Line B is "+ lineB);
System.out.println("Line C is "+ lineC);
// Read the start and end point of the line and write them on the screen.
Point startPointA = lineA.getStartPoint();
Point endPointA = lineA.getEndPoint();
//System.out.println("The start point of the line is: " + startPointA.toString());
//System.out.println("The end point of the line is: " + endPointA.toString());
} // else
} //main
}