4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

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:

Name | Solution found | Comment

  • | - | -
    @enderrag | | Looks good, although I don't like that the edges are cut.

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

Winners:

Congratulations @enderrag, you won 2 SBI!

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

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

30 upvotes 1 downvotes $0.10

Replies (4)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: