0087e815f80bb1a114bd5aaabba18a962af1961d

Author
Pavle Portic <pavle.portic@tilda.center>
Committer
Pavle Portic <pavle.portic@tilda.center>
Date

Message

Initial commit

Diff

This diff is truncated to protect this page.

  1diff --git a/.gitignore b/.gitignore
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..bee8a64b79a99590d5303307144172cfe824fbf7
  4--- /dev/null
  5+++ b/.gitignore
  6@@ -0,0 +1 @@
  7+__pycache__
  8diff --git a/mandelbrot.py b/mandelbrot.py
  9new file mode 100644
 10index 0000000000000000000000000000000000000000..6782d2c0b2976c013b8814ec90b8cb478f5fbb33
 11--- /dev/null
 12+++ b/mandelbrot.py
 13@@ -0,0 +1,418 @@
 14+#! /usr/bin/env python
 15+# -*- coding: utf-8 -*-
 16+# vim:fenc=utf-8
 17+#
 18+# Copyright © 2019 pavle <pavle.portic@tilda.center>
 19+#
 20+# Distributed under terms of the BSD-3-Clause license.
 21+
 22+
 23+import numpy as np
 24+import curses
 25+
 26+MAX_ITERS = 100
 27+COLOR_COUNT = 101
 28+ZOOM_LEVEL = 0
 29+X_SCALE = 1.75
 30+Y_SCALE = 1
 31+X_MID = -0.75
 32+Y_MID = 0
 33+
 34+
 35+def get_points(cols, rows):
 36+	x_min = X_MID - X_SCALE * (1 / (2 ** ZOOM_LEVEL))
 37+	x_max = X_MID + X_SCALE * (1 / (2 ** ZOOM_LEVEL))
 38+	y_min = Y_MID - Y_SCALE * (1 / (2 ** ZOOM_LEVEL))
 39+	y_max = Y_MID + Y_SCALE * (1 / (2 ** ZOOM_LEVEL))
 40+	x = np.linspace(x_min, x_max, cols)
 41+	y = np.linspace(y_min, y_max, rows)
 42+	return x, y
 43+
 44+
 45+def calculate_point(x, y):
 46+	z = 0
 47+	for i in range(MAX_ITERS):
 48+		z = z ** 2 + complex(x, y)
 49+		if abs(z) >= 2:
 50+			return i
 51+
 52+	return -1
 53+
 54+
 55+def calculate_set(cols, rows):
 56+	x, y = get_points(cols, rows)
 57+	matrix = np.zeros([rows, cols])
 58+	for i in range(rows):
 59+		for j in range(cols):
 60+			matrix[i, j] = calculate_point(x[j], y[i])
 61+
 62+	return matrix
 63+
 64+
 65+def print_set(screen, cols, rows, matrix, char):
 66+	screen.clear()
 67+	for i in range(rows):
 68+		for j in range(cols):
 69+			iteration = matrix[i, j]
 70+			if iteration == -1:
 71+				screen.addstr(i, j, ' ', curses.color_pair(1))
 72+			else:
 73+				color_index = (int(iteration) + 2) % COLOR_COUNT
 74+				screen.addstr(i, j, ' ', curses.color_pair(color_index))
 75+
 76+	screen.addstr(rows, 0, f'Iterations: {MAX_ITERS}, Zoom: {ZOOM_LEVEL}, Char: {char}', curses.color_pair(COLOR_COUNT))
 77+	screen.refresh()
 78+
 79+
 80+def handle_keyboard(char):
 81+	global X_MID
 82+	global Y_MID
 83+	global ZOOM_LEVEL
 84+	global MAX_ITERS
 85+
 86+	if char == 113:
 87+		return True
 88+	elif char == 258:  # Down
 89+		Y_MID += Y_SCALE * (1 / (2 ** (ZOOM_LEVEL + 2)))
 90+		return False
 91+	elif char == 259:  # Up
 92+		Y_MID -= Y_SCALE * (1 / (2 ** (ZOOM_LEVEL + 2)))
 93+		return False
 94+	elif char == 260:  # Left
 95+		X_MID -= X_SCALE * (1 / (2 ** (ZOOM_LEVEL + 2)))
 96+		return False
 97+	elif char == 261:  # Right
 98+		X_MID += X_SCALE * (1 / (2 ** (ZOOM_LEVEL + 2)))
 99+		return False
100+	elif char == 40:  # Zoom out
101+		if ZOOM_LEVEL > 0:
102+			ZOOM_LEVEL -= 1
103+		return False
104+	elif char == 41:  # Zoom in
105+		ZOOM_LEVEL += 1
106+		return False
107+	elif char == 43:  # Iterations up
108+		MAX_ITERS += 1
109+		return False
110+	elif char == 45:  # Iterations down
111+		MAX_ITERS -= 1
112+		return False