分享

线段和圆/球的相交性检测

 勤奋不止 2016-12-05
  1. #include "iostream"  
  2. #include "math.h"  
  3. using namespace std;  
  4.   
  5. #define EPS 0.00001  
  6.   
  7. struct pointf   
  8. {  
  9.     float x;  
  10.     float y;  
  11.   
  12.     pointf()  
  13.     {  
  14.   
  15.     }  
  16.     pointf(float m, float n) : x(m),y(n)  
  17.     {  
  18.     }  
  19. };  
  20.   
  21. /** 
  22. * @brief 求线段与圆的交点 
  23. * @return 如果有交点返回true,否则返回false 
  24. * @note 与圆可能存在两个交点,如果存在两个交点在ptInter1和ptInter2都为有效值,如果有一个交点,则ptInter2的值为 
  25. *       无效值,此处为65536.0 
  26. */  
  27. bool LineInterCircle(  
  28.                 const pointf ptStart, // 线段起点  
  29.                 const pointf ptEnd, // 线段终点  
  30.                 const pointf ptCenter, // 圆心坐标  
  31.                 const float Radius,  
  32.                 pointf& ptInter1,  
  33.                 pointf& ptInter2)  
  34. {  
  35.     ptInter1.x = ptInter2.x = 65536.0f;  
  36.     ptInter2.y = ptInter2.y = 65536.0f;  
  37.   
  38.     float fDis = sqrt((ptEnd.x - ptStart.x) * (ptEnd.x - ptStart.x) + (ptEnd.y - ptStart.y) * (ptEnd.y - ptStart.y));  
  39.       
  40.     pointf d;  
  41.     d.x = (ptEnd.x - ptStart.x) / fDis;  
  42.     d.y = (ptEnd.y - ptStart.y) / fDis;  
  43.   
  44.     pointf E;  
  45.     E.x = ptCenter.x - ptStart.x;  
  46.     E.y = ptCenter.y - ptStart.y;  
  47.   
  48.     float a = E.x * d.x + E.y * d.y;  
  49.     float a2 = a * a;  
  50.   
  51.     float e2 = E.x * E.x + E.y * E.y;  
  52.   
  53.     float r2 = Radius * Radius;  
  54.   
  55.     if ((r2 - e2 + a2) < 0)  
  56.     {  
  57.         return false;  
  58.     }  
  59.     else  
  60.     {  
  61.         float f = sqrt(r2 - e2 + a2);  
  62.   
  63.         float t = a - f;  
  64.   
  65.         if( ((t - 0.0) > - EPS) && (t - fDis) < EPS)  
  66.         {  
  67.             ptInter1.x = ptStart.x + t * d.x;  
  68.             ptInter1.y = ptStart.y + t * d.y;  
  69.         }         
  70.   
  71.         t = a + f;  
  72.   
  73.         if( ((t - 0.0) > - EPS) && (t - fDis) < EPS)  
  74.         {  
  75.             ptInter2.x = ptStart.x + t * d.x;  
  76.             ptInter2.y = ptStart.y + t * d.y;  
  77.         }  
  78.   
  79.         return true;  
  80.     }  
  81. }  
  82.   
  83. void main(void)  
  84. {  
  85.     pointf ptStart(3.0, 2.0);  
  86.     pointf ptEnd(10.0, 6.0);  
  87.   
  88.     pointf ptCenter(10.0, 4.0);  
  89.     float fR = 3.0;  
  90.   
  91.     pointf pt1, pt2;  
  92.   
  93.     if (!LineInterCircle(ptStart, ptEnd, ptCenter, fR, pt1, pt2))  
  94.     {  
  95.         cout<<"不相交!"<<endl;  
  96.     }  
  97.     else  
  98.     {  
  99.         cout<<"交点"<<endl;  
  100.         cout<<pt1.x<<" "<<pt1.y<<endl;  
  101.   
  102.         cout<<pt2.x<<" "<<pt2.y<<endl;  
  103.     }  
  104.       
  105. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约