The Problem: Add n pixel buffer to an existing polygon (guaranteed to be closed, non-overlapping, and points in clockwise formation) while maintaining the centroid of the polygon.
Current: I have a PolyRegion class which contains a System.Drawing Path and System.Drawing Region. When I instantiate the class I'd add the factor buffer to the path and scale/transform it. The result is offset instead if scaled with respect to the centroid.
Example: The green polygon is the original. When I scale it by factor of n, I get/want the purple polygon (centered on centroid).
Question: How can I scale with respect to the centroid? AM I better off scaling each point in the point array or scaling the Path/Region?
Code:
public PolyRegion(Int32 Id, List<Point> Points)
{
this.Id = Id;
this.Points = Points;
this.Path = this.CreatePath();
float factor = 10;
float wScale = (float)((this.Path.GetBounds().Width - factor) / this.Path.GetBounds().Width);
float hScale = (float)((this.Path.GetBounds().Height - factor) / this.Path.GetBounds().Height);
Matrix transformMatrix = new Matrix();
transformMatrix.Scale(wScale, hScale);
this.Path.Transform(transformMatrix);
this.Region = new Region(this.Path);
this.Area = CalculateArea();
}
This is more a mathematical/geometric issue than a programming one:
1) Translate figure from centroid position (x, y) to (0,0) coordinate
2) Scale by the factor you desire.
3) Translate back to original centroid.