zoukankan      html  css  js  c++  java
  • 判断线段与圆是否相交

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 
     5 inline int read(){
     6 int x=0,f=1; char ch=getchar();
     7 while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); }
     8 while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar();}
     9 return x*f;
    10 }
    11 
    12 inline void write(int x){
    13 if(x<0){
    14 putchar('-');
    15 x=-x;
    16 }
    17 if(x>9){
    18 write(x/10);
    19 }
    20 putchar(x%10+'0');
    21 }
    22 
    23 double X,Y,R;
    24 double p_x1,p_y1,p_x2,p_y2;
    25 
    26 int judge(double a,double b){ //判断点在不在圆内圆上
    27 double shu1=R*R,shu2=(a-X)*(a-X)+(b-Y)*(b-Y);
    28 if(shu1>shu2) return -1; //圆内
    29 else if(shu1==shu2) return 0; //圆上
    30 else return 1; //圆外
    31 }
    32 
    33 
    34 int main(){
    35 while(scanf("%lf%lf%lf",&X,&Y,&R)!=EOF){
    36 scanf("%lf%lf%lf%lf",&p_x1,&p_y1,&p_x2,&p_y2);
    37 int flag1=judge(p_x1,p_y1),flag2=judge(p_x2,p_y2);
    38 bool flag=false;
    39 if(flag1==0||flag2==0) flag=true; //有一个在圆上
    40 else if(flag1==-1&&flag2==-1) flag=false; //两个都在圆内
    41 else if((flag1==1&&flag2==-1)||(flag1==-1&&flag2==1)) flag=true; //一个在圆内 一个在圆外
    42 else{
    43 double A,B,C;
    44 if(p_x1==p_x2) A=1,B=0,C=-p_x1;
    45 else if(p_y1==p_y2) A=0,B=1,C=-p_y1;
    46 else A=p_y2-p_y1,B=-(p_x2-p_x1),C=-p_x1*p_y2+p_x2*p_y1;
    47 double shu1=(A*X+B*Y+C)*(A*X+B*Y+C);
    48 double shu2=A*A+B*B;
    49 if(shu1<=R*R*shu2){
    50 double eex=X-p_x1,eey=Y-p_y1,rrx=p_x2-p_x1,rry=p_y2-p_y1;
    51 double angleone=eex*rrx+eey*rry;
    52 eex=X-p_x2,eey=Y-p_y2,rrx=p_x1-p_x2,rry=p_y1-p_y2;
    53 double angletwo=eex*rrx+eey*rry;
    54 if(angleone>0&&angletwo>0) flag=true;
    55 }
    56 
    57 }
    58 if(!flag) printf("The line segment does not intersect the circle.
    ");
    59 else printf("The line segment intersects the circle.
    ");
    60 }
    61 
    62 return 0;
    63 }
    64 
    65  
    View Code
  • 相关阅读:
    Linux: 安装和启用firefox浏览器的java
    Latex: beamer
    时频分析:窗口傅立叶变换
    Python: 面向对象
    Linux: 安装NVIDIA显卡驱动
    Matlab: 路径的操作
    python--文件读写
    python--函数
    python--数据类型
    网络基础——网络协议
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11891848.html
Copyright © 2011-2022 走看看