C#用3点绘制圆弧
我需要使用GraphicsPath绘制弧并具有初始,中值和最终点。 弧必须传递它们。
我试过.DrawCurve和.DrawBezier但结果并不完全是弧形。
我能做什么?
解:
经过几个小时的代码编写后,我设法用这个算法绘制了我想要的东西(给出3点a,b,c和一个GraphicsPath路径):
double d = 2 * (aX - cX) * (cY - bY) + 2 * (bX - cX) * (aY - cY); double m1 = (Math.Pow(aX, 2) - Math.Pow(cX, 2) + Math.Pow(aY, 2) - Math.Pow(cY, 2)); double m2 = (Math.Pow(cX, 2) - Math.Pow(bX, 2) + Math.Pow(cY, 2) - Math.Pow(bY, 2)); double nx = m1 * (cY - bY) + m2 * (cY - aY); double ny = m1 * (bX - cX) + m2 * (aX - cX); double cx = nx / d; double cy = ny / d; double dx = cx - aX; double dy = cy - aY; double distance = Math.Sqrt(dx * dx + dy * dy); Vector va = new Vector(aX - cx, aY - cy); Vector vb = new Vector(bX - cx, bY - cy); Vector vc = new Vector(cX - cx, cY - 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);
我会DrawArc()
建议使用DrawArc()
。 要找到通过3个点的弧,您需要计算由点形成的三角形的外接圆 。
一旦你有外接圆计算圆的边界框,使用最小/最大尺寸(中心+/-半径)与DrawArc
使用。 现在通过平移点来计算起始和终止角度,使得外接圆在原点上居中(由-circumcenter平移),并使用X轴取标准化的起始和结束向量的点积:
double startAngle = Math.Acos(VectorToLeftPoint.Dot(XAxis)); double stopAngle = Math.Acos(VectorToRightPoint.Dot(XAxis));
请注意, DrawArc
需要从X轴顺时针方向的角度,因此如果计算出的矢量高于x轴,则应添加DrawArc
这应该是调用DrawArc()
足够信息。
编辑:此方法将找到圆弧,而不一定是“最佳拟合”弧,具体取决于您的预期端点行为。
你有没有尝试过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