quantumdeveloper avatar

Computation Contest #8 Results and Solution

quantumdeveloper

Published: 30 Dec 2019 › Updated: 30 Dec 2019Computation Contest #8 Results and Solution

Computation Contest #8 Results and Solution

Solution

The problem of this contest was to simply draw a fractal of your choice.
I chose to draw this fractal as an example:

This fractal was generated with the chaos game algorithm.
The chaos game works on a few simple rules:

Start with a set of n points in a 2d plane.
Choose a random point x on the plane.
Now in a loop make:
  1. Choose one of the n points randomly
  2. Move x half the distance towards the chosen point
  3. Paint a dot at x(Preferably with low alpha, to make the image look smoother).

In the example shown above I used 5 points:
The 4 corners of a rectangle and the exact center.

Here is my implementation:

import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Graphics;

public class fractal extends JFrame {
    double x, y;
    int len;
    double [] xP;
    double [] yP;
    
    public fractal() {
        // Initialize JFrame:
        super("Fractal");
        this.setSize(1800, 1200);
        this.setVisible(true);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // Initialize points:
        // Here 4 corners of a rectangle and the center:
        xP = new double[]{100, 100, 1700, 1700, 900};
        yP = new double[]{100, 1100, 1100, 100, 600};
        len = 5;
        // Start at one of the corners:
        x = xP[0];
        y = yP[0];
    }
    int n = 0;
    @Override
    public void paint(Graphics g) {
        // Make sure the background really gets drawn black:
        if(n <= 100) {
            g.setColor(Color.BLACK);
            g.fillRect(0, 0, 1800, 1200);
            n++;
        }
        // Use some color different to the background with low alpha value to draw the current position of the dot:
        g.setColor(new Color(255, 255, 255, 2));
        g.drawRect((int)x, (int)y, 1, 1);
        // Choose a new point:
        int z = (int)(len * Math.random());
        // Move the point half the distance to that point:
        x -= (x-xP[z])/2;
        y -= (y-yP[z])/2;
    }
    public static void main(String [] args) {
        fractal f = new fractal();
        while(true) {
            f.repaint();
        }
    }
}

I wonder why only one participant showed up, although it wasn't that hard.

↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓

List of participants with their entries:

NameSolution foundComment
enderrag@enderragLooks good, although I don't like that the edges are cut.

↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓

Winners:

Congratulations enderrag@enderrag, you won 2 SBI!

↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓↑↓

The next contest starts soon. Don't miss it!

Leave Computation Contest #8 Results and Solution to:

Written by

-ħ²/2m ∇²Ψ + VΨ = EΨ = iħ ∂Ψ/∂t

Read more #programming posts


Best Posts From quantumdeveloper

We have not curated any of quantumdeveloper's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From quantumdeveloper