判断线段是否在园内
给出圆心坐标和半径 还有线段的两个端点 判断线段是全在圆内还是部分在圆内 或是 全在圆外
#include<iostream> #include<stdio.h> #include<math.h> using namespace std; struct point{double x;double y;}; double length(point a,point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double dot(point a,point b) { return a.x*b.x+a.y*b.y; } double cross(point a,point b) { return a.x*b.y-a.y*b.x; } int dcmp(double x) { if(fabs(x)<0.000001) return 0; if(x<0) return -1; return 1; } double dis(point p,point a,point b) { point v1,v2,v3; v1.x=b.x-a.x; v1.y=b.y-a.y; v2.x=p.x-a.x; v2.y=p.y-a.y; v3.x=p.x-b.x; v3.y=p.y-b.y; if(dcmp(dot(v1,v2))<0) return length(p,a); else if(dcmp(dot(v1,v3))>0) return length(p,b); return fabs(cross(v1,v2))/length(b,a); } int main() { int i,j,n,m,t; point c,a,b; double r,l1,l2,l3; scanf("%d",&n); while(n--) { scanf("%lf%lf%lf",&c.x,&c.y,&r); scanf("%lf%lf%lf%lf",&a.x,&a.y,&b.x,&b.y); l1=length(a,c); l2=length(b,c); l3=dis(c,a,b); // printf("dis %lf %lf %lf\n",l1,l2,l3); if(l1<=r&&l2<=r) printf("ALL IN\n"); else if (l1>=r&&l2>=r&&l3>=r) printf("ALL OUT\n"); else printf("PART IN\n"); } return 0; }