迭代函数系统(IFS)

迭代函数系统(IFS)

IFS库源码已经上传GitHub fractal,需查看源码的请挪步。

自定义概率迭代函数

代码:

# Sier三角变换
from fractal import IFS
from random import randint


def ifsp(x, y):
    p = randint(1, 3)
    if p == 1:
        return (0.5 * x, 0.5 * y)
    elif p == 2:
        return (0.5 * x + 0.5, 0.5 * y + 0.5)
    else:
        return (0.5 * x + 0.5, 0.5 * y)

ob = IFS([460, 450], title="Sier")
ob.setPx(400, 0, 10)
ob.setIfsp(ifsp)
ob.doIFS(200000)
ob.wait()

结果:

代码:

# 蕨类IFS生成

from fractal import IFS
from random import random


def ifsp(x, y):
    p = random()
    if p < 0.01:
        return (0, 0.16 * y)
    elif p < 0.07:
        if random() > 0.5:
            return (0.21 * x - 0.25 * y, 0.25 * x + 0.21 * y + 0.44)
        else:
            return (-0.2 * x + 0.26 * y, 0.23 * x + 0.22 * y + 0.6)
    else:
        return (0.85 * x + 0.1 * y, -0.05 * x + 0.85 * y + 0.6)

ob = IFS([400, 500], title = "")
ob.setPx(100, 100, 100)
ob.setIfsp(ifsp)
ob.doIFS(200000)
ob.wait()

结果:

代码:

# Box IFS
from fractal import IFS
from random import randint


def ifsp(x, y):
    p = randint(1, 5)
    if p == 1:
        return (x / 3, y / 3)
    elif p == 2:
        return (x / 3 + 2 / 3, y / 3)
    elif p == 3:
        return (x / 3 + 1 / 3, y / 3 + 1 / 3)
    elif p == 4:
        return (x / 3, y / 3 + 2 / 3)
    else:
        return (x / 3 + 2 / 3, y / 3 + 2 / 3)

ob = IFS([500, 500], title="Box")
ob.setPx(490, 5, 5)
ob.setIfsp(ifsp)
ob.doIFS(200000)
ob.wait()

结果:

使用IFS码进行迭代

代码:

from fractal.IFS import IFS

ifscode = [
    [0.879, 0.054, -0.051, 0.878, 0.077, 0.123, 0.123],
    [0.1, -0.193, 0.285, 0.224, 0.174, 0.169, 0.169],
    [0.008, 0.135, 0, 0.204, 0.075, 0.074, 0.074],
    [0.402, 0.045, 0.016, -0.197, 0.111, 0.193, 0.193]
]

ifs = IFS([500, 500])
# ifs.setCoordinate()
ifs.setPx(700, 0, 0)
ifs.setIfsCode(ifscode)
ifs.doIFS(200000)
ifs.wait()

结果:

代码:

from fractal.IFS import IFS

code = [
    [0.195, -0.488, 0.344, 0.443, 0.4431, 0.2452, 0.2],
    [0.462, 0.414, -0.252, 0.361, 0.2511, 0.5692, 0.2],
    [-0.637, 0, 0, 0.501, 0.8562, 0.2512, 0.2],
    [-0.035, 0.07, -0.469, 0.022, 0.4884, 0.5069, 0.2],
    [-0.058, -0.07, -0.453, -0.111, 0.5976, 0.0969, 0.2]
]

ifs = IFS([500,500])
ifs.setCoordinate()
ifs.setPx(500, 0, 0)
ifs.setIfsCode(code)
ifs.doIFS(200000)
ifs.wait()

结果:

你可能感兴趣的:(算法,python)