Line Segment Circle Intersection

前端 未结 5 985
刺人心
刺人心 2020-12-09 05:55

I am trying to determine the point at which a line segment intersect a circle. For example, given any point between P0 and P3 (And also assuming that you know the radius),

5条回答
  •  -上瘾入骨i
    2020-12-09 06:42

    Go for this code..its save the time

    private boolean circleLineIntersect(float x1, float y1, float x2, float y2, float cx, float cy, float cr ) {
          float dx = x2 - x1;
          float dy = y2 - y1;
          float a = dx * dx + dy * dy;
          float b = 2 * (dx * (x1 - cx) + dy * (y1 - cy));
          float c = cx * cx + cy * cy;
          c += x1 * x1 + y1 * y1;
          c -= 2 * (cx * x1 + cy * y1);
          c -= cr * cr;
          float bb4ac = b * b - 4 * a * c;
    
             // return false  No collision
             // return true Collision
          return bb4ac >= 0;
        }
    

提交回复
热议问题