Link to home
Start Free TrialLog in
Avatar of shean85
shean85

asked on

Draw Ellipse four control points

Hi ,
Please help me to draw an ellipse with known 2 diameters and 4 control points(start and end points of the biggest and smallest diameters).

I implemented some algorithm, but I have a problem that in some cases it doesn't work correctly.(example : some of the diameters equal to zero.)


for(float j=-a; j<=a; j=j+1.f) //use the formula for the ellipse to generate the points and draw them on the screen
      {
         float x0 = center_x;
         float y0 = center_y;
      
         float x1 = center_x + (j);
            
         float y1 =0.f;
         if (b==0 || a==0)
                  y1 = center_y;
         else
              y1 = center_y + (float)(sqrt((b*b)-((b*b*j*j)/(a*a))));
       
       float deltax = x1 - x0;
         float deltay = y0 - y1;

         float dis = (float)sqrt((deltax*deltax)+(deltay*deltay));
            
         float c_dis = (float)dis;
         float s_dis = (float)dis;
            
       float pi = (float)acos(-1);
            
       
         //-----------------------------------------------------------------------            
         if(deltay !=0 || deltax!=0)
         {
                    
            float angle;
              if(deltay ==0)
                    angle = atanf(deltax);
              else            
                  angle = (float)atan(deltax/deltay);

            if(deltay <0  && deltax >=0)
              {
                  if (deltax == 0)
                              angle = (float)(pi/2.0) + (float)fabs((atan(deltay)) ) ;
                        else
                              angle = (float)(pi/2.0) + (float)fabs((atan(deltay/deltax)) ) ;
              }

         
              if(deltay <0 && deltax <=0)
              {
                angle = (float)fabs(atan(deltax/deltay)) + (float)(pi);
               
              }
        
         if(deltay >0 && deltax <=0)
             {
                   angle = (float)atan(deltax/deltay) ;
                         
             }

           //---------------------------------------------------------------------

         x1 = (float)x0 + (float)(dis*sin(angle+orient+angle_90));
             y1 = (float)y0 - (float)(dis*cos(angle+orient+angle_90));

         }//end of if (deltay !=0 || deltax!=0)
             
       if(count>0)
         {
              pDC->MoveTo(endpoint.x,endpoint.y);
              find_max_min_x_y_point(endpoint.x,endpoint.y);
            pDC->LineTo((int)(x1+0.5),(int)(y1+0.5));
                      }

       endpoint.x = (int)(x1+0.5);
         endpoint.y = (int)(y1+0.5);
                  count++;

         x0 = center_x;
         y0 = center_y;

         x1 = center_x + (j);
             
         if (b==0||a==0)
               y1= center_y;
         else
             y1 = center_y - (float)(sqrt((b*b)-((b*b*j*j)/(a*a))));
       
       deltax = x1 - x0;
         deltay = y0 - y1;

         dis = (float)sqrt((deltax*deltax)+(deltay*deltay));
            
         c_dis = (float)dis;
         s_dis = (float)dis;
            
       pi = (float)acos(-1);
            
       
         //-----------------------------------------------------------------------            
         if(deltay !=0 || deltax!=0)
         {
                    
             float angle =0.f;
               if(deltay ==0)
                    angle = atanf(deltax);
               else            
                        angle = (float)atan(deltax/deltay);

             if(deltay <0  && deltax >=0)
               {
                   if (deltax == 0)
                              angle = (float)(pi/2.0) + (float)fabs((atan(deltay)) ) ;
                        else
                              angle = (float)(pi/2.0) + (float)fabs((atan(deltay/deltax)) ) ;
               }

           if(deltay <0 && deltax <=0)
               {
                       angle = (float)fabs(atan(deltax/deltay)) + (float)(pi);
               }
        
       
               if(deltay >0 && deltax <=0)
               {
               
                     angle = (float)atan(deltax/deltay) ;
               }

             //---------------------------------------------------------------------

               x1 = (float)x0 + (float)(dis*sin(angle+orient+angle_90));
               y1 = (float)y0 - (float)(dis*cos(angle+orient+angle_90));

         }//end of if (deltay !=0 || deltax!=0)
             
         if(count == 1)
         {
                  endpoint2.x = (int)x1;
                  endpoint2.y = (int)y1;
                  
         }
       if(count>0)
         {
             pDC->MoveTo(endpoint2.x,endpoint2.y);
               find_max_min_x_y_point(endpoint2.x,endpoint2.y);
             pDC->LineTo((int)(x1+0.5),(int)(y1+0.5));
                        }

       endpoint2.x = (int)(x1+0.5);
         endpoint2.y = (int)(y1+0.5);
                  count++;


      }//end of for(j=-a....

Thank you
Avatar of AlexFM
AlexFM

Use CDC::Ellipse function:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cdc.3a3a.ellipse.asp

Having four points (start and end points of the biggest and smallest diameters), you can easily find points required by Ellipse function:

BOOL Ellipse( int x1, int y1, int x2, int y2 );


Avatar of shean85

ASKER

My diameters not always parallel to the X or Y axis

Thank you
Avatar of AndyAinscow
Test the diameters.  If one is zero just draw a line between the other two end points (because that is what the ellipse would be).
Use coordinate transformations. See: SetWorldTransform.
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial