直線與橢圓相交求交點

引自……CSDN

已知a,b和直線上的兩點,中心在原點,求直線與橢圓相交求交點座標

 


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
    double a,b,c,x1,x2,y1,y2,k,j;    
    printf("\nplease input a,b:\n");
    scanf("%lf%lf",&a,&b);
    printf("\nplease input x1,y1,x2,y2\n");
    scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
    k=(y1-y2)/(x1-x2);
    c=y1-k*x1;
    j=(2*a*a*k*c)*(2*a*a*k*c)-4*(b*b+a*a*k*k)*a*a*(c*c-b*b);
    
    if(j<0)
        printf("\nthe line don't intersect with the ellipse");
    else 
        if(j==0)
        {
            x1=-2*k*c*a*a/(2*(b*b+a*a*k*k));
            y1=k*x1+c;
            printf("\nthe line and ellipse intersect at\n(%lf,%lf)",x1,y1);
        }
    else
    {
        x1=(-2*k*c*a*a+sqrt(j))/(2*(b*b+a*a*k*k));
        y1=k*x1+c;
        x2=(-2*k*c*a*a-sqrt(j))/(2*(b*b+a*a*k*k));
        y2=k*x2+c;
        printf("\nthe line and ellipse intersect at\n(%lf,%lf) and (%lf,%lf)",x1,y1,x2,y2);
    }    
    getch();
}


 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章