We help IT Professionals succeed at work.
Get Started

Draw Ellipse four control points

shean85
shean85 asked
on
303 Views
Last Modified: 2013-11-20
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
Comment
Watch Question
Software Architect
Top Expert 2008
Commented:
This problem has been solved!
Unlock 1 Answer and 5 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE