Skip to content

Instantly share code, notes, and snippets.

@Lubba-64
Created March 21, 2022 23:18
Show Gist options
  • Save Lubba-64/dd5da32c4e179765ede175ac79f9309b to your computer and use it in GitHub Desktop.
Save Lubba-64/dd5da32c4e179765ede175ac79f9309b to your computer and use it in GitHub Desktop.
import cmath
from PIL import Image
import yaml
#colors are expressed as (R,G,B) 0-255
props = {}
with open('./props.yaml','rt') as f:
props = yaml.full_load(f.read())
# view
res = props['view']['resolution']
pos = props['view']['view_pos']
scale = props['view']['view_scale']
# equation
power = props['equation']['pow']
iterations = props['equation']['iterations']
equation_id = props['equation']['equation_id']
escape_dist = props['equation']['escape_dist']
# coloring
in_color = props['coloring']['in_color']
out_colors = props['coloring']['out_colors']
img = Image.new( mode = "RGB", size = (res['x'], res['y']), color = (0,0,0) )
pixels = img.load()
# various fractal equations, you can add your own by adding an elif and checking if the id is equal to some string
def fractal_equation(z:complex,c:complex,id):
if id == "HyperbolicCos":
try:
z = cmath.cosh(z**power) + c
except OverflowError:
z = complex(1000,1000)
elif id == "Classic":
z = z**power + c
elif id == "BurningShip":
z = (abs(z.real) + complex(0,1)*abs(z.imag))**power + c
return z
for x in range(res['x']):
print(f'finish rendering column #:{x}')
for y in range(res['y']):
# initializes the current pixel's variables
escaped = False
color_index = 0
x1 = ((res['x'] + pos['x'])-(x*scale))/res['x']
y1 = ((res['y'] + pos['y'])-(y*scale))/res['y']
c = complex(x1,y1)
z = complex(0,0)
# keep iterating over the equation until it "escapes"
for i in range(iterations + 1):
if abs(z.imag) + abs(z.real) < escape_dist:
z = fractal_equation(z,c,equation_id)
else:
escaped = True
color_index = i % len(out_colors)
break
# colors the current pixel based on whether it escaped or not
if not escaped:
pixels[x,y] = tuple(in_color)
else:
pixels[x,y] = tuple(out_colors[color_index])
from datetime import datetime
img.save(fp=f'./fractal-render-{datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}.png',format='png')
view:
resolution:
x: 1280
y: 1280
view_scale: 4
view_pos:
x: 0
y: 1280
equation:
pow: 2
iterations: 1000
equation_id: HyperbolicCos
# the distance from the origin where a point is considered "escaped"
escape_dist: 4
coloring:
# format: [r,g,b]
out_colors: [[255,255,255],[255,240,240],[255,225,225],[255,210,210],[255,195,195],[255,180,180],[255,165,165],[255,150,150],[255,135,135],[255,120,120],[255,105,105]
,[255,90,90],[255,75,75],[255,60,60],[255,45,45],[255,30,30],[255,15,15],[255,0,0],[255,15,15],[255,30,30],[255,45,45],[255,60,60],[255,75,75],[255,90,90],[255,105,105]
,[255,120,120],[255,135,135],[255,150,150],[255,165,165],[255,180,180],[255,195,195],[255,210,210],[255,225,225],[255,240,240]]
in_color: [0,0,0]
@Lubba-64
Copy link
Author

fractal-render-2022-03-21-19-23-20

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment