Pyramid Vertices coordinates

38 views
Skip to first unread message

Andrea Borruso

unread,
Jan 19, 2017, 7:34:43 AM1/19/17
to VPython-users
Hi,
I'm totally a newby.

Starting from a pyramid - mypyr = pyramid(pos=vector(4,2,0), size=vector(8,6,4)) - is there a simple way to extract the coordinates of its five vertex?

Is there something like mypyr.vertex[1].xcords?

Thank you

Bruce Sherwood

unread,
Jan 19, 2017, 8:33:04 AM1/19/17
to VPython-users
At the risk of not really answering your questions, here they are:

vector(4,2-3,0+2) = vector(4,-1,2)
vector(4,2+3,0+2) = vector(4,5,2)
vector(4,2+3,0-2) = vector((4,5,-2)
vector(4,2-3,0-2) = vector(4,-1,-2)
vector(4+8,0,0) = vector(12,0,0)

Here is code that places a red sphere at each corner of the pyramid:

p = pyramid(pos=vector(4,2,0), size=vector(8,6,4))
r = 0.3
sphere(pos=vector(p.pos.x, p.pos.y-p.size.y/2, p.pos.z+p.size.z/2), radius=r, color=color.red)
sphere(pos=vector(p.pos.x, p.pos.y+p.size.y/2, p.pos.z+p.size.z/2), radius=r, color=color.red)
sphere(pos=vector(p.pos.x, p.pos.y+p.size.y/2, p.pos.z-p.size.z/2), radius=r, color=color.red)
sphere(pos=vector(p.pos.x, p.pos.y-p.size.y/2, p.pos.z-p.size.z/2), radius=r, color=color.red)
sphere(pos=vector(p.pos.x+p.size.x, p.pos.y, p.pos.z), radius=r, color=color.red)

But probably the real answer to your question is no, there isn't a VPython function such as mypyr.vertex[1].xcords.

andy

unread,
Jan 19, 2017, 10:01:09 AM1/19/17
to vpytho...@googlegroups.com

On 19 January 2017 at 14:33, Bruce Sherwood <bruce.s...@gmail.com> wrote:
But probably the real answer to your question is no, there isn't a VPython function such as mypyr.vertex[1].xcords.

Wow!!!

You are very kind and also a sort of Groucho Marx :)

Thank you very much and please be patient, probably I will make some other stupid questions

___________________

Andrea Borruso
website: http://blog.spaziogis.it
38° 7' 48" N, 13° 21' 9" E, EPSG:4326
___________________

"cercare e saper riconoscere chi e cosa,
 in mezzo all’inferno, non è inferno, 
e farlo durare, e dargli spazio"

Italo Calvino

Bruce Sherwood

unread,
Jan 19, 2017, 10:12:27 AM1/19/17
to VPython-users
Ecco una funzione che e' molto piu generale per ottenere i vertici del "bounding box" (Here's a function that is much more general to obtain the vertices of the "bounding box"):

def bounding_box(obj, center):
    # center is True for box, sphere, ring
    x = norm(obj.axis)
    y = norm(obj.up)
    z = norm(cross(x,y))
    y = norm(cross(z,x))
    L = obj.size.x
    H = obj.size.y
    W = obj.size.z
    p = vector(obj.pos) # make a copy of obj.pos, so changes to p won't affect the object
    if not center:
        p = p + 0.5*L*x # move to center
    pts = []
    for dx in [-L/2, L/2]:
        for dy in [-H/2, H/2]:
            for dz in [-W/2, W/2]:
                pts.append(p + dx*x + dy*y + dz*z)
    return pts

b = pyramid(pos=vector(4,2,0), axis=vector(3,1,0), size=vector(8,6,4))
c = bounding_box(b, False)
for v in c:
    sphere(pos=v, radius=0.3, color=color.red)

Anche a me piacciono moltissimo i libri di Calvino!

andy

unread,
Jan 19, 2017, 4:14:36 PM1/19/17
to vpytho...@googlegroups.com
Ciao Bruce,
come mai conosci l'italiano? 

Thank you again!

I have a question for you. Your last code is very useful, but I wuold like to change it, but I'm not able to reach my goal.

You have inserted a row to make a copy of obj.pos, so changes to p won't affect the object. 
I would like to have in some way that the vertices follow always the pyramid and if I run myPyramid.rotate(angle=0.5) the spheres follow the rotated pyramid vertices.

Grazie molte



___________________

Andrea Borruso
website: http://blog.spaziogis.it
38° 7' 48" N, 13° 21' 9" E, EPSG:4326
___________________

