View Full Version : Get Center of the Ellipse

Stefano M.

08-26-2011, 08:09 AM

I have below function for drawing an ellipse. I would like to return the center of the circle because I would draw circle in Ellipse. I don't know how to get center of the circle.

Please your help is appreciated

void drawEllipse(float radiusX, float radiusY) {

int i;

glBegin(GL_LINE_LOOP);

for(i=0; i<360; i++) {

float rad = i*DEG2RAD;

glVertex2f(cos(rad)*radiusX,

sin(rad)*radiusY);

}

glEnd();

}

BionicBytes

08-26-2011, 08:14 AM

Cos and sin will vary between -1 and +1 across the 0-360 degree range you have specified.

Therefore the centre of the circle is implied as (0,0)

Presumably you use glTranslate2f before drawing this circle?

If so, then that IS the centre of the circle.

mobeen

08-26-2011, 10:49 AM

You can offset the position (by either issuing the translate call as BionicBytes suggested or you may center the ellipse to the given position by adding the center of ellipse to the x and y value like this,

void drawEllipse(float radiusX, float radiusY, float xCenter, float yCenter) {

int i;

glBegin(GL_LINE_LOOP);

for(i=0; i<360; i++) {

float rad = i*DEG2RAD;

glVertex2f(xCenter + radiusX*cos(rad),

yCenter + radiusY*sin(rad));

}

glEnd();

}

See if this is what u want?

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.