2019年3月2日土曜日

開発環境

Math Adventures with Python: An Illustrated Guide to Exploring Math with Code (Peter Farrell(著)、No Starch Press)のPART 2(RIDING INTO MATH TERRITORY)、7(COMPLEX NUMBERS)、EXERCISE 7-1(DRAWING A JULIA SET)の解答を求めてみる。

コード

Python 3

import math

xmin = -2
xmax = 2
ymin = -2
ymax = 2
x_range = xmax - xmin
y_range = ymax - ymin
xscl = 0
yscl = 0


def setup():
    global xscl, yscl
    size(600, 600)
    colorMode(HSB)
    noStroke()
    xscl = width / float(x_range)
    yscl = height / float(y_range)


def julia(z, c, num):
    for count in range(num + 1):
        if abs(z) > 2.0:
            return count
        z = z ** 2 + c
    return num


def draw():
    translate(width/2, height/2)
    x = xmin
    while x < xmax:
        y = ymin
        while y < ymax:
            z = x + y * 1j
            c = 0.285 + 0.01j
            col = julia(z, c, 100)
            if col == 100:
                fill(0)
            else:
                fill(3 * col, 255, 255)
            rect(x * xscl, y*yscl, 1, 1)
            y += 0.01
        x += 0.01

0 コメント:

コメントを投稿