我需要使用GraphicsPath绘制弧并具有初始,中值和最终点。弧必须传递它们。
我尝试过.DrawCurve和.DrawBezier,但结果并不完全是弧形。
我该怎么办?
SOLUTION:
经过几个小时的代码编写后,我设法用这个算法绘制了我想要的东西(给出3点a,b,c和一个GraphicsPath路径):
double d = 2 * (a.X - c.X) * (c.Y - b.Y) + 2 * (b.X - c.X) * (a.Y - c.Y);
double m1 = (Math.Pow(a.X, 2) - Math.Pow(c.X, 2) + Math.Pow(a.Y, 2) - Math.Pow(c.Y, 2));
double m2 = (Math.Pow(c.X, 2) - Math.Pow(b.X, 2) + Math.Pow(c.Y, 2) - Math.Pow(b.Y, 2));
double nx = m1 * (c.Y - b.Y) + m2 * (c.Y - a.Y);
double ny = m1 * (b.X - c.X) + m2 * (a.X - c.X);
double cx = nx / d;
double cy = ny / d;
double dx = cx - a.X;
double dy = cy - a.Y;
double distance = Math.Sqrt(dx * dx + dy * dy);
Vector va = new Vector(a.X - cx, a.Y - cy);
Vector vb = new Vector(b.X - cx, b.Y - cy);
Vector vc = new Vector(c.X - cx, c.Y - cy);
Vector xaxis = new Vector(1, 0);
float startAngle = (float)Vector.AngleBetween(xaxis, va);
float sweepAngle = (float)(Vector.AngleBetween(va, vb) + Vector.AngleBetween(vb, vc));
path.AddArc(
(float)(cx - distance), (float)(cy - distance),
(float)(distance * 2), (float)(distance * 2),
startAngle, sweepAngle);
答案 0 :(得分:6)
我会按照ANC_Michael的建议使用DrawArc()
。要找到通过3个点的弧,您需要计算由点形成的三角形的circumcircle。
一旦有外接圆计算圆的边界框,使用最小/最大尺寸(中心+/-半径)与DrawArc
一起使用。现在通过平移点来计算你的开始和停止角度,使得外接圆在原点上居中(由-circumcenter平移),并将标准化的起始和结束向量的点积与X轴取:
double startAngle = Math.Acos(VectorToLeftPoint.Dot(XAxis));
double stopAngle = Math.Acos(VectorToRightPoint.Dot(XAxis));
请注意,DrawArc
需要从X轴顺时针方向的角度,因此如果计算出的矢量位于x轴上方,则应添加Math.PI
。这应该是足够的信息来呼叫DrawArc()
。
编辑:此方法会找到圆形弧,而不一定是“最适合”弧,具体取决于您预期的端点行为。
答案 1 :(得分:0)
你是否尝试过DrawArc方法,看看你是否能以某种方式操纵你的3分?
也许
Pen blackPen= new Pen(Color.Black, 3);
// Create rectangle to bound ellipse.
Rectangle rect = new Rectangle(initial x, initial y, final x, median y);
// Create start and sweep angles on ellipse.
float startAngle = 0F;
float sweepAngle = 270.0F;
// Draw arc to screen.
e.Graphics.DrawArc(blackPen, rect, startAngle, sweepAngle);
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawarc%28VS.71%29.aspx