Kee*_*per 11 c# graphics drawing
我需要使用GraphicsPath绘制弧并具有初始,中值和最终点.弧必须传递它们.
我试过.DrawCurve和.DrawBezier,但结果并不完全是弧形.
我能做什么?
解:
经过几个小时的代码编写后,我设法用这个算法绘制了我想要的东西(给出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);
我会DrawArc()按照ANC_Michael的建议使用.要找到通过3个点的弧,您需要计算由点形成的三角形的外接圆.  
一旦你有外接圆计算圆的边界框,DrawArc使用最小/最大尺寸(中心+/-半径).现在通过平移点来计算起始和终止角度,使得外接圆在原点上居中(由-circumcenter平移),并使用X轴取标准化起始和结束向量的点积:
double startAngle = Math.Acos(VectorToLeftPoint.Dot(XAxis));
double stopAngle = Math.Acos(VectorToRightPoint.Dot(XAxis));
请注意,DrawArc期望从X轴顺时针方向的角度,因此Math.PI如果计算的矢量高于x 轴,则应添加.这应该是足够的信息来打电话DrawArc().
编辑:此方法会找到一个圆形弧不一定是"最适合"弧根据您的预计终结点行为.
| 归档时间: | 
 | 
| 查看次数: | 12647 次 | 
| 最近记录: |