Android Open Source - ExpertAndroid Regular Polygon






From Project

Back to project page ExpertAndroid.

License

The source code is released under:

MIT License

If you think the Android project ExpertAndroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.iuriio.demos.expertandroid.ch9openglexperiments;
//from   w w w.  ja v a2  s .co  m
import android.util.Log;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

public class RegularPolygon {
    private float cx, cy, cz, r;
    private int sides;

    //coordinate array
    private float[] xarray = null;
    private float[] yarray = null;

    //texture arrray
    private float[] sarray = null;
    private float[] tarray = null;

    public RegularPolygon(float incx, float incy, float incz, // (x,y,z) center
                          float inr, // radius
                          int insides) // number of sides
    {
        cx = incx;
        cy = incy;
        cz = incz;
        r = inr;
        sides = insides;

        xarray = new float[sides];
        yarray = new float[sides];

        sarray = new float[sides];
        tarray = new float[sides];
        calcArrays();
        calcTextureArrays();
    }

    private void calcArrays() {
        float[] xmarray = this.getXMultiplierArray();
        float[] ymarray = this.getYMultiplierArray();

        //calc xarray
        for (int i = 0; i < sides; i++) {
            float curm = xmarray[i];
            float xcoord = cx + r * curm;
            xarray[i] = xcoord;
        }
        this.printArray(xarray, "xarray");

        //calc yarray
        for (int i = 0; i < sides; i++) {
            float curm = ymarray[i];
            float ycoord = cy + r * curm;
            yarray[i] = ycoord;
        }
        this.printArray(yarray, "yarray");

    }

    private void calcTextureArrays() {
        float[] xmarray = this.getXMultiplierArray();
        float[] ymarray = this.getYMultiplierArray();

        //calc xarray
        for (int i = 0; i < sides; i++) {
            float curm = xmarray[i];
            float xcoord = 0.5f + 0.5f * curm;
            sarray[i] = xcoord;
        }
        this.printArray(sarray, "sarray");

        //calc yarray
        for (int i = 0; i < sides; i++) {
            float curm = ymarray[i];
            float ycoord = 0.5f + 0.5f * curm;
            tarray[i] = ycoord;
        }
        this.printArray(tarray, "tarray");
    }

    public FloatBuffer getVertexBuffer() {
        int vertices = sides + 1;
        int coordinates = 3;
        int floatsize = 4;
        int spacePerVertex = coordinates * floatsize;

        ByteBuffer vbb = ByteBuffer.allocateDirect(spacePerVertex * vertices);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer mFVertexBuffer = vbb.asFloatBuffer();

        //Put the first coordinate (x,y,z:0,0,0)
        mFVertexBuffer.put(cx); //x
        mFVertexBuffer.put(cy); //y
        mFVertexBuffer.put(0.0f); //z

        int totalPuts = 3;
        for (int i = 0; i < sides; i++) {
            mFVertexBuffer.put(xarray[i]); //x
            mFVertexBuffer.put(yarray[i]); //y
            mFVertexBuffer.put(0.0f); //z
            totalPuts += 3;
        }
        Log.d("total puts:", Integer.toString(totalPuts));
        return mFVertexBuffer;
    }

    public FloatBuffer getTextureBuffer() {
        int vertices = sides + 1;
        int coordinates = 2;
        int floatsize = 4;
        int spacePerVertex = coordinates * floatsize;

        ByteBuffer vbb = ByteBuffer.allocateDirect(spacePerVertex * vertices);
        vbb.order(ByteOrder.nativeOrder());
        FloatBuffer mFTextureBuffer = vbb.asFloatBuffer();

        //Put the first coordinate (x,y (s,t):0,0)
        mFTextureBuffer.put(0.5f); //x or s
        mFTextureBuffer.put(0.5f); //y or t

        int totalPuts = 2;
        for (int i = 0; i < sides; i++) {
            mFTextureBuffer.put(sarray[i]); //x
            mFTextureBuffer.put(tarray[i]); //y
            totalPuts += 2;
        }
        Log.d("total texture puts:", Integer.toString(totalPuts));
        return mFTextureBuffer;
    }

