construct the circle from points

i am trying to make a circle from points i dont know why this renders quad instead of circle

function DrawCircle(k : int,h : int,r : int){

    GL.Begin(GL.LINES);

        for (var i:int = 0; i < 180; i++){

                Circle.x = r * Mathf.Cos(i) - h;
                Circle.y = r * Mathf.Sin(i) + k;
                GL.Vertex3(Circle.x + k,Circle.y - h,0);

                Circle.x = r * Mathf.Cos(i + 0.1) - h;
                Circle.y = r * Mathf.Sin(i + 0.1) + k;

                GL.Vertex3(Circle.x + k,Circle.y - h,0);

        }

    GL.End();
}

my math is a bit rusty, can somebody help me?

As the docs for Mathf.Cos and Mathf.Sin clearly state, the angle that they take in is in radians. "Returns the cosine of angle f in radians." and "Returns the sine of angle f in radians." You will need to change all of your calls to these functions to take in i * Mathf.Deg2Rad or just use radians.

–

I am fairly certain that a circle is 360 degrees, not 180.

–

Offsetting your angle by 0.1 only would give you a dotted circle. And adding to the radian note before, this would have to change to (i + 0.1) * Mathf.Deg2Rad or just use radians.

–

What are h and k? You add k and subtract h from both your x and y coordinates both times, so you get x+k-h, x+k-h. If they are the center of your circle, you would only need to add/subtract each one from the respective coordinate like x+k, y-h or something to that effect, depending on what h and k are coming in as.

–

You should probably load an otho matrix (with GL.PushMatrix();GL.LoadOrtho(); before your GL.Begin();) so that your coordinates are going to be used in the way you likely expect. Also, note that the coordinates used by GL functions are in normalized viewport coordinates from bottom-left to top-right and since the screen aspect ratio may not be square, this could give you an ellipse - To fix this, prior to offsetting them, either divide your y coordinates by your aspect ratio or multiply your x coordinates by your aspect ratio.

–

1 Answer

1
function DrawCircle(originX : int,originY : int,r : int){

    GL.Begin(GL.LINES);

        for (var i:int = 0; i < 180; i++){

                Circle.x = r * Mathf.Cos(i*Mathf.DegToRad) + originX;
                Circle.y = r * Mathf.Sin(i*Mathf.DegToRad) + originY;
                GL.Vertex3(Circle.x,Circle.y,0);

                Circle.x = r * Mathf.Cos(i*Mathf.DegToRad+Mathf.DegToRad) + originX;
                Circle.y = r * Mathf.Sin(i*Mathf.DegToRad+Mathf.DegToRad) + originY;
                GL.Vertex3(Circle.x,Circle.y,0);
        }

    GL.End();
}

See if that works for you.

What is Gl.Begin(GL.LINES) ? and GL.End(); Are they functions from a different script? Thank you.

–