quarta-feira, 26 de fevereiro de 2014

A Koch Island written in javascript

The Koch snowflake is a fractal curve that arises from recursively divind a line segment in three sections and replacing the middle section with two line segments that forms a triangle.
It`s a really famous fractal and one that`s really easy to draw.
One cool property of this fractal is that if you switch randomly the direction of the top of the middle section triangle you get a line that resembles a coast.

I`ve written an interactive version of the koch snowflake, including the option to randomly change the triangle direction and a slider to increase/decrease the middle section size.

Have fun playing with it :)

http://beothorn.github.io/KochSnowflakeIslandGenerator/

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);
    }