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/d eltax)) ) ;
}
if(deltay <0 && deltax <=0)
{
angle = (float)fabs(atan(deltax/de ltay)) + (float)(pi);
}
if(deltay >0 && deltax <=0)
{
angle = (float)atan(deltax/deltay) ;
}
//------------------------ ---------- ---------- ---------- ---------- -----
x1 = (float)x0 + (float)(dis*sin(angle+orie nt+angle_9 0));
y1 = (float)y0 - (float)(dis*cos(angle+orie nt+angle_9 0));
}//end of if (deltay !=0 || deltax!=0)
if(count>0)
{
pDC->MoveTo(endpoint.x,end point.y);
find_max_min_x_y_point(end point.x,en dpoint.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/d eltax)) ) ;
}
if(deltay <0 && deltax <=0)
{
angle = (float)fabs(atan(deltax/de ltay)) + (float)(pi);
}
if(deltay >0 && deltax <=0)
{
angle = (float)atan(deltax/deltay) ;
}
//------------------------ ---------- ---------- ---------- ---------- -----
x1 = (float)x0 + (float)(dis*sin(angle+orie nt+angle_9 0));
y1 = (float)y0 - (float)(dis*cos(angle+orie nt+angle_9 0));
}//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,en dpoint2.y) ;
find_max_min_x_y_point(end point2.x,e ndpoint2.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
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
float deltax = x1 - x0;
float deltay = y0 - y1;
float dis = (float)sqrt((deltax*deltax
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/d
}
if(deltay <0 && deltax <=0)
{
angle = (float)fabs(atan(deltax/de
}
if(deltay >0 && deltax <=0)
{
angle = (float)atan(deltax/deltay)
}
//------------------------
x1 = (float)x0 + (float)(dis*sin(angle+orie
y1 = (float)y0 - (float)(dis*cos(angle+orie
}//end of if (deltay !=0 || deltax!=0)
if(count>0)
{
pDC->MoveTo(endpoint.x,end
find_max_min_x_y_point(end
pDC->LineTo((int)(x1+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
deltax = x1 - x0;
deltay = y0 - y1;
dis = (float)sqrt((deltax*deltax
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/d
}
if(deltay <0 && deltax <=0)
{
angle = (float)fabs(atan(deltax/de
}
if(deltay >0 && deltax <=0)
{
angle = (float)atan(deltax/deltay)
}
//------------------------
x1 = (float)x0 + (float)(dis*sin(angle+orie
y1 = (float)y0 - (float)(dis*cos(angle+orie
}//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,en
find_max_min_x_y_point(end
pDC->LineTo((int)(x1+0.5),
}
endpoint2.x = (int)(x1+0.5);
endpoint2.y = (int)(y1+0.5);
count++;
}//end of for(j=-a....
Thank you
ASKER
My diameters not always parallel to the X or Y axis
Thank you
Thank you
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 );