Skip to main content
gamedev.net
🔒 Locked

Basic - Drawing a Circle

Started by learnopengl Aug 22, 2008 at 2:36 PM 4 replies 800+ views
Original Post
learnopengl
learnopengl
Hello, I am a newbie. Please help me draw the following using opengl (C/C++) 1) Circle 2) Archimedian .. Thank You
bitshifter
bitshifter
To render a circle:
#include <math.h>#define MATH_PI 3.141593F#define NUM_POINTS 100void RenderCircle(){   glBegin(GL_LINE_LOOP);   for(int i = 0; i < NUM_POINTS; "increment i")   {      float angle = 2 * MATH_PI * i / NUM_POINTS;      glVertex3d(cos(angle), sin(angle), 0.0);   }   glEnd();}

I couldn't get the 'plus' symbol into this page so i put "increment i" instead.
Yann L
Yann L
This sounds a lot like a homework question. learnopengl, could you please explain for what exactly you need to know this ?
mrbastard
mrbastard
archimedian... spiral?
[size="1"]
sprite_hound
sprite_hound
Quote:
Original post by bitshifter
To render a circle:
*** Source Snippet Removed ***
I couldn't get the 'plus' symbol into this page so i put "increment i" instead.


Ermm... don't you need to take into account the desired radius somewhere in there? Something more like:

#include <cmath>const int numPoints = 360;const float pi = 3.141593f;void renderCircle(float posx, float posy, float radius) {    glBegin(GL_LINE_LOOP);    for (int i = 0; i != numPoints; ++i) {        float angle = 2 * pi * i / static_cast<float>(numPoints);        glVertex2f(posx + radius * cosf(angle), posy + radius * sinf(angle));    }    glEnd();}


For a spiral (if that's what you're after) I guess it would be similar, except you'd increase the radius with every point, and have the potential to go past 360 degrees.

#include <cmath>const int numPoints = 360;const float pi = 3.141593f;void renderSpiral(float posx, float posy, float rotations, float startRadius, float endRadius) {    glBegin(GL_LINE_LOOP);    for (int i = 0; i != numPoints; ++i) {        float angle = 2 * pi * rotations * i / static_cast<float>(numPoints);        float radius = startRadius + (endRadius - startRadius) * i / static_cast<float>(numPoints);        glVertex2f(posx + radius * cosf(angle), posy + radius * sinf(angle));    }    glEnd();}


(I've not checked these or anything.)
bitshifter
bitshifter
I left out the radius modifier for simplicity.
My code snippet assumes a radius of one unit.
For someone not knowing how to map the points of a circle, i think my code is a bit easier to understand than yours.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.