-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaffine_transform.py
62 lines (43 loc) · 1.82 KB
/
affine_transform.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
AFFINE TRANSFORM
================
Affine transformations can be used to generate a number of fractals. This module defines a funciton affineTransformation, along with certain transforms and corresponding probabilities that can be used to generate fractals.
"""
from random import choices
B_FERN_TRANSFORMS = (
lambda x,y: (0, 0.16*y),
lambda x,y: (0.85*x + 0.04*y, -0.04*x + 0.85*y + 1.6),
lambda x,y: (0.20*x - 0.26*y, 0.23*x + 0.22*y + 1.6),
lambda x,y: (-0.15*x + 0.28*y, 0.26*x + 0.24*y + 0.44)
)
B_FERN_PROBS = (0.01, 0.85, 0.07, 0.07)
def affineTransformation(maximum, transforms = B_FERN_TRANSFORMS, probabilities = B_FERN_PROBS):
"""
Call this function to calculate the x- and y-values corresponding to the transforms and probabilities passed as arguments.
Parameters:
maximum (int): The number of iterations to use.
transforms (tuple): A tuple of lambda functions, each representing a transformation equation.
probabilities (tuple): A tuple of floats, each providing the probability associated with the corresponding transform equation.
Returns:
A tuple where the first entry is x-values and the second entry is y-values.
"""
# Lists to hold the coordinates with the initial points pre-inserted.
xPoints = [0]
yPoints = [0]
# The initial points
x0 = 0.0
y0 = 0.0
count = 0
print("Starting calculations...")
while count < maximum:
# Pick a transformation and calculate the next value
chosenTransform = choices(transforms, probabilities)[0]
nextValue = chosenTransform(x0, y0)
# Append to list
xPoints.append(nextValue[0])
yPoints.append(nextValue[1])
# Update values
x0 = nextValue[0]
y0 = nextValue[1]
count += 1
return (xPoints, yPoints)