Click to See Complete Forum and Search --> : ellipse part drawing in BC 3.1
migol
December 26th, 2005, 05:28 PM
I know it's a bit newbie question, but I've got a big problem with this.
I have to draw an ellipse part using BC 3.1 and it's graphics.h library. It has a nice ellipse drawing function... but I can't rotate it. It's radiuses can be only a horizojntal and vertical line. So is here anybody who could help me and write a function that will draw rotated ellipse? It would be great if it wouldn't use any angles, but only points (but I won't mind if somebody would write it with angles of course :) ).
olivthill
December 27th, 2005, 02:55 AM
Borland 3.1 library of functions for drawing round shapes includes circle, ellipse, fillellipse, pislice, sector, and arc. There is no functions doing what you'd like to have. The solution would be to get the mathematical formula of your ellipse and plot it with putpixel().
migol
December 27th, 2005, 06:08 AM
and that's what I'm asking for.
I've also found an information, that this shape can be aproximated with drawing two arc using bisecton and recurrency, but i can find the formula for it.
olivthill
December 27th, 2005, 10:12 AM
Here the code for drawing a standard ellipse with Windows API:
int x, y, a, b, cx, cy; double i;
double pi;
pi = 3.1415926535897932;
a = 30; // width of ellipse
b = 20; // height of ellipse
cx = 200; // center for x
cy = 100; // center for y
for (i = 0; i < pi + pi; i += 0.01) {
x = (int)(a * cos(i)) + cx;
y = (int)(b * sin(i)) + cy;
SetPixel(hdc, x, y, RGB(255,0,0));
}
And here is the code for drawing a rotated ellipse
int x, y, a, b, cx, cy; double i;
double angle, radians, pi, cosval, sinval;
pi = 3.1415926535897932;
a = 30; // width of ellipse
b = 20; // height of ellipse
cx = 200; // center for x
cy = 100; // center for y
angle = 30; // angle in degrees
radians = (angle) / ((180.0 / pi));
for (i = 0; i < pi + pi; i += 0.01) {
cosval = cos(radians);
sinval = sin(radians);
x = (int)((double)a * cos(i) * cosval)
+ (int)((double)b * sin(i) * sinval)
+ cx;
y = (int)((double)-a * cos(i) * sinval)
+ (int)((double)b * sin(i) * cosval)
+ cy;
SetPixel(hdc, x, y, RGB(0,0,0));
}
migol
December 27th, 2005, 05:30 PM
and that's what i'm talking about. i'll try to implement this into my program.
cindyonlyone
January 2nd, 2006, 08:35 PM
You can use the following codes to split the ellipse to points:
void CreateEllipse(CRect &rcPos)
{
double dSqrt2 = (double)sqrt(2.0);
double dRadius1 = (double)rcPos.Width() / 2.0;
double dRadius2 = (double)rcPos.Height() / 2.0;
double dCenterX = dRadius1 + (double)rcPos.left;
double dCenterY = dRadius2 + (double)rcPos.top;
// Right
m_lpShapePoints[0].x = (long)(dRadius1/dSqrt2 + dCenterX);
m_lpShapePoints[0].y = (long)(dRadius2/dSqrt2 + dCenterY);
m_lpShapePoints[1].x = (long)(dRadius1*(8-dSqrt2)/6 + dCenterX);
m_lpShapePoints[1].y = (long)(dRadius2*(7*dSqrt2-8)/6 + dCenterY);
m_lpShapePoints[2].x = (long)(dRadius1*(8-dSqrt2)/6 + dCenterX);
m_lpShapePoints[2].y = (long)(-dRadius2*(7*dSqrt2-8)/6 + dCenterY);
m_lpShapePoints[3].x = (long)(dRadius1/dSqrt2 + dCenterX);
m_lpShapePoints[3].y = (long)(-dRadius2/dSqrt2 + dCenterY);
// Bottom
m_lpShapePoints[4].x = (long)(dRadius1*(7*dSqrt2-8)/6 + dCenterX);
m_lpShapePoints[4].y = (long)(-dRadius2*(8-dSqrt2)/6 + dCenterY);
m_lpShapePoints[5].x = (long)(-dRadius1*(7*dSqrt2-8)/6 + dCenterX);
m_lpShapePoints[5].y = (long)(-dRadius2*(8-dSqrt2)/6 + dCenterY);
m_lpShapePoints[6].x = (long)(-dRadius1/dSqrt2 + dCenterX);
m_lpShapePoints[6].y = (long)(-dRadius2/dSqrt2 + dCenterY);
// Left
m_lpShapePoints[7].x = (long)(-dRadius1*(8-dSqrt2)/6 + dCenterX);
m_lpShapePoints[7].y = (long)(-dRadius2*(7*dSqrt2-8)/6 + dCenterY);
m_lpShapePoints[8].x = (long)(-dRadius1*(8-dSqrt2)/6 + dCenterX);
m_lpShapePoints[8].y = (long)(dRadius2*(7*dSqrt2-8)/6 + dCenterY);
m_lpShapePoints[9].x = (long)(-dRadius1/dSqrt2 + dCenterX);
m_lpShapePoints[9].y = (long)(dRadius2/dSqrt2 + dCenterY);
// Top
m_lpShapePoints[10].x = (long)(-dRadius1*(7*dSqrt2-8)/6 + dCenterX);
m_lpShapePoints[10].y = (long)(dRadius2*(8-dSqrt2)/6 + dCenterY);
m_lpShapePoints[11].x = (long)(dRadius1*(7*dSqrt2-8)/6 + dCenterX);
m_lpShapePoints[11].y = (long)(dRadius2*(8-dSqrt2)/6 + dCenterY);
m_lpShapePoints[12].x = (long)(dRadius1/dSqrt2 + dCenterX);
m_lpShapePoints[12].y = (long)(dRadius2/dSqrt2 + dCenterY);
}
And then drawing it with PolyBizer of GDI API,now your ellipse can be rotated at any angle.
Hope this is useful to you.
Cindy
--------------------------------------------------------------------------
For high quality flow/diagram MFC/C++ Visio Like visualization Source Code,download XD++ at:
http://www.********.net
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.