0

I am trying to crate a program that randomly generates line segments using parametric equations. What I have created kinds of does the job, but instead of the lines being disconnected from one another they form one continues line. This is what I have written in 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)

When I run the code this is what is generated plot of what is generated. What I would like to code to do is keep the values of the initial point and direction vector constant while just updating the d with random values.

I have tried doing something like this

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]

but I get an error "setting an array element with a sequence".

  • 1
    Implicitly correcting your indentation and typo: your `t_parameter()` function returns a 2D array (albeit 1 in one dimension), because you return `np.array([t])`. Thus, `d` is two dimensional and `d[i]` is an array instead of a scalar. Just return `t` from `t_parameter()` and you'll be good to go. –  Aug 02 '17 at 22:42
  • 1
    Note that NumPy has a [vectorised uniform random routine](https://docs.scipy.org/doc/numpy-1.12.0/reference/generated/numpy.random.uniform.html#numpy.random.uniform). You can simply do `d = np.random.uniform(-10, 10, n)` and remove the whole `t_parameter()` function. –  Aug 02 '17 at 22:43

0 Answers0