Circle.h

Go to the documentation of this file.
00001 
00009 #ifndef _MESHLIB_CIRCLE_H_
00010 #define _MESHLIB_CIRCLE_H_
00011 
00012 
00013 namespace MeshLib{
00014 
00020 class CCircle
00021 {
00022         public:
00026     CCircle(){ m_c = CPoint2(0,0); m_r = 1.0; };
00030     CCircle( const CPoint2 & c, const double r ) {      m_c = c; m_r = r;};
00034     ~CCircle(){};
00035         
00036 
00040         CPoint2 & c() { return m_c; };
00041 
00045         double & r() { return  m_r; };
00046 
00047         private:
00051                 CPoint2 m_c;
00055                 double m_r;
00056 
00057 };
00058 
00062 inline CCircle orthogonal(  CCircle  C[3] )
00063 {
00064 
00065         //The cricle C_i is : <p,p> - 2<p,c_i> = d_i
00066         double d[3];
00067         for( int i = 0; i < 3; i ++ )
00068         {
00069                 d[i] = C[i].r()*C[i].r() - mag2( C[i].c());
00070         }
00071         
00072         //the common chord lines are <p,dc_k> = h_k
00073         CPoint2 dc[2];
00074         
00075         dc[0] = C[1].c() - C[0].c();
00076         dc[1] = C[2].c() - C[1].c();
00077 
00078         double  h[2];
00079         h[0] = ( d[0] - d[1] )/2.0;
00080         h[1] = ( d[1] - d[2] )/2.0;
00081 
00082         double m[2][2];
00083 
00084         for( int i = 0; i < 2; i ++ )
00085         for( int j = 0; j < 2; j ++ )
00086         {
00087                 m[i][j] = dc[i]*dc[j];
00088         }
00089         
00090         double det = m[0][0] * m[1][1] - m[1][0] * m[0][1];
00091         double inv[2][2];
00092 
00093         inv[0][0] =  m[1][1]/det;
00094         inv[1][1] =  m[0][0]/det;
00095         inv[0][1] = -m[0][1]/det;
00096         inv[1][0] = -m[1][0]/det;
00097 
00098         double x[2];
00099         x[0] = inv[0][0] * h[0] + inv[0][1] * h[1];
00100         x[1] = inv[1][0] * h[0] + inv[1][1] * h[1];
00101 
00102         CPoint2 center = dc[0] * x[0] + dc[1] * x[1];
00103 
00104         double radius = sqrt( mag2(center-C[0].c()) - C[0].r()*C[0].r() );
00105         
00106         //printf("%f %f %f\n", radius, sqrt( mag2(center-C[1].c()) - C[1].r()*C[1].r() ), sqrt( mag2(center-C[2].c()) - C[2].r()*C[2].r() ) );
00107         
00108         return CCircle( center, radius );
00109 };
00110 
00111 
00112 
00113 inline int _circle_circle_intersection(CCircle C0, CCircle C1, CPoint2 & p0, CPoint2 & p1 )
00114 {
00115 
00116         double x0 = C0.c()[0];
00117         double y0 = C0.c()[1]; 
00118         double r0 = C0.r();
00119                                                                                 
00120         double x1 = C1.c()[0];
00121         double y1 = C1.c()[1]; 
00122         double r1 = C1.r();
00123           
00124         double a, dx, dy, d, h, rx, ry;
00125         double x2, y2;
00126 
00127   /* dx and dy are the vertical and horizontal distances between
00128    * the circle centers.
00129    */
00130   dx = x1 - x0;
00131   dy = y1 - y0;
00132 
00133   /* Determine the straight-line distance between the centers. */
00134   d = sqrt((dy*dy) + (dx*dx));
00135 
00136   /* Check for solvability. */
00137   if (d > (r0 + r1))
00138   {
00139     /* no solution. circles do not intersect. */
00140     return 0;
00141   }
00142   if (d < abs(r0 - r1))
00143   {
00144     /* no solution. one circle is contained in the other */
00145     return 0;
00146   }
00147 
00148   /* 'point 2' is the point where the line through the circle
00149    * intersection points crosses the line between the circle
00150    * centers.  
00151    */
00152 
00153   /* Determine the distance from point 0 to point 2. */
00154   a = ((r0*r0) - (r1*r1) + (d*d)) / (2.0 * d) ;
00155 
00156   /* Determine the coordinates of point 2. */
00157   x2 = x0 + (dx * a/d);
00158   y2 = y0 + (dy * a/d);
00159 
00160   /* Determine the distance from point 2 to either of the
00161    * intersection points.
00162    */
00163   h = sqrt((r0*r0) - (a*a));
00164 
00165   /* Now determine the offsets of the intersection points from
00166    * point 2.
00167    */
00168   rx = -dy * (h/d);
00169   ry = dx * (h/d);
00170 
00171   /* Determine the absolute intersection points. */
00172   p0 = CPoint2( x2 + rx, y2+ry);
00173   p1 = CPoint2( x2 - rx, y2-ry);
00174 
00175   return 1;
00176 };
00177 
00178 
00179 }; //namespace
00180 
00181 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Defines