title: Circle Line Intersection points
body: <pre><code>public static ArrayList&lt;IntPoint&gt; getCircleLineIntersectionPoint(IntPoint pointA, IntPoint pointB, IntPoint center, int radius) {
      // returns a list of intersection points between a line which passes through given points,
      // pointA and pointB, and a circle described by given radius and center coordinate
  
      double disc, A, B, C, slope, c;
      double x1, x2, y1, y2;
      IntPoint point1, point2;
      ArrayList&lt;IntPoint&gt; intersections = new ArrayList&lt;IntPoint&gt;();  
      try{
          slope = Util.calculateSlope(pointA, pointB);
      }catch (UndefinedSlopeException e){         
          C =  Math.pow(center.y, 2) + Math.pow(pointB.x, 2) - 2 * pointB.x * center.x + Math.pow(center.x, 2) - Math.pow(radius, 2);
          B = -2 * center.y;
          A = 1;
          disc = Math.pow(B, 2) - 4 * 1 * C;
          if (disc &lt; 0){
              return intersections;
          }
          else{
              y1 = (-B + Math.sqrt(disc)) / (2 * A);
              y2 = (-B - Math.sqrt(disc)) / (2 * A);
  
              x1 = pointB.x;
              x2 = pointB.x;
          }
          point1 = new IntPoint((int)x1, (int)y1);
          point2 = new IntPoint((int)x2, (int)y2);
          if (Util.euclideanDistance(pointA,  point2) &gt; Util.euclideanDistance(pointA, point1)){
              intersections.add(point1);
          }
          else{
              intersections.add(point2);
          }
          return intersections;
      }
      if (slope == 0){
          C =  Math.pow(center.x, 2)  + Math.pow(center.y, 2) + Math.pow(pointB.y, 2) - 2 * pointB.y * center.y  - Math.pow(radius, 2);
          B = -2 * center.x;
          A = 1;
          disc = Math.pow(B, 2) - 4 * 1 * C;
          if (disc &lt; 0){
              return intersections;
          }
          else{
              x1 = (-B + Math.sqrt(disc)) / (2*A);
              x2 = (-B - Math.sqrt(disc)) / (2*A);
              y1 = pointB.y;
              y2 = pointB.y;
          }
      }
      else{
          c = slope * pointA.x + pointA.y;
          B = (2 * center.x + 2 * center.y * slope  + 2 * c * slope);
          A = 1 + Math.pow(slope, 2);
          C = (Math.pow(center.x, 2) + Math.pow(c, 2) + 2 * center.y * c + Math.pow(center.y, 2) - Math.pow(radius, 2));
          disc = Math.pow(B, 2) - (4 * A * C);
  
          if (disc &lt; 0){
              return intersections;
          }
          else{
              x1 = (-B + Math.sqrt(disc)) / (2 * A);
              x2 = (-B - Math.sqrt(disc)) / (2 * A);
  
              y1 = slope * x1 - c;
              y2 = slope * x2 - c;
          }
      }
  
      point1 = new IntPoint((int)x1, (int)y1);
      point2 = new IntPoint((int)x2, (int)y2);
      if (Util.euclideanDistance(pointA,  point2) &gt; Util.euclideanDistance(pointA, point1)){
          //if (Util.angleBetween(pointA, pointB, point1) &lt; Math.PI/2){
              intersections.add(point1);
          //}
      }
      else{
          //if (Util.angleBetween(pointA, pointB, point1) &lt; Math.PI/2){
              intersections.add(point2);
          //}
      }       
      return intersections;
  }
  </code></pre>
  
  <p>I am using the above algorithm to test for intersection between a circle and a line. It works fine sometimes but at other times it fails. The code represents the equation which is derived from solving for x simultaneously from circle and line equations <code>(x-a)^+(y-b)^2=r^2</code> and <code>y = mx - mx1 + y1</code>. Has anyone got an idea where I am going wrong either in my maths or elsewhere?</p>