【问题标题】:Fill Octagon in C#在 C# 中填充八边形
【发布时间】:2018-03-21 03:39:34
【问题描述】:

我创建了一个绘制八边形的方法,效果很好,只要大小为200或更大

public static void FillOctagon(PaintEventArgs e, Color color, int x, int y, int width, int height)
{
     e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

     var points = new []
     {
          new Point(((x + width) / 2) - (width / 4), y), //side 1
          new Point(x, ((y + height) / 2) - (height / 4)), //side 2
          new Point(x, ((y + height) / 2) + (height / 4)), //side 3
          new Point(((x + width) / 2) - (width / 4), y + height), //side 4
          new Point((x + width) - (width / 4), y + height), //side 5
          new Point(x + width, ((y + height) / 2) + (height / 4)), //side 6
          new Point(x + width, ((y + height) / 2) - (height / 4)), //side 7
          new Point((x + width) - (width / 4), y) //side 8
     };

     using (var br = new SolidBrush(color))
     {
          using (var gpath = new GraphicsPath())
          {
              gpath.AddPolygon(points);
              e.Graphics.FillPath(br, gpath);
          }
     }
}

protected override void OnPaint(PaintEventArgs e)
{
     base.OnPaint(e);
     FillOctagon(e, Color.DodgerBlue, 20, 20, 50, 50);
}

好吧,我的问题是,如果尺寸小于 200 或者宽度与高度不同,反之亦然,图形就会变形。 我的目标是创建一个自适应图形,当宽度和高度小于 200 或宽度与高度不同时保持其形状

例如,如果我将大小设置为 50x50,就会发生这种情况:

【问题讨论】:

  • 我在 C# 中没有看到问题,我认为你应该检查你的算法,它会绘制形状
  • 不要同时使用宽度和高度,而是将Math.Min(width, height) 用于这两个数字。这将使其保持“方形”。
  • 我测试了您的代码,50x50 的形状与 200x200 的形状相同(只是更小)。尝试使用foreach(var p in points) { Debug.WriteLine(p.X + ", " + p.Y); } 将坐标记录到调试控制台并检查它们是否正确。
  • 我在 200x200、50x50 和 100x50 三种不同的场景中测试了您的代码,并且似乎运行良好。您的代码还有其他内容吗?你都发了吗?
  • 是的,这都是我的代码,我当时不明白,因为我被指定为 50x50 的尺寸变形了,如图所示

标签: c# graphics polygon shape


【解决方案1】:

我会给你一个不同的方法。将八边形想象成一个八边形的块状圆圈。

从圆的原点出发,您可以使用三角函数计算给定角度 t(以弧度为单位)和半径 r 的圆环边缘的点。

x = r cos t
y = r sin t

您可以应用此方法计算八边形(或具有任意边数的等边形状)的点,但它不能变形(拉伸)。为了让它变形,公式稍微改变(其中a是水平半径,b是垂直半径)。

x = a cos t
y = b sin t

这可能是代码中的样子 - 我已在此实例中修改了您的代码。

public static void FillEquilateralPolygon(PaintEventArgs e, int sides, Color color, double x, double y, double width, double height)
{
    e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

    double a = width / 2;
    double b = height / 2;

    var points = new List<Point>();

    for (int pn = 0; pn < sides; pn++)
    {
        double angle = (360.0 / sides * pn) * Math.PI / 180;
        double px = a * Math.Cos(angle);
        double py = b * Math.Sin(angle);
        var point = new Point((int) (px + x), (int) (py + y));
        points.Add(point);
    }

    using (var br = new SolidBrush(color))
    {
        using (var gpath = new GraphicsPath())
        {
            gpath.AddPolygon(points.ToArray());
            e.Graphics.FillPath(br, gpath);
        }
    }
}

现在可以调用这个方法,传入8个边,渲染一个可以变形的八角形。

FillEquilateralPolygon(e, 8, Color.Red, 201, 101, 201, 101);

如果不想变形,就用最小边的半径代替r换成ab .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-01
    • 2014-02-03
    • 1970-01-01
    相关资源
    最近更新 更多