我正在试图创建一个使用参数方程随机生成线段的程序。我创造了各种各样的工作,但不是线路彼此断开,而是形成一条连续线。这就是我在python中编写的内容。
enter import numpy as np
import random as rand
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.set_aspect("equal")
npoints = 10
V = np.zeros(npoints)
def point1 (npoints):
x0 = np.zeros(npoints)
y0 = np.zeros(npoints)
z0 = np.zeros(npoints)
for k in range (npoints):
theta = rand.uniform(0.0, np.pi)
phi = rand.uniform(0.0, (2 * np.pi))
x0[k] = 10 * np.sin(phi) * np.cos(theta)
y0[k] = 10 * np.sin(phi) * np.sin(theta)
z0[k] = 10 * np.cos(theta)
return np.array([x0,y0,z0])
def point2 (npoints):
x1 = np.zeros(npoints)
y1 = np.zeros(npoints)
z1 = np.zeros(npoints)
for j in range (npoints):
theta = rand.uniform(0.0, np.pi)
phi = rand.uniform(0.0, (2 * np.pi))
x1[j] = 10 * np.sin(phi) * np.cos(theta)
y1[j] = 10 * np.sin(phi) * np.sin(theta)
z1[j] = 10 * np.cos(theta)
return np.array([x1,y1,z1])
n = 10
def t_parameter(n):
t = np.zeros(n)
for i in range (n):
t[i] = rand.uniform(-10,10)
return np.array([t])
p1 = point1(npoints)
p2 = point2(npoints)
V = p2-p1
d = t_paramiter(n)
Lx = d*V[0]+p1[0]
Ly = d*V[1]+p1[1]
Lz = d*V[2]+p1[2]
ax.plot_wireframe(Lx,Ly,Lz)
当我运行代码时,这就是生成的plot of what is generated。我想编写的代码是保持初始点和方向向量的值不变,同时只用随机值更新d。
我尝试过做这样的事情
Lx = np.zeros(npoints)
Ly = np.zeros(npoints)
Lz = np.zeros(npoints)
for i in range (n):
Lx[i] = d[i]*V[i]+p1[i]
Ly[i] = d[i]*V[i]+p1[i]
Lz[i] = d[i]*V[i]+p1[i]
但是我得到一个错误"用序列"设置数组元素。
答案 0 :(得分:0)
我没有足够的代表发表评论,但有几个快速说明:
point1
,point2
和t_parameter
。d = t_paramiter(n)
应为d = t_parameter(n)
。请注意,您只是绘制随机行的一系列终点:
d*V[0]+p1[0]
是终点的x值,而不是行。我的知识在这里结束,但你需要分别绘制10个单独的两个系列,而你正在绘制一个系列中有10个点。
希望有所帮助!