Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download
Project: Peter's Files
Views: 3893
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu1804
Kernel: Python 3 (system-wide)

Iterated Function System Fractal Generator -- Examples

from IFSFGL import *

The Barnsely Fern

BW = np.array([0.01,.85,0.07,.07]) B1 = np.array([[0,0,0.5],[0, 0.16, 0],[0,0,1]]) # Translate(.5,0) @ ScaleXY(0,.16) B2 = np.array([[0.849,0.037,0.075],[-0.037, 0.849, 0.183],[0,0,1]]) # about Translate(0.075,0.183) @ Rotate(-0.037) @ Scale(0.849) B3 = np.array([[0.197, -0.226, 0.4],[0.226, 0.197, 0.049],[0,0,1]]) B4 = np.array([[-0.15,0.283,0.575],[0.26, 0.237, -0.084],[0,0,1]]) BT = [B1, B2, B3, B4]
plot_figures(generate_figures(1,[XBox],BT), size=5)
Image in a Jupyter notebook
fern = Fractal(BT, weights=BW, size=20)
fern.add_points(100_000)
fern.display_pic()
Image in a Jupyter notebook
G = generate_points(100_000, BT, BW)
Generating 100000 points... Finished in 2.1689255237579346 seconds.
fern.load_in_points(G)
fern.display_pic()
Image in a Jupyter notebook
fern.developement
200000

The Crab

C1 = Translate(0.5,0.15) @ Rotate(np.pi/4) @ Scale(1/4) C2 = Scale(1/2) C3 = Translate(-.5,0.15) @ Rotate(-np.pi/4) @ Scale(1/4) C4 = Rotate(-np.pi/6) @ Scale(1/2) C5 = Rotate(np.pi/6) @ Scale(1/2) CT = [C1, C2, C3, C4, C5]
crab = Fractal(CT)
crab.add_points(1_000_000)
crab.pic
Image in a Jupyter notebook

Keir's Leaf

LW = np.array([0.64,0.12,0.12,0.12]) L1 = Translate(0.15,0.3) @ Scale(0.7) L2 = Translate(0.35,0) @ Rotate(np.pi/4) @ Scale(0.35*np.sqrt(2)) L3 = Translate(0.3,0.35) @ Rotate(-np.pi/4) @ Scale(0.35*np.sqrt(2)) L4 = Translate(0.45,0.2) @ ScaleXY(1/10,3/10) LT = [L1, L2, L3, L4]
leaf = Fractal(LT, LW, size = 15) leaf.add_points(1_000_000)
leaf.pic
Image in a Jupyter notebook

Kotch Curve

k1 = Scale(1/3) k2 = Translate(1,0) @ Rotate(np.pi/3) @ Scale(1/3) k3 = Translate(1.5,np.sin(np.pi/3)) @ Rotate((-1*np.pi)/3) @ Scale(1/3) k4 = Translate(2,0) @ Scale(1/3) kT = [k1,k2,k3,k4]
kotchCurve = Fractal(kT, size=20) # kotchCurve.add_points(1_000_000) # kotchCurve.pic
kotchCurve.make_gif(name='point3', n=1_000_000, zoom=3.1, frames=30, zoomPoint=(1.5,np.sin(np.pi/3)))

Alyson's Alteration

a1 = Scale(1/3) a2 = Translate(1.5,-np.sin(np.pi/3)) @ Rotate(2*np.pi/3) @ Scale(1/3) a3 = Translate(2,0) @ Rotate(-2*np.pi/3) @ Scale(1/3) a4 = Translate(2,0) @ Scale(1/3) aT = [a1,a2,a3,a4]
plot_figures(generate_figures(1,[np.array([[0,0,1],[3,0,1]]).T],aT))
Image in a Jupyter notebook
alysonsAlt = Fractal(aT, size = 10)
alysonsAlt.add_points(1_000_000)
alysonsAlt.pic
Image in a Jupyter notebook

Kotch Snowflake

s=np.sin(pi/3) c=np.cos(pi/3) Line1 = np.array([ [0,1.5], [3*s,0], [1,1]]) Line2 = np.array([ [-1.5,0], [0,3*s], [1,1]]) Line3 = np.array([ [1.5,-1.5], [0,0], [1,1]]) L11 = Translate(0,2*s) @ Scale(1/3) L12 = Translate(.5+s**2,2*s-c*s) @ Rotate((pi/3)) @ Scale(1/3) L13 = Translate(1.5-s**2,2*s-c*s) @ Rotate((-1*pi)/3) @ Scale(1/3) L14 = Translate(1,0) @ Scale(1/3) T1_L = [L11,L12,L13,L14] L21 = Translate(0,2*s) @ Scale(1/3) L22 = Translate(-s**2,2*s-c*s) @ Rotate(pi/3) @ Scale(1/3) L23 = Translate(-2+s**2,2*s-c*s) @ Rotate((-1*pi)/3) @ Scale(1/3) L24 = Translate(-1,0) @ Scale(1/3) T2_L = [L21,L22,L23,L24] L31 = Translate(-1,0) @ Scale(1/3) L32 = Translate(-c/2,-s/2) @ Rotate(-pi/3) @ Scale(1/3) L33 = Translate(c/2,-s/2) @ Rotate(pi/3) @ Scale(1/3) L34 = Translate(1,0) @ Scale(1/3) T3_L = [L31,L32,L33,L34] plot_figures(generate_figures(4, [Line1], T1_L) + generate_figures(4, [Line2], T2_L)+ generate_figures(4, [Line3], T3_L))
Image in a Jupyter notebook

Koch Tessalation

T = T1_L + T2_L + T3_L
tessellation = Fractal(T)
tessellation.add_points(1_000_000)
tessellation.pic
Image in a Jupyter notebook

Name

name = Fractal(word_fractal('name'), size=10) name.add_points(100000) name.display_pic()
Image in a Jupyter notebook

Ï€\pi-inspired Fractals

line = np.array([[0, 0, 1], [1, 0, 1]], dtype = np.float64).T P1 = Translate(-1/5**(1/pi),0) @ Scale(1/5**(1/pi)) P2 = Scale(1/5**(1/pi)) P3 = Translate(1/5**(1/pi),0) @ Scale(1/5**(1/pi)) P4 = Rotate(-pi/2) @ Scale(1/5**(1/pi)) P5 = Translate(1/5**(1/pi),0) @ Rotate(-pi/2.5) @ Scale(1/5**(1/pi)) P = [P1,P2,P3,P4,P5]
plot_figures([line] + generate_figures(1,[line],P))
Image in a Jupyter notebook
pFractal = Fractal(P)
pFractal.add_points(5_000_000)
pFractal.pic
Image in a Jupyter notebook
pFractal.dimension(n=10_000_000, endSize=15)

Serpinski Triangle

E1 = Scale(1/2) E2 = Translate(1/2,0) @ Scale(1/2) E3 = Translate(cos(pi/3)/2, sin(pi/3)/2) @ Scale(1/2) E = [E1, E2, E3]
serpinski = Fractal(E) serpinski.add_points(1_000_000) serpinski.pic
Image in a Jupyter notebook
serpinski.dimension()

Squiggle

S1 = Scale(1/4) S2 = Translate(1/4,0) @ Rotate(pi/2) @ Scale(1/4) S3 = Translate(1/4,1/4) @ Scale(1/4) S4 = Translate(1/2,1/4) @ Rotate(-pi/2) @ Scale(1/4) S5 = Translate(1/2,0) @ Rotate(-pi/2) @ Scale(1/4) S6 = Translate(1/2,-1/4) @ Scale(1/4) S7 = Translate(3/4,-1/4) @ Rotate(pi/2) @ Scale(1/4) S8 = Translate(3/4,0) @ Scale(1/4) S = [S1,S2,S3,S4,S5,S6,S7,S8]
squiggle = Fractal(S) squiggle.add_points(1_000_000) squiggle.pic

Dragon Curve

D1 = Rotate(-pi/4) @ Scale(cos(pi/4)) D2 = Translate(1, 0) @ Rotate(-3*pi/4) @ Scale(cos(pi/4)) D = [D1, D2] plot_figures([line] + generate_figures(1,[line],D))
Image in a Jupyter notebook
dragon = Fractal(D)
dragon.add_points(1_000_000)
dragon.pic

Dill

from random import *
ReflectY = np.array([[-1, 0, 0], [0, 1, 0], [0, 0, 1]])
stem = [Translate(.5,0) @ Rotate(-.4) @ ScaleXY(0,.16), Translate(0.236,0.132) @ Rotate(.037) @ Scale(0.65)] h = 0.645 k = 0.41 w = .3 neg = -1 pos = 1 while (neg != pos) and (abs(neg-pos) != 1): neg = 0 pos = 0 parts = [] for _ in range(1): x = random() * (2 * w) - w y = - .25 * (x - w) * (x + w) + .18 r = sqrt(y**2 + x**2) if x >= 0: parts += [Translate(h,k) @ Rotate(-arctan(x/y)) @ Scale(r) @ Translate(-.5,0)] pos += 1 else: parts += [Translate(h,k) @ ReflectY @ Rotate(-arctan(abs(x)/y)) @ Scale(r) @ Translate(-.5,0)] neg += 1 dillWeights = np.zeros(2 + len(parts)) dillWeights[0:2] = .4 dillWeights[2:] = .2 / len(parts) Dill = stem + parts
figs = generate_figures(2,[XBox],Dill) plot_figures(figs, size=5)
Image in a Jupyter notebook
dillFractal = Fractal(Dill, weights=dillWeights, size=25)
dillFractal.add_points(1_000_000)
dillFractal.pic
Image in a Jupyter notebook