09-20-2002, 09:06 AM

I have referred to many books, but none say how to draw a circle not centered at the origin...any has any ideas?

View Full Version : simple circle

09-20-2002, 09:06 AM

I have referred to many books, but none say how to draw a circle not centered at the origin...any has any ideas?

Deiussum

09-20-2002, 09:48 AM

Same way you do it at the origin, but for each calculated point you add the center's x,y value to the calculated x,y value.

nexusone

09-20-2002, 11:05 AM

One easy solution is to draw it and then translate it to the position you want...

The math function is:

where offset is the XY location.

also note the angle is in rads and not degrees.

x = offset_X + radius * cos( angle );

y = offset_Y + radius * sin( angle );

Originally posted by xypher:

I have referred to many books, but none say how to draw a circle not centered at the origin...any has any ideas?

The math function is:

where offset is the XY location.

also note the angle is in rads and not degrees.

x = offset_X + radius * cos( angle );

y = offset_Y + radius * sin( angle );

Originally posted by xypher:

I have referred to many books, but none say how to draw a circle not centered at the origin...any has any ideas?

Deiussum

09-20-2002, 11:18 AM

Isn't that basically what I said?

BTW, if you are drawing a circle by using GL_LINES, the sin/cos method is probably fine, but if you are doing something like drawing circles to procedural textures, you're probably better off using something like the midpoint circle algorithm.

BTW, if you are drawing a circle by using GL_LINES, the sin/cos method is probably fine, but if you are doing something like drawing circles to procedural textures, you're probably better off using something like the midpoint circle algorithm.

jono_123

09-21-2002, 08:42 PM

or you could always use the other general equation for a cricle:

x^2 + y^2 = r^2 where r is the radius

x^2 + y^2 = r^2 where r is the radius

09-23-2002, 03:40 AM

I am drawing my circle in this fashion:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

The above works fine

But when I try to make it not at the origin but with center (5,5):

I do the following:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f(5.0+ cos(angle), 5.0+ sin(angle));

}

glEnd();

But the circle just disappears from the window...

why would it be so?

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

The above works fine

But when I try to make it not at the origin but with center (5,5):

I do the following:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f(5.0+ cos(angle), 5.0+ sin(angle));

}

glEnd();

But the circle just disappears from the window...

why would it be so?

09-23-2002, 03:47 AM

also how would i be able to draw a radius inside the circle...(just a line starting from the new center to the circumference at any angle)

nexusone

09-23-2002, 04:05 AM

Do you mean that it is not drawn?

or as you rotate the circle is disapears?

How big is your viewing area?

or you could post you view setup.

Originally posted by xypher:

I am drawing my circle in this fashion:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

The above works fine

But when I try to make it not at the origin but with center (5,5):

I do the following:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f(5.0+ cos(angle), 5.0+ sin(angle));

}

glEnd();

But the circle just disappears from the window...

why would it be so?

or as you rotate the circle is disapears?

How big is your viewing area?

or you could post you view setup.

Originally posted by xypher:

I am drawing my circle in this fashion:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

The above works fine

But when I try to make it not at the origin but with center (5,5):

I do the following:

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f(5.0+ cos(angle), 5.0+ sin(angle));

}

glEnd();

But the circle just disappears from the window...

why would it be so?

09-23-2002, 04:18 AM

it is not drawn at all, here is what i am doing:

#define PI 3.1415926535898

GLint circle_points = 300;

int angle, i;

void display(void)

{

glClear (GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

glColor3f (1.0, 1.0, 1.0);

glTranslatef (5.0,5.0,0.0);

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

glTranslatef (5.0,5.0,0.0);

glFlush(); // displays on the screen

}

void reshape (int w, int h)

{

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity ();

gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);

}

void init (void)

{

glClearColor (0.0, 0.0, 0.0, 0.0);

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (300,300);

glutInitWindowPosition (200, 200);

init ();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

return 1;

}

#define PI 3.1415926535898

GLint circle_points = 300;

int angle, i;

void display(void)

{

glClear (GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

glColor3f (1.0, 1.0, 1.0);

glTranslatef (5.0,5.0,0.0);

// draws a cricle

glBegin(GL_LINE_LOOP);

for (i = 0; i < circle_points; i++)

{

angle = 2*PI*i/circle_points;

glVertex2f( cos(angle), sin(angle));

}

glEnd();

glTranslatef (5.0,5.0,0.0);

glFlush(); // displays on the screen

}

void reshape (int w, int h)

{

glViewport (0, 0, (GLsizei) w, (GLsizei) h);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity ();

gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);

}

void init (void)

{

glClearColor (0.0, 0.0, 0.0, 0.0);

}

int main(int argc, char** argv)

{

glutInit(&argc, argv);

glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);

glutInitWindowSize (300,300);

glutInitWindowPosition (200, 200);

init ();

glutDisplayFunc(display);

