英文:
Plotting a 3-dimensional superball shape
问题
我正在尝试在Python Matplotlib中绘制一个3D超球体,其中超球体被定义为一种通用的数学形状,可以用来描述使用形状参数 p
的圆角立方体,其中对于 p = 1
,形状等同于一个球体。
这篇论文 声称,超球体是通过使用修改后的球坐标来定义的,具体如下:
x = r*cos(u)**1/p * sin(v)**1/p
y = r*cos(u)**1/p * sin(v)**1/p
z = r*cos(v)**1/p
其中 u = phi
且 v = theta
。
我成功让代码运行,至少对于 p = 1
,生成了一个球体 - 正如它应该做的:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
r, p = 1, 1
# 生成数据
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
u, v = np.meshgrid(u, v)
x = r * np.cos(u)**(1/p) * np.sin(v)**(1/p)
y = r * np.sin(u)**(1/p) * np.sin(v)**(1/p)
z = r * np.cos(v)**(1/p)
# 绘制曲面
ax.plot_surface(x, y, z)
plt.show()
这是上述代码的 p = 1
的3D图。
然而,当我为 p
输入任何其他值,例如2,它只给我一个部分形状,而实际上应该给我一个完整的超球体。
这是上述代码的 p = 2
的3D图。
我认为修复更多地涉及到数学,但如何修复这个问题呢?
英文:
I'm trying to plot a 3D superball in python matplotlib, where a superball is defined as a general mathematical shape that can be used to describe rounded cubes using a shape parameter p
, where for p = 1
the shape is equal to that of a sphere.
This paper claims that the superball is defined by using modified spherical coordinates with:
x = r*cos(u)**1/p * sin(v)**1/p
y = r*cos(u)**1/p * sin(v)**1/p
z = r*cos(v)**1/p
with u = phi
and v = theta
.
I managed to get the code running, at least for p = 1
which generates a sphere - exactly as it should do:
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot(projection='3d')
r, p = 1, 1
# Make data
u = np.linspace(0, 2 * np.pi, 100)
v = np.linspace(0, np.pi, 100)
u, v = np.meshgrid(u, v)
x = r * np.cos(u)**(1/p) * np.sin(v)**(1/p)
y = r * np.sin(u)**(1/p) * np.sin(v)**(1/p)
z = r * np.cos(v)**(1/p)
# Plot the surface
ax.plot_surface(x, y, z)
plt.show()
This is a 3D plot of the code above for p = 1
.
However, as I put in any other value for p
, e.g. 2, it's giving me only a partial shape, while it should actually give me a full superball.
This is a 3D plot of the code above for p = 2
.
I believe the fix is more of mathematical nature, but how can this be fixed?
答案1
得分: 4
在绘制常规球体时,我们以不同的方式转换正坐标和负坐标:
- 正坐标:
x**0.5
- 负坐标:
-1 * abs(x)**0.5
对于超级球体的变种,可以使用np.sign
和np.abs
来应用相同的逻辑:
power = lambda base, exp: np.sign(base) * np.abs(base)**exp
x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p)
y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p)
z = r * power(np.cos(v), 1/p)
完整示例,p = 4
:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
r, p = 1, 4
# 生成数据
u = np.linspace(0, 2 * np.pi)
v = np.linspace(0, np.pi)
u, v = np.meshgrid(u, v)
# 转换坐标
# 正坐标:base**exp
# 负坐标:-abs(base)**exp
power = lambda base, exp: np.sign(base) * np.abs(base)**exp
x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p)
y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p)
z = r * power(np.cos(v), 1/p)
# 绘制表面
ax.plot_surface(x, y, z)
plt.show()
英文:
When plotting a regular sphere, we transform positive and negative coordinates differently:
- Positives:
x**0.5
- Negatives:
-1 * abs(x)**0.5
For the superball variants, apply the same logic using np.sign
and np.abs
:
power = lambda base, exp: np.sign(base) * np.abs(base)**exp
x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p)
y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p)
z = r * power(np.cos(v), 1/p)
Full example for p = 4
:
import matplotlib.pyplot as plt
import numpy as np
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})
r, p = 1, 4
# Make the data
u = np.linspace(0, 2 * np.pi)
v = np.linspace(0, np.pi)
u, v = np.meshgrid(u, v)
# Transform the coordinates
# Positives: base**exp
# Negatives: -abs(base)**exp
power = lambda base, exp: np.sign(base) * np.abs(base)**exp
x = r * power(np.cos(u), 1/p) * power(np.sin(v), 1/p)
y = r * power(np.sin(u), 1/p) * power(np.sin(v), 1/p)
z = r * power(np.cos(v), 1/p)
# Plot the surface
ax.plot_surface(x, y, z)
plt.show()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论