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

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.

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?

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.

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

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

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?

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)

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:

[b]I am drawing my circle in this fashion:

// draws a cricle

glBegin(GL_LINE_LOOP);

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

{

angle = 2PIi/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 = 2PIi/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?[/b]

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;

}

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.

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.

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.

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: rawEllipse(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;
```

}