求内切圆的圆心和半径(已知三个点的坐标)


/******
m0 n0 m1 n1 m2 n2 为三角形的三个点的坐标值 m为横坐标 n为纵坐标
px 内切圆的圆心的横坐标
py 内切圆的圆心的纵坐标
pr 内切圆的半径

***/

int NeiQieYuan(int m0, int n0, int m1, int n1, int m2, int n2, float *px, float *py, float *pr)
{
    int dax = 0;
    int day = 0;

    int dbx = 0;
    int dby = 0;

    float absA = 0.0f;
    float absB = 0.0f;
    float temp = 0;

    dax = m0 - m1;
    day = n0 - n1;

    dbx = m2 - m1;
    dby = n2 - n1;

    temp = dax * dax + day * day * 1.0f;
    absA = sqrtf(temp);
    temp = dbx * dbx + dby * dby * 1.0f;
    absB = sqrtf(temp);

    // (absB * day - absA * dby)(y - n1) = (absA * dbx - absB * dax)(x - m1)

    // 第一个角平分线方程
    // a(y - n1) = b(x - m1)


    // 方程1 
    float a = 0.0f;
    float b = 0.0f;

    a = (absB * day - absA * dby);
    b = (absA * dbx - absB * dax);



    dax = m0 - m2;
    day = n0 - n2;

    dbx = m1 - m2;
    dby = n1 - n2;

    temp = dax * dax + day * day * 1.0f;
    absA = sqrtf(temp);
    temp = dbx * dbx + dby * dby * 1.0f;
    absB = sqrtf(temp);

    float c = 0.0f;
    float d = 0.0f;

    c = (absB * day - absA * dby);
    d = (absA * dbx - absB * dax);
    // 第二个角平分线方程
    // c(y - n2) = d(x - m2)
    float PointX = 0.0f;
    float PointY = 0.0f;


    if(a != 0)
    {
        PointX = (c * b * m1 + n2 * a * c - n1 * a * c - a * d * m2) / (c * b - a * d);
        PointY = b * (PointX - m1) / a + n1;

    }else
    {
        PointX = m1;
        PointY = d * (m1 - m2) / c + n2;
    }

    // dax * (y - n2) = day * (x - m2)

    // 点到直线的方程 (-day)(y - PointY) = (dax)(x - PointX)

    // 计算点到直线的距离

    float intersectionX = 0.0f;
    float intersectionY = 0.0f;

    if(dax != 0)
    {
        intersectionX = (day * day * m2 - day * dax * n2 + day * dax * PointY + dax * dax * PointX) / (dax * dax + day * day);
        intersectionY = day * (intersectionX - m2) / dax + n2;

    }else
    {
        intersectionX = m2;
        intersectionY = dax * (intersectionX - PointX) / (-day) + PointY;
    }

    *px = PointX;
    *py = PointY;

    float temp1 = (intersectionX - PointX) * (intersectionX - PointX) + (intersectionY - PointY) * (intersectionY - PointY);
    *pr = sqrtf((intersectionX - PointX) * (intersectionX - PointX) + (intersectionY - PointY) * (intersectionY - PointY));

    return 0;

}

你可能感兴趣的:(图像处理)