问题:将 n 个像素缓冲区添加到现有多边形(保证闭合、不重叠且点顺时针排列),同时保持多边形的质心。
当前: 我有一个 PolyRegion 类,其中包含一个 System.Drawing Path 和 System.Drawing Region。当我实例化类时,我会将因子缓冲区添加到路径并缩放/转换它。如果相对于质心缩放,则结果会偏移。
示例:绿色多边形是原始多边形。当我按 n 倍缩放它时,我得到/想要紫色多边形(以质心为中心)。
问题:如何相对于质心进行缩放?我最好缩放点阵列中的每个点或缩放路径/区域吗?
代码:
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();
}
最佳答案
与其说是编程问题,不如说这是一个数学/几何问题:
1) 将图形从质心位置 (x, y) 平移到 (0,0) 坐标
2) 按您想要的系数进行缩放。
3) 转换回原始质心。
关于c# - 将 N 像素缓冲区添加到多边形(区域/路径),同时在 C# 中维护质心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20357322/