glutReshapeFunc(reshape);

glutMainLoop();

return 1;

}

Deiussum

09-23-2002, 05:07 AM

You should be setting the matrix mode to GL_PROJECTION in your reshape, not GL_MODELVIEW. Set it to GL_MODELVIEW again AFTER the gluOrtho2D.

09-23-2002, 05:21 AM

that makes the situation worse,

i just get the circle on the screen for a glance, and then it dissapears..

so I leave it the way it is.

But the moment i put in a glTranlatef command , the circle dissapears...it seems quite strange to me.

i just get the circle on the screen for a glance, and then it dissapears..

so I leave it the way it is.

But the moment i put in a glTranlatef command , the circle dissapears...it seems quite strange to me.

Deiussum

09-23-2002, 06:06 AM

You left the glLoadIdentity at the beginning of your display function, didn't you? If you didn't, your glTranslate is going to incrementally translate your circle every frame, which would eventually move it off the screen.

09-25-2002, 11:06 PM

Here is the partial source code that draws Ellipse or Circle in my Opengl Cad Application.

point1(CPoint) is the first point and

MouseDownPoint(CPoint) is the second point in the rectangle.

if polygon = TRUE then the program fills the ellipse

else draw the lines of ellipse.

You can increase the line fragments in this line from 30 to more number.

for (int i = 0; i <= 30; i++)

glEvalCoord1d((double)i / 30.0);

By decreasing the line fragments , you can see the lines are distributed equally.

This code seems complicated , but is better then Circle equation we learned in high school.

The source code is based on Bezier curve.

See the red book for Evaluators ...

I hope this would help you,

I believe this is the best way to draw ellipse.

void CDrawing: http://www.opengl.org/discussion_boards/ubb/biggrin.gifrawEllipse(BOOL polygon)

{

double *ctrlpoints = new double[39];

CPoint point1,m_MouseDownPoint;

point1.x = data[0];

point1.y = data[1];

m_MouseDownPoint.x = data[2];

m_MouseDownPoint.y = data[3];

ctrlpoints[0] = (point1.x + m_MouseDownPoint.x) / 2.0;

ctrlpoints[1] = point1.y;

ctrlpoints[2] = 0.0;

ctrlpoints[3] = (point1.x + ctrlpoints[0]) / 2.0;

ctrlpoints[4] = point1.y;

ctrlpoints[5] = 0.0;

ctrlpoints[6] = point1.x;

ctrlpoints[7] = (point1.y + (point1.y + m_MouseDownPoint.y) / 2.0) / 2.0;

ctrlpoints[8] = 0.0;

ctrlpoints[9] = point1.x;

ctrlpoints[10] = (point1.y + m_MouseDownPoint.y) / 2.0;

ctrlpoints[11] = 0.0;

ctrlpoints[12] = point1.x;

ctrlpoints[13] = ( (point1.y + m_MouseDownPoint.y) / 2.0 + m_MouseDownPoint.y ) / 2.0;

ctrlpoints[14] = 0.0;

ctrlpoints[15] = (point1.x + ctrlpoints[0]) / 2.0;

ctrlpoints[16] = m_MouseDownPoint.y;

ctrlpoints[17] = 0.0;

ctrlpoints[18] = ctrlpoints[0];

ctrlpoints[19] = m_MouseDownPoint.y;

ctrlpoints[20] = 0.0;

ctrlpoints[21] = (ctrlpoints[0] + m_MouseDownPoint.x) / 2.0;

ctrlpoints[22] = m_MouseDownPoint.y;

ctrlpoints[23] = 0.0;

ctrlpoints[24] = m_MouseDownPoint.x;

ctrlpoints[25] = ( (point1.y + m_MouseDownPoint.y) / 2.0 + m_MouseDownPoint.y ) / 2.0;

ctrlpoints[26] = 0.0;

ctrlpoints[27] = m_MouseDownPoint.x;

ctrlpoints[28] = (point1.y + m_MouseDownPoint.y) / 2.0;

ctrlpoints[29] = 0.0;

ctrlpoints[30] = m_MouseDownPoint.x;

ctrlpoints[31] = (point1.y + (point1.y + m_MouseDownPoint.y) / 2.0) / 2.0;

ctrlpoints[32] = 0.0;

ctrlpoints[33] = (m_MouseDownPoint.x + (point1.x + m_MouseDownPoint.x)/2.0 ) / 2.0;

ctrlpoints[34] = point1.y;

ctrlpoints[35] = 0.0;

ctrlpoints[36] = ctrlpoints[0];

ctrlpoints[37] = ctrlpoints[1];

ctrlpoints[38] = 0.0;

glEnable(GL_MAP1_VERTEX_3);

for(int k = 0; k < 4 ;k++)

{

glMap1d(GL_MAP1_VERTEX_3,0.0,1.0,3,4,&ctrlpoints[k*9]);

glEnable(GL_MAP1_VERTEX_3);

if(polygon)

{

glBegin(GL_POLYGON);

glVertex3d( (point1.x + m_MouseDownPoint.x) / 2.0 ,

(point1.y + m_MouseDownPoint.y) / 2.0 , 0.0);

}

else

{

glBegin(GL_LINE_STRIP);

}

for (int i = 0; i <= 30; i++)

glEvalCoord1d((double)i / 30.0);

glEnd();

}

glDisable(GL_MAP1_VERTEX_3);

delete [] ctrlpoints;

}

