EV3 Chaos

Need to practice with Python. Not just for ev3dev/EV3 but surely this will be my main target.

So today I remembered the 80’s… Chaos and fractals and my 8088 taking hours to draw a Mandelbrot on an ambar Hercules screen.

So I found a simple python script from Lennart Poettering and adapted it to work on EV3:

#!/usr/bin/python3

# EV3 03m58s - video: https://youtu.be/w5aKqmXz_Wk
# based on a python script from  Lennart Poettering
# found here http://0pointer.de/blog/projects/mandelbrot.html

from PIL import Image, ImageDraw
import math, colorsys
import ev3dev.ev3 as ev3
from time import sleep

lcd = ev3.Screen()
lcd.clear()

sleep(1)

dimensions = (178, 128)
scale = 1.0/(dimensions[0]/3)
center = (2.0,1.0)
iterate_max = 15
colors_max = 2

img = Image.new("1", dimensions,"white")

d = ImageDraw.Draw(img)

# Calculate the mandelbrot sequence for the point c with start value z
def iterate_mandelbrot(c, z = 0):
    for n in range(iterate_max + 1):
        z = z*z +c
        if abs(z) > 2:
            return n
    return None

# Draw our image
for y in range(dimensions[1]):
    for x in range(dimensions[0]):
        c = complex(x * scale - center[0], y * scale - center[1])

        n = iterate_mandelbrot(c)

        if n is None:
            v = 1
        else:
            v = n/100.0

        if v > 0.5 :
            d.point((x,y), fill = 0)
        else:
            d.point((x,y), fill = 1)

        lcd.image.paste(img, (0,0))
        lcd.update()

del d
img.save("result.png")
sleep(1)

My EV3 is running ev3dev-jessie-2016-12-21 release. No need to install PIL or anything else, just create the script, give execution permissions and run it.

The script takes 3m58s to run. next video shows the result (4x speed):

And also the output file:

Ah, those mighty 80’s!!

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *