1. 程式人生 > >任意三點繪製圓弧 求弧長

任意三點繪製圓弧 求弧長

接著  http://blog.csdn.net/ilson_/article/details/77018346

1. 先求出線的方向角(用向量)

// 得到兩點的角度 
// 0 - 360 以 point1 為圓心繞一週
double CShape::getAngleFromTwoPoint(CPoint point1, CPoint point2)
{
    double angle = 0;
CPoint O = point1; //以第一點為圓心旋轉
CPoint A = point2;

double r = getTheRealLengthOfTheLine(O, A, FALSE); //這只是求兩點間距離很簡單的我就不貼了

CPoint B = CPoint(O.x + r, O.y); //以圓心O為起點 O點右側距離r個畫素長度做B點(O->B)
CPoint OA = CPoint(A.x - O.x, A.y - O.y); //向量OA O->A
CPoint OB = CPoint(B.x - O.x, B.y - O.y); //向量OB O->B

double sin = (OA.x * OB.y - OB.x * OA.y) / (r * r); // |OA| = |OB| = r
angle = ASIN(sin); //弧度 ASIN(c) = (asin(c) * (180.0 / PI))

if (O.x < A.x && O.y > A.y)
 //象限1
angle = angle;
else if (O.x > A.x && O.y > A.y) //象限2
angle = 180 - angle;
else if (O.x > A.x && O.y < A.y) //象限3
angle = 180 - angle;
else if (O.x < A.x && O.y < A.y) //象限4
angle = 360 + angle;

return angle;
}


2. 主要判斷B點的範圍

// 計算弧長 L = ⊙ * r (弧度 * 半徑)
// 判斷中點B與圓心O是否在 AC 夾角內
// 如果在 圓弧角度為 angle = A角 - C角; 如果不在 angle = 360 - (A角 - C角) 取反
void CMyArc::getTheDataOfTheArc(OUT double &lengthArc, OUT double &angle)
{

CPoint O = m_ptResetPoint[3]; //圓心
CPoint A = m_ptResetPoint[0]; //起點
CPoint B = m_ptResetPoint[1]; //中點
CPoint C = m_ptResetPoint[2]; //終點

double OA = getAngleFromTwoPoint(O, A, TRUE);
double OB = getAngleFromTwoPoint(O, B, TRUE);
double OC = getAngleFromTwoPoint(O, C, TRUE);

double fMax = max(OA, OC);
double fMin = min(OA, OC);
if (OB > fMin && OB < fMax)
angle = fMax - fMin;
else
angle = 360 - (fMax - fMin);

double r = getTheRealLengthOfTheLine(O, A, TRUE) * m_fRealRuler;
double radian = angle * PI / 180; //角度轉弧度
lengthArc = radian * r;
}