Pooya Sagharichiha
Pooya Sagharichiha

Reputation: 233

Draw Square With Polar Coordinates in OpenGL

How can I draw a square with polar coordinates in OpenGL?

I know these Equations:

 x = r * cos ( theta ) 
 y = r * sin ( theta )
 r = n * cos ( theta )

My code:

float baseX = width / 2.f; 
float baseY = height / 2.f
int n = 7;
glBegin(GL_POINTS);{
    for (float tempAngle = 0.0 ; tempAngle <= PI/4 ; tempAngle++) {
        radius = n * cos(PI/4);
        x = baseX + radius * cos(tempAngle);
        y = baseY + radius * sin(tempAngle);
        glVertex2f(x, y);
    }
}glEnd();

Upvotes: 1

Views: 3539

Answers (3)

Pooya Sagharichiha
Pooya Sagharichiha

Reputation: 233

I found the solution.

int n = 70;
int i = 1;
glBegin(GL_TRIANGLE_FAN);{
    for (float tempAngle = 0.0 ; tempAngle <= 2 * PI ; tempAngle += PI/8) {
        radius = n * cos(tempAngle);
        x = baseX + radius * cos(tempAngle);
        y = baseY + radius * sin(tempAngle);
        if (i % 2 == 0)
            glVertex2f(x, y);
        i++;
    }
}glEnd();

Upvotes: 0

Reto Koradi
Reto Koradi

Reputation: 54592

I hope we all realize that this is a purely theoretical exercise, and is not a reasonable way of drawing a square with OpenGL. But I thought it might be fun anyway, so here we go.

I don't think your formula is correct. Draw the right side of a square in a coordinate system, which is a vertical line at x = 1.0. If you then look at the distance from the origin of a given point on that line, depending on theta, you see that:

cos(theta) = 1.0 / r

Which quickly leads to the value of r for the square in a polar coordinate system:

r = 1.0 / cos(theta)

Based on this, here is code for drawing a square using polar coordinates. side2 is half the side length of the square:

const float PI_F = static_cast<float>(M_PI);
const unsigned DIV_COUNT = 10;
const float ANG_INC = 0.5f * PI_F / static_cast<float>(DIV_COUNT);

glBegin(GL_LINE_LOOP);

for (unsigned iDiv = 0; iDiv < DIV_COUNT; ++iDiv)
{
    float ang = -0.25f * PI_F + static_cast<float>(iDiv) * ANG_INC;
    float r = side2 / cos(ang);
    glVertex2f(r * cos(ang), r * sin(ang));
}

for (unsigned iDiv = 0; iDiv < DIV_COUNT; ++iDiv)
{
    float ang = -0.25f * PI_F + static_cast<float>(iDiv) * ANG_INC;
    float r = side2 / cos(ang);
    glVertex2f(r * -sin(ang), r * cos(ang));
}

for (unsigned iDiv = 0; iDiv < DIV_COUNT; ++iDiv)
{
    float ang = -0.25f * PI_F + static_cast<float>(iDiv) * ANG_INC;
    float r = side2 / cos(ang);
    glVertex2f(r * -cos(ang), r * -sin(ang));
}

for (unsigned iDiv = 0; iDiv < DIV_COUNT; ++iDiv)
{
    float ang = -0.25f * PI_F + static_cast<float>(iDiv) * ANG_INC;
    float r = side2 / cos(ang);
    glVertex2f(r * sin(ang), r * -cos(ang));
}

glEnd();

Upvotes: 1

datenwolf
datenwolf

Reputation: 162164

I'm not really sure what you mean by your question. A square has four corners, which correspond to four vertices when drawing. Fixed function OpenGL uses cartesian coordinates (it's trivial to implement a vertex shader that ingests polar or spherical coordinates and properly transforms to cartesian clip space).

So what exactly is your question. I can see only one meaningful way to interpret this question: "What are the coordinates of a square's corners in polar coordinates?" However I don't see how you relate that to OpenGL in your mental model.

Upvotes: 0

Related Questions