Creates a float array of vertices for a 2d circle - Android java.lang

Android examples for java.lang:array

Description

Creates a float array of vertices for a 2d circle

Demo Code

import android.opengl.GLES20;

public class Main{

    /**//from  w w  w .  jav  a  2 s  .  c om
     * Creates a float array of vertices for a 2d circle
     * This function is based on the info I found at http://slabode.exofire.net/circle_draw.shtml
     * 
     * @param radius
     * @param xOffset
     * @param yOffset
     * @param numVertices
     * @return
     */
    public static float[] get2DCircleCoordinates(float radius,
            float xOffset, float yOffset, int numVertices) {
        float[] coordinates = new float[numVertices * 3 + 3]; //add 3 to complete the circle
        int coordinatesLength = coordinates.length;

        float theta = (float) (2 * Math.PI / numVertices);
        float cos = (float) Math.cos(theta);
        float sin = (float) Math.sin(theta);
        float temp;

        float x = radius; //start at angle = 0
        float y = 0;

        int i = 0;

        while (i < coordinatesLength) {
            coordinates[i] = x + xOffset;
            coordinates[i + 1] = y + yOffset;
            coordinates[i + 2] = 0.0f;
            i = i + 3;

            //apply rotation matrix
            temp = x;
            x = cos * x - sin * y;
            y = sin * temp + y * cos;
        }

        return coordinates;
    }

}

Related Tutorials