[C语言] 已知圆上两点球圆心

//待完善-仅供参考
#include "stdio.h"
#include "math.h"
double O1_x = 0,O1_y = 0;
double O2_x = 0,O2_y = 0;

void CircleCenter(double x1,double y1,double x2,double y2,double R)  
{  
    double c1=0,c2=0,A=0,B=0,C=0;
    c1 =  (pow(x2, 2) - pow(x1, 2) + pow(y2, 2) - pow(y1, 2)) / 2 /(x2 - x1); 
    c2 = (y2 - y1) / (x2 - x1);  //斜率
    A = (pow(c2,2)+ 1.0);  
    B = (2 * x1*c2 - 2 * c1*c2 - 2 * y1);  
    C = pow(x1-c1,2)+pow(y1,2)-pow(R,2);  
    printf("A:%f,B:%f,C:%f,c1:%f,c2:%f\n",A,B,C,c1,c2);
    O1_x = (-B + sqrt(B*B - 4 * A*C)) / (2 * A);
    O1_y = c1 - c2 * O1_x; 
    O2_x = (-B - sqrt(B*B - 4 * A*C)) / (2 * A);
    O2_y = c1 - c2 * O2_x;
        
}  
void main(void)
{
    double x1=0,y1=0,x2=0,y2=0,r=0,O[2]={0},a=0;
    printf("请分别输入(x1,y1)和(x2,y2):\n");
    printf("\tx1:");
    scanf("%lf",&x1);
    printf("\ty1:");
    scanf("%lf",&y1);
    printf("\n\tx2:");
    scanf("%lf",&x2);
    printf("\ty2:");
    scanf("%lf",&y2);
    printf("\n\tr:");
    scanf("%lf",&r);
    a = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 
    if((2*r)<a)
    {
        printf("无解!\n");
        return ;
    }
    else if(r>a)
        printf("有2解!\n");
    else
        printf("有1解!\n");
    
    CircleCenter(x1,y1,x2,y2,r);
    printf("圆心1坐标位:(%f,%f)\n",O1_x,O1_y);
    printf("圆心2坐标位:(%f,%f)",O2_x,O2_y);
}

猜你喜欢

转载自www.cnblogs.com/star-water/p/13203991.html