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

Demo Code

/*/* w w w. j  a va 2  s.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 the outline of a 2D inclined ellipse 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.
     *      If "a" and "b" are equal, a circle will be rendered,
     *      however the function "circle" is a bit more efficient
     *      for that special case.
     *  </p>
     *
     *  <p> Reference:  Rogers, D.F., Adams, J.A.,
     *                  _Mathematical_Elements_For_Computer_Graphics_,
     *                  McGraw-Hill, 1976, pg 104, 216.
     *  </p>
     *
     *  @param  gl   Reference to the graphics context we are rendering into.
     *  @param  x    X Coordinate of the center of the ellipse.
     *  @param  y    Y Coordinate of the center of the ellipse.
     *  @param  a    Length of the semi-major axis of the ellipse, oriented
     *               along the X axis.
     *  @param  b    Length of the semi-minor axis of the ellipse, oriented
     *               along the Y axis.
     *  @param  inc  Inclination angle of the major axis in degrees.
     *  @param  numVerts  The number of verticies to use.
     **/
    public static final void drawEllipse(GL2 gl, float x, float y, float a,
            float b, float inc, int numVerts) {

        //  Convert inclination to radians.
        double p = inc * Math.PI / 180;

        //  Calculate the sine and cosine of the inclination angle.
        float c1 = (float) Math.cos(p);
        float s1 = (float) Math.sin(p);

        //  Calculate the parametric increment.
        p = 2 * Math.PI / (numVerts - 1);

        //  Calculate the sine and cosine of the parametric increment.
        float c2 = (float) Math.cos(p);
        float s2 = (float) Math.sin(p);

        //  Initialize the accumulation variables.
        float c3 = 1;
        float s3 = 0;

        //  Begin rendering the ellipse.
        gl.glBegin(GL2.GL_LINE_LOOP);
        for (int m = 0; m < numVerts; ++m) {
            //  Calculate increments in X & Y.
            float x1 = a * c3;
            float y1 = b * s3;

            //  Calculate new X & Y.
            float xm = x + x1 * c1 - y1 * s1;
            float ym = y + x1 * s1 + y1 * c1;

            //  Draw the next line segment.
            gl.glVertex3f(xm, ym, 0);

            //  Calculate new angle values.
            float t1 = c3 * c2 - s3 * s2;
            s3 = s3 * c2 + c3 * s2;
            c3 = t1;
        }
        gl.glEnd();

    }
}

Related Tutorials