Skip to content

Instantly share code, notes, and snippets.

@oweidner
Last active September 5, 2022 19:09
Show Gist options
  • Save oweidner/87926a66092bc05a74910d5c971544b5 to your computer and use it in GitHub Desktop.
Save oweidner/87926a66092bc05a74910d5c971544b5 to your computer and use it in GitHub Desktop.
Generalized SuperformulaU in Julia
using Pkg
Pkg.add("Plots")
using Plots
gr()
# Generalized SuperformulaU 2D returns the radius for a given angle phi.
SuperformulaU2D(phi, a, b, y, z , n1, n2, n3) =
( abs( cos(y*phi/4) / a )^n2 + abs( sin(z*phi/4) / b )^n3 ) ^ (-1/n1)
# Tuples holding "interesting" values for a, b, y, z, n1, n2, n3
params = [
# Rotationally symmetric structures using the same values for y and z
(1,1,3,3,4.5,10,10), (1,1,4,4,12,15,15), (1,1,7,7,10,6,6), (1,1,5,5,4,4,4),
(1,1,5,5,2,2,7), (1,1,5,5,2,13,13), (1,1,4,4,1,1,1), (1,1,4,4,1,7,8),
(1,1,6,6,1,7,8), (1,1,2,2,2,2,2), (1,1,1,1,0.5,0.5,0.5), (1,1,2,2,0.5,0.5,0.5),
(1,1,3,3,0.5,0.5,0.5), (1,1,5,5,1,1,1), (1,1,2,2,1,1,1), (1,1,7,7,3,4,17),
(1,1,2,2,1,4,8), (1,1,6,6,1,4,8), (1,1,7,7,2,8,4), (1,1,4,4,0.5,0.5,4),
(1,1,8,8,0.5,0.5,8), (1,1,16,16,0.5,0.5,16), (1,1,3,3,30,15,15), (1,1,4,4,30,15,15),
# Rotationally asymmetric structures using different values for y and z
(1,1,4,6,3,1,1), (1, 1, 61, -3,3,1,1), (1, 1, 2, 10,1.5,1,1), (1, 1, 2, 10,-1.5,1,1),
(1, 1, 2, 44,-0.2,1,1), (1, 1, 8, 40,-0.2,1,1), (1, 1, 88, 64,3,1,1), (1, 1, 88, 64,-20,1,1)
]
plots = []
# create a new plot for each value tuple and push it to the plots array
for i in params
local ts = LinRange(0, 2pi, 10000)
local rs = SuperformulaU2D.(ts, i[1], i[2], i[3], i[4], i[5], i[6], i[7])
local p = plot(ts, rs, proj=:polar, showaxis=:off, legend=:no, title=i)
push!(plots, p)
end
# 'splatting' (unpacking) the array of plots into subplots using ...
plot(plots..., layout = (8,4), size = (1800, 2000), titlefontsize=10)
# save plot to file
savefig("./superformula2D.png")
using Pkg
# The GLMakie backend renders the output in a native
# OpenGL desktop window.
Pkg.add("GLMakie")
using GLMakie
GLMakie.activate!()
# The WGLMakie backend renders the output as WebGL in a
# browser window.
# Pkg.add("WGLMakie")
# using WGLMakie
# WGLMakie.activate!()
# Generalized SuperformulaU 2D returns the radius for a given angle phi.
SuperformulaU2D(phi, a, b, y, z , n1, n2, n3) =
( abs( cos(y*phi/4) / a )^n2 + abs( sin(z*phi/4) / b )^n3 ) ^ (-1/n1)
u = LinRange(-pi, pi, 1000)
v = LinRange(-pi/2, pi/2, 1000)
x = [SuperformulaU2D(ui, 1,1,8,8,0.5,0.5,8) * cos(ui) * SuperformulaU2D(vj, 1,1,8,8,0.5,0.5,8) * cos(vj) for ui in u, vj in v]
y = [SuperformulaU2D(ui, 1,1,8,8,0.5,0.5,8) * sin(ui) * SuperformulaU2D(vj, 1,1,8,8,0.5,0.5,8) * cos(vj) for ui in u, vj in v]
z = [SuperformulaU2D(vj, 1,1,8,8,0.5,0.5,8) * sin(vj) for ui in u, vj in v]
# surface() implicitly creates and returns a Figure object which can be
# controlled via the figure keyword.
figure, = surface(x, y, z, figure = (resolution = (1800, 1200),), axis=(type=Axis3,))
# save plot to file - supresses interactive output window
# save("superformula3D.png", figure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment