Extracting points from trails (Project 3.5c)

15 views
Skip to first unread message

CM

unread,
Oct 15, 2017, 9:14:21 PM10/15/17
to comphys
The trails are vpython curve objects, which are a little strange.  Here is an example curve, c:

import vpython as vp
c = vp.curve(pos=[(0,0,0), (1,0,0), (2,1,0)], radius=0.05)

The syntax for printing the position vector of the 3rd point is:

print(  c.point(2)['pos']   )
which outputs
<2.000000, 1.000000, 0.000000>

(Using 2, not 3, as an input argument, because counting starts at 0 in python)

And for just the x-position, you'd use:
print(  c.point(2)['pos'].x   )
and get
2.0
as output.

This is because, as I understand it:
 The curve c has a method point(...) which returns a dict object, e,g,:
print(  c.point(2)   )
outputs
{'pos': <2.000000, 1.000000, 0.000000>, 'color': <1.000000, 1.000000, 1.000000>, 'radius': 0.05, 'visible': True, 'retain': -1}

dict objects work a little like arrays, except you feed them a string (the key) like
'pos' instead of an integer index.
So
c.point(2) grabs the dict for the 3rd point in the curve c, and c.point(2)['pos'] grabs the 'pos' dictionary value <2.000000, 1.000000, 0.000000>

Hope this helps.

 

CM

unread,
Oct 15, 2017, 9:45:14 PM10/15/17
to comphys
You can also get the number of points in a curve c with len(c._pts).  The documentation seems to say that vpython will never make more than 1000 of them even if you tell it to.  So it does some behind-the-scenes optimization.

Jay Wang

unread,
Oct 15, 2017, 10:30:23 PM10/15/17
to comphys
Nice digging and a useful tip, especially for reconstructing a path, for instance.

Reply all
Reply to author
Forward
0 new messages