point1(CPoint) is the first point and

MouseDownPoint(CPoint) is the second point in the rectangle.

if polygon = TRUE then the program fills the ellipse

else draw the lines of ellipse.

You can increase the line fragments in this line from 30 to more number.

for (int i = 0; i <= 30; i++)

glEvalCoord1d((double)i / 30.0);

By decreasing the line fragments , you can see the lines are distributed equally.

This code seems complicated , but is better then Circle equation we learned in high school.

The source code is based on Bezier curve.

See the red book for Evaluators ...

I hope this would help you,

I believe this is the best way to draw ellipse.

void CDrawing: http://www.opengl.org/discussion_boards/ubb/biggrin.gifrawEllipse(BOOL polygon)

{

double *ctrlpoints = new double[39];

CPoint point1,m_MouseDownPoint;

point1.x = data[0];

point1.y = data[1];

m_MouseDownPoint.x = data[2];

m_MouseDownPoint.y = data[3];

ctrlpoints[0] = (point1.x + m_MouseDownPoint.x) / 2.0;

ctrlpoints[1] = point1.y;

ctrlpoints[2] = 0.0;

ctrlpoints[3] = (point1.x + ctrlpoints[0]) / 2.0;

ctrlpoints[4] = point1.y;

ctrlpoints[5] = 0.0;

ctrlpoints[6] = point1.x;

ctrlpoints[7] = (point1.y + (point1.y + m_MouseDownPoint.y) / 2.0) / 2.0;

ctrlpoints[8] = 0.0;

ctrlpoints[9] = point1.x;

ctrlpoints[10] = (point1.y + m_MouseDownPoint.y) / 2.0;

ctrlpoints[11] = 0.0;

ctrlpoints[12] = point1.x;

ctrlpoints[13] = ( (point1.y + m_MouseDownPoint.y) / 2.0 + m_MouseDownPoint.y ) / 2.0;

ctrlpoints[14] = 0.0;

ctrlpoints[15] = (point1.x + ctrlpoints[0]) / 2.0;

ctrlpoints[16] = m_MouseDownPoint.y;

ctrlpoints[17] = 0.0;

ctrlpoints[18] = ctrlpoints[0];

ctrlpoints[19] = m_MouseDownPoint.y;

ctrlpoints[20] = 0.0;

ctrlpoints[21] = (ctrlpoints[0] + m_MouseDownPoint.x) / 2.0;

ctrlpoints[22] = m_MouseDownPoint.y;

ctrlpoints[23] = 0.0;

ctrlpoints[24] = m_MouseDownPoint.x;

ctrlpoints[25] = ( (point1.y + m_MouseDownPoint.y) / 2.0 + m_MouseDownPoint.y ) / 2.0;

ctrlpoints[26] = 0.0;

ctrlpoints[27] = m_MouseDownPoint.x;

ctrlpoints[28] = (point1.y + m_MouseDownPoint.y) / 2.0;

ctrlpoints[29] = 0.0;

ctrlpoints[30] = m_MouseDownPoint.x;

ctrlpoints[31] = (point1.y + (point1.y + m_MouseDownPoint.y) / 2.0) / 2.0;

ctrlpoints[32] = 0.0;

ctrlpoints[33] = (m_MouseDownPoint.x + (point1.x + m_MouseDownPoint.x)/2.0 ) / 2.0;

ctrlpoints[34] = point1.y;

ctrlpoints[35] = 0.0;

ctrlpoints[36] = ctrlpoints[0];

ctrlpoints[37] = ctrlpoints[1];

ctrlpoints[38] = 0.0;

glEnable(GL_MAP1_VERTEX_3);

for(int k = 0; k < 4 ;k++)

{

glMap1d(GL_MAP1_VERTEX_3,0.0,1.0,3,4,&ctrlpoints[k*9]);

glEnable(GL_MAP1_VERTEX_3);

if(polygon)

{

glBegin(GL_POLYGON);

glVertex3d( (point1.x + m_MouseDownPoint.x) / 2.0 ,

(point1.y + m_MouseDownPoint.y) / 2.0 , 0.0);

}

else

{

glBegin(GL_LINE_STRIP);

}

for (int i = 0; i <= 30; i++)

glEvalCoord1d((double)i / 30.0);

glEnd();

}

glDisable(GL_MAP1_VERTEX_3);

delete [] ctrlpoints;

}

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