Finding the vertices of a square giving the mid point and radius

66 Views Asked by At

I'm a programmer with terrible mathematics skills, but I'm getting by with studies. I'm trying to find out the vertices (x,y) of a square, giving the centre point (0,0) and the radius of the square which is 0.5.

Is there a formula that can locate A, B, C and D?

void Draw(int s) //pass in S for numberof sides 
{


    const unsigned int sides = s; //number of siders

    const float PI = 3.14159; //pie 
    const float step = (2 * PI) / static_cast<float>(sides); //make the circle and divide it by the amount of sides to get the shape 

    glTranslatef(x, y, 0);//the x and y position of the shape 
    glRotatef(angle, 0, 0, 1);//the angle, we spin on Z axis, also set point
    glScalef(radius, radius, 0);//depending on the radius it scales the shape 

    glBegin(GL_TRIANGLE_FAN);//we tell gl we make triangle fan 
    glVertex2f(0, 0);//start from this point 
    for (unsigned int i = 0; i <= sides; ++i)//depending on how many sides we have, loop it 
    {
        if (i == 0)
            glColor3ub(0, 0, 255);//make sure the first shape changes colour
        else if (i == 1)
            glColor3ub(255, 0, 0);//leave the other triangles red
        else
            glColor3ub(0, 255, 0);//leave the other triangles red


        glVertex2f(cos(i * step), sin(i * step));//Returns the cosine of the specified angle
        v[i].x = cos(i * step);
        v[i].y = cos(i * step);
        cout << v[i].x << endl;
        //makes the lines, starts from 




    }
    glEnd();//tell open gl we finish making shape 


}

enter image description here

As you can see from the code it creates a shape depending on the number of sides. Four sides will amount to a square. However, how can I get the vertices of the square.

This is because I want to morph the pentagon into the square by interpolating it, with the code provided that makes the square, I do not now how I can get the vertices of the square.