1. 三點繪製圓
已知三點爲(x1,y1)、(x2,y2),(x3,y3),求三點形成的圓的座標,可以設求得的圓的原點爲(X,Y),半徑爲R,這樣可以得到方程組:
(x1-X)²-(y1-Y)²=R² (1)式
(x2-X)²-(y2-Y)²=R² (2)式
(x3-X)²-(y3-Y)²=R² (3)式
由上述方程組可以得到:
2(x1-x2)X +2(y1-y2)Y= x12-x22+y12-y22 (5)式
2(x1-x3)X +2(y1-y3)Y= x12-x32+y12-y32 (6)式
此時,設:
A= x1-x2
B = y1-y2
C= x1-x3
D = y1-y3
E= (x12-x22+y12-y22)/2
F= (x12-x32+y12-y32)/2
得到方程組:
AX+BY = E
CX+DY = F
求解,得到
X= -DE-BF)/ (BC-AD)
Y = -(AF-CE)/ (BC-AD)
經過計算,得到了圓心的座標位置,也就得到了圓的半徑計算公式
R = sqrt((x1-X)(x1-X)+(y1-Y) (y1-Y));
2. 在圓上確定三點的角度
根據四個象限的座標,選定cos作爲角度的值,並利用sin值擴展到360度全象限,利用三個點的位置關係確定弧度爲順時針還是逆時針繪製弧線,其中某點(x0, y0):
Sin = (y0-Y)/R
Cos= (x0-X)/R
3. 繪製弧線
在C#中,可以利用DrawArc()繪製弧線,其中最重要的要注意角度,繪製時角度要沿順時針旋轉,角度0度位於X軸的正方向上,在繪製弧度時,確定三個點的位置關係十分重要。
4.C#程序源碼
void DrawArcFromThreePoint(Graphics mImgGraph,int x1, int y1, int x2, int y2, int x3, int y3)
{
double a = x1 - x2;
double b = y1 - y2;
double c = x1 - x3;
double d = y1 - y3;
double e = ((x1 * x1 - x2 * x2) + (y1 * y1 - y2 * y2)) / 2.0;
double f = ((x1 * x1 - x3 * x3) + (y1 * y1 - y3 * y3)) / 2.0;
double det = b * c - a * d;
if( Math.Abs(det) >0.001)
{
//x0,y0爲計算得到的原點
double x0 = -(d * e - b * f) / det;
double y0 = -(a * f - c * e) / det;
SolidBrush OriginBrush = newSolidBrush(Color.Blue);
mImgGraph.FillEllipse(OriginBrush,(int)(x0-3),(int)(y0-3),6,6);
double radius = Math.Sqrt((x1 - x0)*(x1 - x0)+(y1 - y0)*(y1 - y0));
double angle1;
double angle2;
double angle3;
double sinValue1;
double cosValue1;
double sinValue2;
double cosValue2;
double sinValue3;
double cosValue3;
sinValue1 = (y1 - y0) / radius;
cosValue1 = (x1 - x0) / radius ;
if (cosValue1 >= 0.99999) cosValue1 = 0.99999;
if (cosValue1 <= -0.99999) cosValue1 = -0.99999;
angle1 = Math.Acos(cosValue1);
angle1 = angle1 / 3.14 * 180;
if (sinValue1 < -0.05) angle1 = 360 - angle1;
sinValue2 = (y2 - y0) / radius;
cosValue2 = (x2 - x0) / radius;
if (cosValue2 >= 0.99999) cosValue2 = 0.99999;
if (cosValue2 <= -0.99999) cosValue2 = -0.99999;
angle2 = Math.Acos(cosValue2);
angle2 = angle2 / 3.14 * 180;
if (sinValue2 < -0.05) angle2 = 360 - angle2;
sinValue3 = (y3 - y0) / radius;
cosValue3 = (x3 - x0) / radius;
if (cosValue3 >= 0.99999) cosValue3 = 0.99999;
if (cosValue3 <= -0.99999) cosValue3 = -0.99999;
angle3 = Math.Acos(cosValue3);
angle3 = angle3 / 3.14 * 180;
if (sinValue3 < -0.05) angle3 = 360-angle3 ;
Pen CurvePen = newPen(m_CurveColor);
Pen CurvePen1 = newPen(Color.Green);
bool PosDown = false;
double Delta13;
if (angle1 < angle3)
{
Delta13 = angle3 - angle1;
}
else Delta13 = angle3 - angle1 + 360;
double Delta12;
if (angle1 < angle2)
{
Delta12 = angle2 - angle1;
}
else Delta12 = angle2 - angle1 + 360;
if (Delta13 > Delta12) PosDown = true;
else PosDown = false;
if (PosDown)
{
if (angle3 > angle1) mImgGraph.DrawArc(CurvePen, (int)(x0 - radius), (int)(y0 - radius), (int)(2 * radius), (int)(2 * radius), (int)(angle1), (int)(angle3 - angle1));
else mImgGraph.DrawArc(CurvePen, (int)(x0 - radius), (int)(y0 - radius), (int)(2 * radius), (int)(2 * radius), (int)(angle1), (int)(angle3 - angle1 + 360));
}
else
{
if (angle1 > angle3) mImgGraph.DrawArc(CurvePen, (int)(x0 - radius), (int)(y0 - radius), (int)(2 * radius), (int)(2 * radius), (int)(angle3), (int)(angle1 - angle3));
else mImgGraph.DrawArc(CurvePen, (int)(x0 - radius), (int)(y0 - radius), (int)(2 * radius), (int)(2 * radius), (int)(angle3), (int)(angle1 - angle3 + 360));
}
}
}