opengl Draws the outline of a 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 the outline of a 2D circle centered on the specified point and contained in the XY plane (z=0).

Demo Code

/*//from  www  . j  av  a2  s .  c  o m
 *   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 the outline of a 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 drawCircled(GL2 gl, double x, double y,
            double radius, int numVerts) {

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

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

        //  Begin rendering the circle.
        gl.glBegin(GL2.GL_LINE_LOOP);
        for (int m = 0; m < numVerts; ++m) {
            double xm1mx = xm1 - x;
            double ym1mx = ym1 - y;
            double x1 = x + xm1mx * c1 - ym1mx * s1;
            double y1 = y + xm1mx * s1 + ym1mx * c1;

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

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

    }
}

Related Tutorials