1
votes

I'm trying to write code for a weaving pattern in OpenGL. Weaving Pattern Pic

Now, I am trying to write code for a similar pattern using a circle. I draw a circle using points, each point is drawn using cos and sin functions. I understand this is not as efiicient as SeigeLord's method as it makes higher use of resources. I am able to get the circle, I want to get points on it's circumference.

My code :

#include<GL/glut.h>
#include<stdio.h>
#include<math.h>

int n, r;

void display()
{
    int i, j;
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-50, 50, -50, 50);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    for (i = 0;i <= 360;i += 1)
    {
        glBegin(GL_POINTS);
        glVertex2f(r*cos(i), r*sin(i));
        glEnd();
    }
    /*for (i = 0;i < 360;i += 10)
    {
        glBegin(GL_LINES);
        glVertex2f(r*cos(i), r*sin(i));
        glVertex2f(r*cos(i + 300), r*sin(i + 300));
        glEnd();
    }*/
    glFlush();
}
int main(int argc, char **argv)
{
    r = 30;
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(300, 50);
    glutInitWindowSize(800, 800);
    glutCreateWindow("Cylinder");
    glutDisplayFunc(display);
    glutMainLoop();
}

I tried using the commented code for getting lines between points 300 degrees apart, do this every at point 10 degrees apart.(It looks good at 3 degrees apart). Now, this obviously doesn't work as we use trigonometric functions, which won't space out the points equally.

I hope you understand my question, how can I get points on the circle equally apart?

One solution, I think might work is, while plotting the points itself, if I use an array to save every nth point, I may get equidistant points. Am I right? Is there any other way of getting the points?

Please do correct me if I am wrong anywhere above, I am just newbie here.

1

1 Answers

0
votes

Note that sin and cos take their input in radians(i.e. 0 to 2* pi), not degrees(0 to 360). So your code should probably be

for (i = 0;i <= 360;i += 1)
{
    glBegin(GL_POINTS);
    glVertex2f(r*cos(i * (M_PI / 180.)), r*sin(i* (M_PI / 180.)));
    glEnd();
}

edit:

To get N equidistant point we have to put them (1/N) part of the circle away from each other:

for (i = 0;i < N;i += 1)
{
    double angle = i * (2.0 * M_PI / N);
    glBegin(GL_POINTS);
    glVertex2f(r*cos(angle), r*sin(angle));
    glEnd();
}