I have been working on a Geometry based project in C#. For this, I am using NetTopologySuite. I have a Z_Point class which is a basic class with a constructor that takes three doubles for X , Y and Z. Then I have a Z_Polygon class which has a constructor that takes a list of points as argument. For offsetting the polygon, I use NetTopologySuite, and the Offset method of my Z_Polygon has the following implementation;
public List<Z_Polygon> Offset(double distance)
{
Coordinate[] coords = new Coordinate[Vertices.Count];
for (int i = 0; i < this.Vertices.Count; i++)
{
coords[i] = new Coordinate(this.Vertices[i].X, this.Vertices[i].Y);
}
LinearRing rng1 = new LinearRing(coords);
Polygon pol1 = new Polygon(rng1);
var bufParams = new BufferParameters();
bufParams.MitreLimit = 5;
bufParams.JoinStyle = JoinStyle.Mitre;
Geometry b = BufferOp.Buffer(pol1, distance, bufParams);
List<Z_Polygon> result = new List<Z_Polygon>();
List<Z_Point> newVertices = new List<Z_Point>();
if (b is MultiPolygon)
{
b = (MultiPolygon)b;
for (int i = 0; i < b.NumGeometries; i++)
{
Polygon pol = (Polygon)b.GetGeometryN(i);
Geometry g = (Geometry)pol;
foreach (var item in pol.Coordinates)
{
newVertices.Add( new Z_Point(item.X, item.Y));
}
result.Add(new Z_Polygon(newVertices));
}
}
else
{
Polygon pol = (Polygon)b;
foreach (var item in pol.Coordinates)
{
newVertices.Add(new Z_Point(item.X, item.Y));
}
result.Add(new Z_Polygon(newVertices));
}
return result;
}
However, this is not returning the correct result. It gives some extra lines which are not required. Kindly check the image below; The image shows the actual result and expected result side by side
Here is the code for the polygon;
Z_Point p1 = new Z_Point(1300.0, 1200.0);
Z_Point p2 = new Z_Point(1300.0, 9800.0);
Z_Point p3 = new Z_Point(6200.0, 9800.0);
Z_Point p4 = new Z_Point(6200.0, 6500.0);
Z_Point p5 = new Z_Point(2600.0, 6500.0);
Z_Point p6 = new Z_Point(2600.0, 5300.0);
Z_Point p7 = new Z_Point(10700.0, 5300.0);
Z_Point p8 = new Z_Point(10700.0, -2900.0);
Z_Point p9 = new Z_Point(3200.0, -2900.0);
Z_Point p10 = new Z_Point(3200.0, -800.0);
Z_Point p11 = new Z_Point(9700.0, -800.0);
Z_Point p12 = new Z_Point(9700.0, 1200.0);
Z_Polygon polygon1 = new Z_Polygon(new List<Z_Point>() { p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p1 });
List<Z_Point> new_polygon = polygon1.Offset(-800);
You forgot to clear your newVertices
list after adding a new polygon to the result. It might be necessary to create a new list inside the loop.