Wednesday, 25 March 2015

Intersection of circles implementation in C++

Intersection of circles implementation in Cpp:

Two circles may intersect in two imaginary points, a single degenerate point, or two distinct points.
The intersections of two circles determine a line known as the radical line. If three circles mutually intersect in a single point, their point of intersection is the intersection of their pairwise radical lines, known as the radical center.

Intersection cicles

The below sample code tells us how to find the intersection point of the three circles.

#include "stdafx.h"
#include <iostream>
#include <float.h>
#include <stdlib.h>
#include <math.h>
using namespace std;
double _EPSILON_CONST_ = 0.000001;
bool Cal3CircleIntersection (double dX0, double dY0, double dDist0,
                             double dX1, double dY1, double dDist1,
                             double dX2, double dY2, double dDist2)
{
    double a= 0.0, dX = 0.0, dY= 0.0, dDistTemp= 0.0, h = 0.0, dRX= 0.0, dRY= 0.0;
    double dResX= 0.0, dResY= 0.0;
 
  /* dX and dY are the vertical and horizontal distances between the circle centers. */
    dX = dX1 - dX0; dY = dY1 - dY0;

    /* Calculate the straight-line distance between the centers. */
    dDistTemp = sqrt((dY*dY) + (dX*dX));
    cout << dDistTemp;

    /* Check for solvability. */
    if (dDistTemp > (dDist0 + dDist1))
      { return false; }
    else if (dDistTemp < abs(dDist0 - dDist1))
      { return false; }

    /* 'point 2' is the point where the line through the circle

    * intersection points crosses the line between the circle

    * centers.

    */

    /* Calculate the distance from point 0 to point 2. */

    a = ((dDist0*dDist0) - (dDist1*dDist1) + (dDistTemp*dDistTemp)) / (2.0 * dDistTemp) ;

    /* Calculate the coordinates of point 2. */

    dResX = dX0 + (dX * a/dDistTemp);

    dResY = dY0 + (dY * a/dDistTemp);

    /* Calculate the distance from point 2 to either of the intersection points. */

    h = sqrt((dDist0*dDist0) - (a*a));

    /* Now Calculate the offsets of the intersection points from

    * point 2.

    */

    dRX = -dY * (h/dDistTemp);

    dRY = dX * (h/dDistTemp);

    /* Calculate the absolute intersection points. */

    double dIntersect_X1 = dResX + dRX;

    double dIntersect_X2 = dResX - dRX;

    double dIntersect_Y1 = dResY + dRY;

    double dIntersect_Y2 = dResY - dRY;

    /* Lets Calculate if circle 3 intersects at either of the above intersection points. */

    dX = dIntersect_X1 - dX2;

    dY = dIntersect_Y1 - dY2;

    double d1 = sqrt ((dY * dY) + (dX * dX));

    dX = dIntersect_X2 - dX2;

    dY = dIntersect_Y2 - dY2;

    double d2 = sqrt ((dY * dY) + (dX * dX));

    if(abs (d1 - dDist2) < _EPSILON_CONST_)

      { cout << dIntersect_X1 << "\t" << dIntersect_Y1 << "\n"; }

    else if(abs (d2 - dDist2) < _EPSILON_CONST_)

      { cout << dIntersect_X2 << "\t" << dIntersect_Y2 << "\n"; }

    else

      { cout << "INTERSECTION Circle1 AND Circle2 AND Circle3:"; }

    return true;

}


Test the function using some values by calling it in the main
int main ()
{

   Cal3CircleIntersection (0.0, 0.0, 2.0,   // circle 1 (dXCent1, center_y, radius)

                           0.0, 10.0, 8.0,  // circle 2 (dXCent1, center_y, radius)

                           0.0, 11.0, 9.0); // circle 3 (dXCent1, center_y, radius)
    getchar ();

    return 0;

}


No comments: