Task: Julia set





Java: import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

public class JuliaSet extends JPanel {
    private static final int MAX_ITERATIONS = 300;
    private static final double ZOOM = 1;
    private static final double CX = -0.7;
    private static final double CY = 0.27015;
    private static final double MOVE_X = 0;
    private static final double MOVE_Y = 0;

    public JuliaSet() {
        setPreferredSize(new Dimension(800, 600));
        setBackground(Color.white);
    }

    void drawJuliaSet(Graphics2D g) {
        int w = getWidth();
        int h = getHeight();
        BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                double zx = 1.5 * (x - w / 2) / (0.5 * ZOOM * w) + MOVE_X;
                double zy = (y - h / 2) / (0.5 * ZOOM * h) + MOVE_Y;
                float i = MAX_ITERATIONS;
                while (zx * zx + zy * zy < 4 && i > 0) {
                    double tmp = zx * zx - zy * zy + CX;
                    zy = 2.0 * zx * zy + CY;
                    zx = tmp;
                    i--;
                }
                int c = Color.HSBtoRGB((MAX_ITERATIONS / i) % 1, 1, i > 0 ? 1 : 0);
                image.setRGB(x, y, c);
            }
        }
        g.drawImage(image, 0, 0, null);
    }

    @Override
    public void paintComponent(Graphics gg) {
        super.paintComponent(gg);
        Graphics2D g = (Graphics2D) gg;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        drawJuliaSet(g);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setTitle("Julia Set");
            f.setResizable(false);
            f.add(new JuliaSet(), BorderLayout.CENTER);
            f.pack();
            f.setLocationRelativeTo(null);
            f.setVisible(true);
        });
    }
}


PHP: set_time_limit(300);
header("Content-Type: image/png");

class Julia {
	
	static private $started = false;
	
	public static function start() {
		if (!self::$started) {
			self::$started = true;
			new self;
		}
	}
	
	const AXIS_REAL 	= 0;
	const AXIS_IMAGINARY 	= 1;
	const C 		= [-0.75, 0.1];
	const RADII 		= [1, 0.5];
	const CENTER 		= [0, 0];
	const MAX_ITERATIONS 	= 100;
	const TICK_SPACING 	= 0.001;
	
	private $maxDistance;
	private $imageResource;
	private $whiteColorResource;
	private $z0 = [];
	
	private function __construct() {
		$this->maxDistance = max($this->distance(self::C), 2);
		$this->imageResource = imagecreate(
			$this->coordinateToPixel(self::RADII[self::AXIS_REAL], self::AXIS_REAL),
			$this->coordinateToPixel(self::RADII[self::AXIS_IMAGINARY], self::AXIS_IMAGINARY)
		);
		imagecolorallocate($this->imageResource, 0, 0, 0);
		$this->whiteColorResource = imagecolorallocate($this->imageResource, 255, 255, 255);
		
		for ($x = self::CENTER[self::AXIS_REAL] - self::RADII[self::AXIS_REAL];
		$x <= self::CENTER[self::AXIS_REAL] + self::RADII[self::AXIS_REAL]; $x += self::TICK_SPACING) {
			$z0[self::AXIS_REAL] = $x;
			
			for ($y = self::CENTER[self::AXIS_IMAGINARY] - self::RADII[self::AXIS_IMAGINARY];
			$y <= self::CENTER[self::AXIS_IMAGINARY] + self::RADII[self::AXIS_IMAGINARY]; $y += self::TICK_SPACING) {
				$z0[self::AXIS_IMAGINARY] = $y;
				$iterations = 1;
				do {
					$z0 = $this->q($z0);
					$iterations++;
				} while($iterations < self::MAX_ITERATIONS && $this->distance($z0) <= $this->maxDistance);
				
				if ($iterations !== self::MAX_ITERATIONS) {
					imagesetpixel(
						$this->imageResource,
						$this->coordinateToPixel($x, self::AXIS_REAL),
						$this->coordinateToPixel($y, self::AXIS_IMAGINARY),
						$this->whiteColorResource
					);
				}
				$z0[self::AXIS_REAL] = $x;
			}
		}
	}
	
	public function __destruct() {
		imagepng($this->imageResource);
		imagedestroy($this->imageResource);
	}
	
	private function q($z) {
		return [ ($z[self::AXIS_REAL] ** 2) - ($z[self::AXIS_IMAGINARY] ** 2) + self::C[self::AXIS_REAL],
                       (2 * $z[self::AXIS_REAL] * $z[self::AXIS_IMAGINARY]) + self::C[self::AXIS_IMAGINARY] ];
	}
	
	private function distance($z) {
		return sqrt( ($z[self::AXIS_REAL] ** 2) + ($z[self::AXIS_IMAGINARY] ** 2) );
	}
	
	private function coordinateToPixel($coordinate, $axis) {
		return ($coordinate + self::RADII[$axis]) * (self::TICK_SPACING ** -1);
	}
}

Julia::start();