-
Notifications
You must be signed in to change notification settings - Fork 3
/
superformula.py
executable file
·65 lines (51 loc) · 2.04 KB
/
superformula.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
63
64
65
"""
Creates superformula samples using two variables.
Author(s): Jonah Chazan ([email protected]), Wei Chen ([email protected])
"""
import numpy as np
import math
def get_sf_parameters(variables, alpha, beta):
'''
v[0]: s
v[1]: t
'''
parameters = []
for v in variables:
# Set [w, h, m, n1, n2, n3]
parameters.append([v[0]/10, 1, 3+math.floor(v[0]+v[1])%beta, 1, 8+alpha*(v[0]-10), 8+alpha*(v[1]-10)])
# parameters.append([v[0]/10, 1, 4+math.floor(v[0]+v[1])%beta, 2, 7+alpha*(v[1]-10), 7+alpha*(v[1]-10)]) # sf-mix
# parameters.append([v[1]/10, 1, 5+math.floor(v[0]+v[1])%beta, 2, 7+alpha*(v[1]-10), 7+alpha*(v[1]-10)]) # sf-d1
# parameters.append([v[0]/10, 1, 4+math.floor(v[0]+v[1])%beta, 1, 7+alpha*(v[1]-10), 7+alpha*(v[1]-10)]) # sf-roll
return np.array(parameters)
def superformula(w, h, m, n1, n2, n3, num_points=1000):
phis = np.linspace(0, 2 * math.pi, num_points)
def r(phi):
# Force a, b to be 1 so we have a more linear example
a = 1
b = 1
aux = abs(math.cos(m * phi / 4) / a) ** n2 + abs(math.sin(m * phi / 4) / b) ** n3
return aux ** (-1.0/n1)
r = np.vectorize(r, otypes=[np.float])
rs = r(phis)
# Use w, h to scale width and height
x = w * rs * np.cos(phis)
y = h * rs * np.sin(phis)
# Scale the shapes so that they have the same height
mn = min(y)
mx = max(y)
h = mx-mn
y /= h
x /= h
return (x, y)
def variables_to_data(variables, alpha, beta, n_points):
parameters = get_sf_parameters(variables, alpha, beta)
x_plots = []
for p in parameters:
x, y = superformula(*p, num_points=n_points)
xy = np.concatenate((x.reshape(-1,1), y.reshape(-1,1)), axis=1).flatten()
x_plots.append(xy)
n_samples = len(variables)
data = np.zeros((n_samples, 2*n_points))
for index in range(n_samples):
data[index,:] = np.reshape(x_plots[index], 2*n_points)
return data