quarta-feira, 26 de fevereiro de 2014

Draw a circle on playn

A simple implementation of drawCircle on a playn surface.

    private void drawCircle(float x, float y, float radius, Surface surface, int color) {
        surface.setFillColor(color);
        int circunferenceSidesCount = 10;

        float[] points = new float[(circunferenceSidesCount + 1) * 2];
        int[] indices = new int[circunferenceSidesCount * 3];
        points[0] = x;
        points[1] = y;
        for (int i = 0; i < circunferenceSidesCount; i++) {
            int pointIndex = (i + 1) * 2;

            float angleProgression = (float) i / circunferenceSidesCount;

            double deltaX = radius * Math.cos(MathUtil.TWO_PI * angleProgression);
            points[pointIndex] = (float) (x - deltaX);
            double deltaY = radius * Math.sin(MathUtil.TWO_PI * angleProgression);
            points[pointIndex + 1] = (float) (y - deltaY);

            int indicesIndex = i * 3;
            indices[indicesIndex] = i + 1;
            indices[indicesIndex + 1] = (i + 2 > circunferenceSidesCount) ? 1 : i + 2;
            indices[indicesIndex + 2] = 0;
        }

        surface.fillTriangles(points, indices);
    }

Nenhum comentário:

Postar um comentário