0

我正在尝试创建一个使用参数方程随机生成线段的程序。我创造的东西可以完成这项工作,但不是将线路彼此断开,而是形成一条连续线路。这是我用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)

当我运行代码时,这就是生成的情节。我想做的代码是保持初始点和方向向量的值不变,同时用随机值更新 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]

但我收到错误“使用序列设置数组元素”。

4

0 回答 0