Skip to content

Instantly share code, notes, and snippets.

@wmalarski
Created April 25, 2017 18:33
Show Gist options
  • Save wmalarski/447315244633c851636c0b57c929d2f4 to your computer and use it in GitHub Desktop.
Save wmalarski/447315244633c851636c0b57c929d2f4 to your computer and use it in GitHub Desktop.
VPython Lab6. Fractal
from __future__ import division
import numpy as np
import matplotlib.pyplot as plt
if __name__ == "__main__":
n = 10**6
x_pos = np.zeros(n)
y_pos = np.zeros(n)
def new_pos(p, x, y):
if p <= 0.85:
return 0.85 * x + 0.04 * y, -0.04 * x + 0.859 * y + 1.6
elif 0.85 < p <= 0.92:
return 0.2 * x - 0.26 * y, 0.23 * x + 0.22 * y + 1.6
elif 0.92 < p <= 0.99:
return -0.15 * x + 0.28 * y, 0.26 * x + 0.24 * y + 0.44
else:
return 0.0, 0.16 * y
for i in range(n - 1):
x_pos[i + 1], y_pos[i + 1] = new_pos(np.random.sample(), x_pos[i], y_pos[i])
plt.figure(facecolor='black')
plt.subplot(1, 2, 1, axisbg='black')
plt.plot(x_pos, y_pos, ',', color='Lime')
xl, xu, yl, yu = 0.25, 0.75, 2, 3
plt.plot([xl, xu, xu, xl, xl], [yl, yl, yu, yu, yl], 'r')
x_pos2 = np.zeros(n)
y_pos2 = np.zeros(n)
xn, yn = 0.0, 0.0
cnt = 0
while cnt < n:
xn, yn = new_pos(np.random.sample(), xn, yn)
if xl < xn < xu and yl < yn < yu:
x_pos2[cnt], y_pos2[cnt] = xn, yn
cnt += 1
plt.subplot(1, 2, 2, axisbg='black')
plt.plot(x_pos2, y_pos2, ',', color='Lime')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment