我创建一个 GraphicsPath 对象,添加一个椭圆,旋转 GraphicsPath 对象然后绘制它。现在我想获取 graphicsPath 最左边的点,这样我就可以检查它是否在特定边界内(用户可以用鼠标移动 graphicsPath)。
我目前正在使用 GraphicsPath 中的 GetBounds() 方法,但这只会导致以下结果
蓝色是来自 GetBounds() 的矩形,因此您可以看出我从该方法获得的最左边的点与我想要的点之间有一些空间。我怎样才能得到我正在寻找的点?
最佳答案
如果您实际旋转GraphicsPath
,您可以使用Flatten
函数来获取大量路径点。然后您可以选择最小的 x 值并从中选择相应的 y 值。
这会起作用,因为你有一个椭圆,所以只有一个点可以在最左边..
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/