c# - 将 N 像素缓冲区添加到多边形(区域/路径),同时在 C# 中维护质心

标签 c# geometry system.drawing polygons scaletransform

问题:将 n 个像素缓冲区添加到现有多边形(保证闭合、不重叠且点顺时针排列),同时保持多边形的质心。

当前: 我有一个 PolyRegion 类,其中包含一个 System.Drawing Path 和 System.Drawing Region。当我实例化类时,我会将因子缓冲区添加到路径并缩放/转换它。如果相对于质心缩放,则结果会偏移。

示例:绿色多边形是原始多边形。当我按 n 倍缩放它时,我得到/想要紫色多边形(以质心为中心)。

enter image description here

问题:如何相对于质心进行缩放?我最好缩放点阵列中的每个点或缩放路径/区域吗?

代码:

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/

相关文章:

c# - 在 WinForms 中绘制垂直堆叠的文本

c# - 质量和压缩与 system.drawing.imaging 之间的区别?

c# - 如何使用 IoC 注入(inject)打开的连接

image - 在圆形图像顶部带有边框的圆形图像 css

C# 正则表达式 : An object reference is required for the non-static field, 方法,或属性 'Regex.Match(string)'

c# - C#/WPF 中相邻形状的并集

python - 从给定的 n 个点中选择最远的 k 个点

c# - 比例平移

c# - 筛选后选中的行变为未选中

c# - 为什么我的上下文菜单不能使用 DataContext={Binding}?