"cercare e saper riconoscere chi e cosa,
 in mezzo all’inferno, non è inferno, 
e farlo durare, e dargli spazio"

Italo Calvino

--
You received this message because you are subscribed to a topic in the Google Groups "VPython-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vpython-users/6BBbaR4VmwM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Bruce Sherwood

unread,
Jan 19, 2017, 7:10:13 PM1/19/17
to VPython-users
Every time you change the position or orientation or size of the pyramid, just call the function again and use the output to update the positions of the spheres. Note that you should create the spheres just once, perhaps at the start of the program, and continually adjust their positions. (I'm assuming you don't want long trails of many spheres.)

Bruce

Nel 1960 con una borsa di studio Fulbright studiavo fisica nel Instituto Galileo Galilei (che bello!) della Universita' di Padova. Sono tornato in Italia parecchie volte, e continuo di godere libri in italiano (Calvino, Camilleri - e non soltanto Montalabano, Ferrante...).

Bruce Sherwood

unread,
Jan 19, 2017, 9:19:30 PM1/19/17
to VPython-users
Here is an improved version that doesn't require you to specify whether the object's pos is at its center or at the end. Change pyramid to box and the bounding box is still correct. The function isn't perfect, as there will be a problem if up is parallel to axis.

def bounding_box(obj):
    centered = [box, sphere, ellipsoid, ring]
    x = norm(obj.axis)
    y = norm(obj.up)
    z = norm(cross(x,y))
    y = norm(cross(z,x))
    L = obj.size.x
    H = obj.size.y
    W = obj.size.z
    p = vector(obj.pos) # make a copy of obj.pos, so changes to p won't affect the object
    if type(obj) not in centered:
        p = p + 0.5*L*x # move to center
    pts = []
    for dx in [-L/2, L/2]:
        for dy in [-H/2, H/2]:
            for dz in [-W/2, W/2]:
                pts.append(p + dx*x + dy*y + dz*z)
    return pts

b = pyramid(pos=vector(4,2,0), axis=vector(3,1,0), size=vector(8,6,4))
c = bounding_box(b)

John

unread,
Jan 21, 2017, 1:43:02 PM1/21/17
to VPython-users
You might consider creating a compound object out of the spheres and the pyramid so that the spheres rotate with the pyramid when you rotate the compound object. See documentation for an example.


John
To unsubscribe from this group and all its topics, send an email to vpython-user...@googlegroups.com.

Bruce Sherwood

unread,
Jan 21, 2017, 2:46:58 PM1/21/17
to VPython-users
But if he's using Classic VPython there is no compound object. The equivalent in Classic VPython would be to put the pyramid and the spheres in a frame, which is not (yet) available in GlowScript and Jupyter VPythons.

Andrea Borruso

unread,
Jan 23, 2017, 10:33:53 AM1/23/17
to VPython-users
Caro Bruce,


On Friday, 20 January 2017 01:10:13 UTC+1, Bruce Sherwood wrote:
Every time you change the position or orientation or size of the pyramid, just call the function again and use the output to update the positions of the spheres. Note that you should create the spheres just once, perhaps at the start of the program, and continually adjust their positions. (I'm assuming you don't want long trails of many spheres.)

it works.

b.rotate(angle=0.5)
c = bounding_box(b)
for v in c:
    sphere(pos=v, radius=0.3, color=color.red)


How to delete the spheres before the rotation?

Grazie

 

Bruce Sherwood

unread,
Jan 23, 2017, 10:57:58 AM1/23/17
to VPython-users
That's why I said this: "Note that you should create the spheres just once, perhaps at the start of the program, and continually adjust their positions. (I'm assuming you don't want long trails of many spheres.)"

You don't want to create multiple copies of the pyramid, and you also don't want to create multiple copies of the spheres. The code could look something like this:

s = []
for i in range(8):
sphere(color=color.red), 



Bruce Sherwood

unread,
Jan 23, 2017, 11:04:17 AM1/23/17
to VPython-users
That's why I said this: "Note that you should create the spheres just once, perhaps at the start of the program, and continually adjust their positions. (I'm assuming you don't want long trails of many spheres.)"

You don't want multiple copies of the pyramid, so you change the pyramid each time, you don't create a new pyramid each time. Same for the spheres.

A possible structure:

p = pyramid()
s = []
for i in range(8):
    s.append(sphere(color=color.red))
while True:
    rate(2)
    p.rotate(angle=0.5)
    c = bounding_box(p)
    for i in range(8):
        s[i].pos = c[i]
Reply all
Reply to author
Forward
0 new messages