    public ShortBuffer getIndexBuffer() {
        short[] iarray = new short[sides * 3];
        ByteBuffer ibb = ByteBuffer.allocateDirect(sides * 3 * 2);
        ibb.order(ByteOrder.nativeOrder());
        ShortBuffer mIndexBuffer = ibb.asShortBuffer();
        for (int i = 0; i < sides; i++) {
            short index1 = 0;
            short index2 = (short) (i + 1);
            short index3 = (short) (i + 2);
            if (index3 == sides + 1) {
                index3 = 1;
            }
            mIndexBuffer.put(index1);
            mIndexBuffer.put(index2);
            mIndexBuffer.put(index3);

            iarray[i * 3 + 0] = index1;
            iarray[i * 3 + 1] = index2;
            iarray[i * 3 + 2] = index3;
        }
        this.printShortArray(iarray, "index array");
        return mIndexBuffer;
    }

    private float[] getXMultiplierArray() {
        float[] angleArray = getAngleArrays();
        float[] xmultiplierArray = new float[sides];
        for (int i = 0; i < angleArray.length; i++) {
            float curAngle = angleArray[i];
            float sinvalue = (float) Math.cos(Math.toRadians(curAngle));
            float absSinValue = Math.abs(sinvalue);
            if (isXPositiveQuadrant(curAngle)) {
                sinvalue = absSinValue;
            } else {
                sinvalue = -absSinValue;
            }
            xmultiplierArray[i] = this.getApproxValue(sinvalue);
        }
        this.printArray(xmultiplierArray, "xmultiplierArray");
        return xmultiplierArray;
    }

    private float[] getYMultiplierArray() {
        float[] angleArray = getAngleArrays();
        float[] ymultiplierArray = new float[sides];
        for (int i = 0; i < angleArray.length; i++) {
            float curAngle = angleArray[i];
            float sinvalue = (float) Math.sin(Math.toRadians(curAngle));
            float absSinValue = Math.abs(sinvalue);
            if (isYPositiveQuadrant(curAngle)) {
                sinvalue = absSinValue;
            } else {
                sinvalue = -absSinValue;
            }
            ymultiplierArray[i] = this.getApproxValue(sinvalue);
        }
        this.printArray(ymultiplierArray, "ymultiplierArray");
        return ymultiplierArray;
    }

    private boolean isXPositiveQuadrant(float angle) {
        if ((0 <= angle) && (angle <= 90)) {
            return true;
        }

        if ((angle < 0) && (angle >= -90)) {
            return true;
        }
        return false;
    }

    private boolean isYPositiveQuadrant(float angle) {
        if ((0 <= angle) && (angle <= 90)) {
            return true;
        }

        if ((angle < 180) && (angle >= 90)) {
            return true;
        }
        return false;
    }

    private float[] getAngleArrays() {
        float[] angleArray = new float[sides];
        float commonAngle = 360.0f / sides;
        float halfAngle = commonAngle / 2.0f;
        float firstAngle = 360.0f - (90 + halfAngle);
        angleArray[0] = firstAngle;

        float curAngle = firstAngle;
        for (int i = 1; i < sides; i++) {
            float newAngle = curAngle - commonAngle;
            angleArray[i] = newAngle;
            curAngle = newAngle;
        }
        printArray(angleArray, "angleArray");
        return angleArray;
    }

    private float getApproxValue(float f) {
        if (Math.abs(f) < 0.001) {
            return 0;
        }
        return f;
    }

    public int getNumberOfIndecies() {
        return sides * 3;
    }

    public static void test() {
        RegularPolygon triangle = new RegularPolygon(0, 0, 0, 1, 3);
    }

    private void printArray(float array[], String tag) {
        StringBuilder sb = new StringBuilder(tag);
        for (int i = 0; i < array.length; i++) {
            sb.append(";").append(array[i]);
        }
        Log.d("hh", sb.toString());
    }

    private void printShortArray(short array[], String tag) {
        StringBuilder sb = new StringBuilder(tag);
        for (int i = 0; i < array.length; i++) {
            sb.append(";").append(array[i]);
        }
        Log.d(tag, sb.toString());
    }
}




Java Source Code List

com.androidbook.notebad.NoteEditor.java
com.androidbook.notebad.NotePadProvider.java
com.androidbook.notebad.NotePad.java
com.androidbook.notebad.NotesList.java
com.androidbook.notebad.TitleEditor.java
com.androidbook.parse.BaseActivity.java
com.androidbook.parse.BaseListActivity.java
com.androidbook.parse.CreateAMeaningActivity.java
com.androidbook.parse.CreateAWordActivity.java
com.androidbook.parse.Field.java
com.androidbook.parse.FormActivity.java
com.androidbook.parse.IReportBack.java
com.androidbook.parse.IValidator.java
com.androidbook.parse.IValueValidator.java
com.androidbook.parse.LoginActivity.java
com.androidbook.parse.ParseApplication.java
com.androidbook.parse.ParseObjectWrapperOld1.java
com.androidbook.parse.ParseObjectWrapper.java
com.androidbook.parse.ParseStarterProjectActivity.java
com.androidbook.parse.PasswordFieldRule.java
com.androidbook.parse.PasswordResetActivity.java
com.androidbook.parse.PasswordResetSuccessActivity.java
com.androidbook.parse.SignupActivity.java
com.androidbook.parse.SignupSuccessActivity.java
com.androidbook.parse.StringUtils.java
com.androidbook.parse.UserListActivity.java
com.androidbook.parse.WelcomeActivity.java
com.androidbook.parse.WordListActivity.java
com.androidbook.parse.WordListAdapter.java
com.androidbook.parse.WordMeaningListAdapter.java
com.androidbook.parse.WordMeaning.java
com.androidbook.parse.WordMeaningsListActivity.java
com.androidbook.parse.Word.java
com.iuriio.demos.expertandroid.ch10search.MainActivity.java
com.iuriio.demos.expertandroid.ch10search.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.MainActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SearchActivity.java
com.iuriio.demos.expertandroid.ch11searchprovider.SimpleSuggestionProvider.java
com.iuriio.demos.expertandroid.ch11searchprovider.SuggestUrlProvider.java
com.iuriio.demos.expertandroid.ch13parsesimple.BaseActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.LoginActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.MainActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseApp.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseObjectWrapper.java
com.iuriio.demos.expertandroid.ch13parsesimple.ParseStarterProjectActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.PasswordResetSuccessActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.ResetPasswordActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.SignupActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.StringUtils.java
com.iuriio.demos.expertandroid.ch13parsesimple.WordListActivity.java
com.iuriio.demos.expertandroid.ch13parsesimple.Word.java
com.iuriio.demos.expertandroid.ch1circleview.AbstractBaseView.java
com.iuriio.demos.expertandroid.ch1circleview.CircleView.java
com.iuriio.demos.expertandroid.ch1circleview.MainActivity.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DatePickerFragment.java
com.iuriio.demos.expertandroid.ch2durationcontrol.DurationControl.java
com.iuriio.demos.expertandroid.ch2durationcontrol.MainActivity.java
com.iuriio.demos.expertandroid.ch3flowlayout.FlowLayout.java
com.iuriio.demos.expertandroid.ch3flowlayout.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.ChildObject.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainActivity.java
com.iuriio.demos.expertandroid.ch4gsonserialization.MainObject.java
com.iuriio.demos.expertandroid.ch6forms.BaseActivity.java
com.iuriio.demos.expertandroid.ch6forms.Field.java
com.iuriio.demos.expertandroid.ch6forms.FormActivity.java
com.iuriio.demos.expertandroid.ch6forms.IValidator.java
com.iuriio.demos.expertandroid.ch6forms.IValueValidator.java
com.iuriio.demos.expertandroid.ch6forms.MainActivity.java
com.iuriio.demos.expertandroid.ch6forms.PasswordFieldRule.java
com.iuriio.demos.expertandroid.ch6forms.StringUtils.java
com.iuriio.demos.expertandroid.ch6forms.WelcomeActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.AnimatedSimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20AbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20ControlledAnimatedTexturedCubeRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SimpleTriangleRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.ES20SingleTextureAbstractRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.FrustumDimensions.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MainActivity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.MyApplication.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES10Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.OpenGLES20Activity.java
com.iuriio.demos.expertandroid.ch9openglexperiments.PolygonRenderer.java
com.iuriio.demos.expertandroid.ch9openglexperiments.RegularPolygon.java
com.iuriio.demos.expertandroid.ch9openglexperiments.Shape.java
com.iuriio.demos.expertandroid.ch9openglexperiments.SimpleTriangleRenderer.java
converters.FieldTransporter.java
converters.IFieldTransport.java
converters.IntegerFieldTransport.java
converters.ParseObjectEssentials.java
converters.StringFieldTransport.java
converters.User.java
converters.ValueField.java