任意多边形求交:
private void btnPolygon_Click(object sender, EventArgs e)
{ double[] Xs = new double[] { , , , , , , , , , , , , , };
double[] Ys = new double[] { , , , , , , , };
int m = Xs.Length / ;
var coords = new Coordinate[m];
var coordscheck = new Coordinate[m];
for (var i = ; i < Xs.Length / ; i++)
{
var x = Xs[i * ];
var y = Xs[i * + ];
coords[i] = new Coordinate(x, y);
coordscheck[i] = new Coordinate(x, y);
}
// Shell Rings
var ring = new LinearRing(coords);
var gf = new GeometryFactory();
var ringCheck = gf.CreateLinearRing(coordscheck);
// Hole Coordinates
int n = Ys.Length / ;
var coordsholecheck = new Coordinate[n];
var coordshole = new Coordinate[n];
for (var i = ; i < Ys.Length / ; i++)
{
var x = Ys[i * ];
var y = Ys[i * + ];
coordshole[i] = new Coordinate(x, y);
coordsholecheck[i] = new Coordinate(x, y);
}
// Hole LinearRing Arrays
var hole = new LinearRing(coordshole);
var holes = new ILinearRing[];
var holeCheck = gf.CreateLinearRing(coordsholecheck);
var holescheck = new ILinearRing[];
holes[] = hole;
holescheck[] = holeCheck;
Polygon pg = new Polygon(ring, holes);//
var polygonCheck = gf.CreatePolygon(ringCheck, holescheck);
var areaCheck = polygonCheck.Area;
var area = pg.Area;
//
inputGeometry = pg; double[] XsAnalysis = new double[] { , , , , , , , , , , , , , };
double[] YsAnalysis = new double[] { , , , , , , , , , , , };
m = XsAnalysis.Length / ;
var coordsAnalysis = new Coordinate[m];
var coordscheckAnalysis = new Coordinate[m];
for (var i = ; i < XsAnalysis.Length / ; i++)
{
var x = XsAnalysis[i * ];
var y = XsAnalysis[i * + ];
coordsAnalysis[i] = new Coordinate(x, y);
coordscheckAnalysis[i] = new Coordinate(x, y);
}
// Shell Rings
var ringAnalysis = new LinearRing(coordsAnalysis);
var gfAnalysis = new GeometryFactory();
var ringCheckAnalysis = gfAnalysis.CreateLinearRing(coordscheckAnalysis);
// Hole Coordinates
n = YsAnalysis.Length / ;
var coordsholecheckAnalysis = new Coordinate[n];
var coordsholeAnalysis = new Coordinate[n];
for (var i = ; i < YsAnalysis.Length / ; i++)
{
var x = YsAnalysis[i * ];
var y = YsAnalysis[i * + ];
coordsholeAnalysis[i] = new Coordinate(x, y);
coordsholecheckAnalysis[i] = new Coordinate(x, y);
}
// Hole LinearRing Arrays
var holeAnalysis = new LinearRing(coordsholeAnalysis);
var holesAnalysis = new ILinearRing[];
var holeCheckAnalysis = gf.CreateLinearRing(coordsholecheckAnalysis);
var holescheckAnalysis = new ILinearRing[];
holesAnalysis[] = holeAnalysis;
holescheckAnalysis[] = holeCheckAnalysis;
Polygon pgAnalysis = new Polygon(ringAnalysis, holesAnalysis);//
var polygonCheckAnalysis = gf.CreatePolygon(ringCheckAnalysis, holescheckAnalysis);
var areaCheckAnalysis = polygonCheckAnalysis.Area;
var areaAnalysis = pgAnalysis.Area;
//
AnalysisPolygonGeometry = pgAnalysis; intersectionOutput = AnalysisPolygonGeometry.Intersection(inputGeometry);
this.Invalidate();
}