Skip to content

Instantly share code, notes, and snippets.

@mate-h
Last active March 26, 2023 01:38
Show Gist options
  • Save mate-h/aa399b413f682ad7e177b7686659fdc8 to your computer and use it in GitHub Desktop.
Save mate-h/aa399b413f682ad7e177b7686659fdc8 to your computer and use it in GitHub Desktop.
Superformula

superformula

Steps

  1. Clone the gist
  2. Install ASDF from https://asdf-vm.com
  3. Install python plugin and the latest python version
asdf plugin-add python
asdf list-all python
asdf install python 3.11.0
asdf global python 3.11.0
  1. Install requirements
pip install -r superformula.txt
  1. Run the script
./superformula.py

Further reading: https://github.com/mate-h/supershape

#!/usr/bin/env python3
import plotly.express as px
import pandas as pd
import numpy as np
import math
import progressbar
def generate_dataset():
# Define the parameter ranges (adjust these as needed)
m_values = np.linspace(0, 10, 11)
n1_values = np.linspace(1, 5, 5)
n2_values = np.linspace(1, 5, 5)
n3_values = np.linspace(1, 5, 5)
angle_values = np.linspace(0, 2 * math.pi, 360*2)
dataset = []
total_iterations = len(m_values) * len(n1_values) * \
len(n2_values) * len(n3_values) * len(angle_values)
green_text = "\033[32m"
end_color = "\033[0m"
progress_bar = progressbar.ProgressBar(
max_value=total_iterations,
widgets=[
progressbar.Bar(marker="━",
left="["+green_text, right=end_color+"]"),
progressbar.Percentage(),
" ",
progressbar.ETA(),
],
)
# Use nested loops to calculate the superformula for each combination of parameter values
for m in m_values:
for n1 in n1_values:
for n2 in n2_values:
for n3 in n3_values:
for angle in angle_values:
a = 1
b = 1
# n2 = 5
# n3 = 5
# n1 = 5
# m = 4
radius = superformula(
a, b, m, n1, n2, n3, angle)
row = [a, b, m, n1, n2, n3, angle, radius]
dataset.append(row)
progress_bar.update(progress_bar.value + 1)
progress_bar.finish()
# Convert the dataset to a NumPy array
dataarr = np.array(dataset)
df = pd.DataFrame(dataarr, columns=[
"a", "b", "m", "n1", "n2", "n3", "angle", "radius"])
print("Dataset shape: ", dataarr.shape)
print("Dataset size: ", dataarr.size)
print("Dataset dtype: ", dataarr.dtype)
return df
def superformula(a, b, m, n1, n2, n3, angle):
# Calculate the superformula
return math.pow(
math.pow(math.fabs(math.cos(m * angle / 4) / a), n2) +
math.pow(math.fabs(math.sin(m * angle / 4) / b), n3),
-1 / n1
)
def show_plot():
# Create a DataFrame with column labels
df = generate_dataset()
df["angle"] = df["angle"] * 180 / math.pi
fig = px.line_polar(df, r="radius", theta="angle",
line_close=True, color="m")
# remove elements from the plot except the lines
fig.update_layout(
showlegend=False,
polar=dict(
radialaxis=dict(
visible=False,
),
angularaxis=dict(
visible=False,
),
bgcolor="rgba(0,0,0,0)",
),
margin=dict(l=0, r=0, t=0, b=0),
)
fig.update_traces(line_width=.33)
# set page background color
fig.update_layout(
paper_bgcolor="#212121",
)
# Show the plot
fig.show()
if __name__ == "__main__":
show_plot()
numpy
plotly
pandas
packaging
progressbar2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment