c# - 获取旋转的 GraphicsPath 中最左边的点

标签 c# graphicspath

我创建一个 GraphicsPath 对象,添加一个椭圆,旋转 GraphicsPath 对象然后绘制它。现在我想获取 graphicsPath 最左边的点,这样我就可以检查它是否在特定边界内(用户可以用鼠标移动 graphicsPath)。

我目前正在使用 GraphicsPath 中的 GetBounds() 方法,但这只会导致以下结果

image .

蓝色是来自 GetBounds() 的矩形,因此您可以看出我从该方法获得的最左边的点与我想要的点之间有一些空间。我怎样才能得到我正在寻找的点?

最佳答案

如果您实际旋转GraphicsPath,您可以使用Flatten 函数来获取大量路径点。然后您可以选择最小的 x 值并从中选择相应的 y 值。

这会起作用,因为你有一个椭圆,所以只有一个点可以在最左边..

enter image description here

private void panel1_Paint(object sender, PaintEventArgs e)
{
    GraphicsPath gp = new GraphicsPath();
    gp.AddEllipse(77, 55, 222, 77);

    Rectangle r = Rectangle.Round(gp.GetBounds());
    e.Graphics.DrawRectangle(Pens.LightPink, r);
    e.Graphics.DrawPath(Pens.CadetBlue, gp);

    Matrix m = new Matrix();
    m.Rotate(25);
    gp.Transform(m);
    e.Graphics.DrawPath(Pens.DarkSeaGreen, gp);
    Rectangle rr = Rectangle.Round(gp.GetBounds());
    e.Graphics.DrawRectangle(Pens.Fuchsia, rr);

    GraphicsPath gpf = (GraphicsPath)gp.Clone();
    gpf.Flatten();
    float mix = gpf.PathPoints.Select(x => x.X).Min();
    float miy = gpf.PathPoints.Where(x => x.X == mix).Select(x => x.Y).First();
    e.Graphics.DrawEllipse(Pens.Red, mix - 2, miy - 2, 4, 4);
}

请不要问我为什么旋转边界这么宽——我真的不知道!

如果您在绘制之前旋转 Graphics 对象,您仍然可以使用相同的技巧..

关于c# - 获取旋转的 GraphicsPath 中最左边的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45675882/

相关文章:

c# - 具有 10 个元素的随机 boolean 数组,其中 10 个元素中有 3 个为真

c# - 如何在 C# 中查找多个

c# - 使用 Graphics.FillPath 奇怪地绘制 GraphicsPath

c# - 使用 PathGradientBrush 创建的阴影显示出不需要的刺结果

c# - WinForms:在正确的位置绘制路径

python - GraphicsPath 并不总是自行刷新

c# - SQL 到 LINQ - Case 语句

c# - 使用 RequiredFieldValidator 验证服务器控件中的文本框

c# - C#中使用GEO坐标绘制 map

c# - 如何在 Linq to SQL 中使用 distinct 和 group by