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


转自:http://blog.csdn.net/baidu_31872269/article/details/54923589

/******
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;

}
   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109

根据三角形外心的定义,外接圆的圆心就是三角形三边的垂直平分线的交半径为垂直平分线长度的一半。 具体实现上,可以使用以下步骤: 1. 计算三角形三边的长度和半周长。 2. 计算三角形面积。 3. 根据海伦公式计算外接圆半径。 4. 计算三角形三边的中垂线方程。 5. 解中垂线交,即为外接圆心。 下面是一个示例代码: ```c #include <stdio.h> #include <math.h> struct Point { double x; double y; }; double getDistance(struct Point p1, struct Point p2) { double dx = p1.x - p2.x; double dy = p1.y - p2.y; return sqrt(dx * dx + dy * dy); } struct Circle { struct Point center; double radius; }; struct Circle getCircle(struct Point p1, struct Point p2, struct Point p3) { double a = getDistance(p1, p2); double b = getDistance(p2, p3); double c = getDistance(p3, p1); double s = (a + b + c) / 2; double area = sqrt(s * (s - a) * (s - b) * (s - c)); double radius = a * b * c / (4 * area); double ma = (p2.y - p1.y) / (p2.x - p1.x); double mb = (p3.y - p2.y) / (p3.x - p2.x); double x = (ma * mb * (p1.y - p3.y) + mb * (p1.x + p2.x) - ma * (p2.x + p3.x)) / (2 * (mb - ma)); double y = (-1 / ma) * (x - (p1.x + p2.x) / 2) + (p1.y + p2.y) / 2; struct Circle circle = {{x, y}, radius}; return circle; } int main() { struct Point p1 = {0, 0}; struct Point p2 = {3, 0}; struct Point p3 = {0, 4}; struct Circle circle = getCircle(p1, p2, p3); printf("Circle center: (%.2f, %.2f)\n", circle.center.x, circle.center.y); printf("Circle radius: %.2f\n", circle.radius); return 0; } ``` 这里假设三角形的三个已知,并用结构体表示和圆,getDistance函数用于计算两间的距离。getCircle函数用于计算外接圆的圆心半径,其中ma和mb分别是p1p2和p2p3两条边的中垂线斜率,x和y是中垂线交坐标。最后在main函数中测试代码,并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值