In order to plot a squircle using its parametric equations:
The equations can be found in page 9 of this this article:
Fong, Chamberlain. "Squircular Calculations." arXiv preprint arXiv:1604.02174 (2016).
Unfortunately the following singularities are not handled by those equations:
- For
t = 0
,t = pi/2
,t = pi
,t = 3pi/2
, andt=2*pi
Here is the code to plot these equations in Python
"""Plot squircle shape using its parametric equations"""
import numpy as np
from numpy import pi, sqrt, cos, sin, sign
import matplotlib.pyplot as plt
# FG stands for Fernandez-Guasti who introduced the algebric equations.
def FG_param(r, s, t):
x = np.ones_like(t)
y = np.ones_like(t)
for i, _t in enumerate(t):
if np.isclose(_t, 0.0):
x[i] = r
y[i] = 0.0
elif np.isclose(_t, pi/2.0):
x[i] = 0.0
y[i] = r
elif np.isclose(_t, pi):
x[i] = -r
y[i] = 0.0
elif np.isclose(_t, 3*pi/2.0):
x[i] = 0.0
y[i] = -r
elif np.isclose(_t, 2*pi):
x[i] = r
y[i] = 0.0
else:
x[i] = r*sign(cos(t[i]))/(s*sqrt(2)*np.abs(sin(t[i])))*sqrt(1 - sqrt(1-s**2*sin(2*t[i])**2))
y[i] = r*sign(sin(t[i]))/(s*sqrt(2)*np.abs(cos(t[i])))*sqrt(1 - sqrt(1-s**2*sin(2*t[i])**2))
return x, y
s = 0.7
r = 1.0
NTHETA = 90
t = np.linspace(0, 2*pi, NTHETA)
x, y = FG_param(r, s, t)
plt.gca().set_aspect('equal')
plt.scatter(x, y, s=5)
plt.show()
The function FG_param
looks very clumsy. I appreciate any help to make it more compact and neat.
Thank you
0
or1
if they are coerced to; for example,np.isclose(_t, pi/2.0)
returns1
if true, so that can be inverted (display zero if matched) and then multiplied by the RHS to remove it; the equation can be restructured to ber + xsin(...)
, wherex
is the condition. This can be repeated for all remaining conditions and structured accordingly. I may provide a code sample \$\endgroup\$x[i] = r * \ (np.isclose(_t, 0.0) or np.isclose(_t, 2*pi)) * \ (not np.isclose(_t, 0.0) or np.isclose(_t, 2*pi)) * \ (1 - (not np.isclose(_t, pi))) * \ (not np.isclose(_t, 3*pi/2.0))
\$\endgroup\$