opengl Draws a filled 2D circle centered on the specified point and contained in the XY plane (z=0). - Java javax.media.opengl

Java examples for javax.media.opengl:Draw

Description

opengl Draws a filled 2D circle centered on the specified point and contained in the XY plane (z=0).

Demo Code

/*// w w w .jav a  2s . c om
 *   GLUtils -- A set of static utilities for rendering simple shapes in OpenGL.
 *   
 *   Copyright (C) 2002-2004 by Joseph A. Huwaldt.
 *   All rights reserved.
 *   
 *   This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2 of the License, or (at your option) any later version.
 *   
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *   Or visit:  http://www.gnu.org/licenses/lgpl.html
 **/
import javax.media.opengl.GL2;

public class Main{
    /**
     *  <p> Draws a filled 2D circle centered
     *      on the specified point and contained in the XY plane (z=0).
     *      Uses an efficient parametric algorithm to avoid expensive
     *      calls to triginometric functions.  Verticies are evenly
     *      spaced in parameter space.
     *  </p>
     *
     *  <p> Reference:  Rogers, D.F., Adams, J.A.,
     *                  _Mathematical_Elements_For_Computer_Graphics_,
     *                  McGraw-Hill, 1976, pg 103, 216.
     *  </p>
     *
     *  @param  gl   Reference to the graphics context we are rendering into.
     *  @param  x    X Coordinate of the center of the circle.
     *  @param  y    Y Coordinate of the center of the circle.
     *  @param  radius    The radius of the cirlce.
     *  @param  numVerts  The number of verticies to use.
     **/
    public static final void drawCircleFill(GL2 gl, float x, float y,
            float radius, int numVerts) {

        //  Calculate the parametric increment.
        double p = 2 * Math.PI / (numVerts - 1);
        float c1 = (float) Math.cos(p);
        float s1 = (float) Math.sin(p);

        //  Calculate the initial point.
        float xm1 = x + radius;
        float ym1 = y;

        //  Begin rendering the circle.
        gl.glBegin(GL2.GL_TRIANGLE_FAN);
        gl.glVertex3f(x, y, 0.0f); // origin
        for (int m = 0; m < numVerts; ++m) {
            float xm1mx = xm1 - x;
            float ym1mx = ym1 - y;
            float x1 = x + xm1mx * c1 - ym1mx * s1;
            float y1 = y + xm1mx * s1 + ym1mx * c1;

            //  Draw the next line segment.
            gl.glVertex3f(x1, y1, 0);

            //  Prepare for the next loop.
            xm1 = x1;
            ym1 = y1;
        }
        gl.glEnd();

    }
}

Related Tutorials