The Student Room Group

how to call the x component of a vector in python

how would you write the x component of :
a = sphere(pos = vector(1,2,3)) *example
Original post by localmemelord
how would you write the x component of :
a = sphere(pos = vector(1,2,3)) *example

That doesn't look like correct Python to me... what module is this?
Original post by MrSandyWilly
That doesn't look like correct Python to me... what module is this?


vpython
Isn't the x-component this:

a = sphere(pos = vector(1,2,3))
Original post by MrSandyWilly
Isn't the x-component this:

a = sphere(pos = vector(1,2,3))

no i mean, what is the line of code that calls the x component of that vector
Reply 5
a.pos[0]? I'm assuming sphere is a class

never heard of vpython, I'd have just used a numpy array
(edited 3 years ago)
Original post by Sinnoh
a.pos[0]? I'm assuming sphere is a class

never heard of vpython, I'd have just used a numpy array

that doesnt work unfortunately, a typeerror forms:
"'vpython.cyvector.vector' object is not subscriptable"
Reply 7
Original post by localmemelord
that doesnt work unfortunately, a typeerror forms:
"'vpython.cyvector.vector' object is not subscriptable"


ugh.

It must have its own method for calling the component then
Original post by Sinnoh
ugh.

It must have its own method for calling the component then

it seems so
Reply 9
What about print()

See the bottom of the documentation page:

https://www.glowscript.org/docs/VPythonDocs/VisualIntro.html
Original post by just_m3
What about print()

See the bottom of the documentation page:

https://www.glowscript.org/docs/VPythonDocs/VisualIntro.html

wouldnt that print out all of the components instead of only the x component
Reply 11
Original post by localmemelord
wouldnt that print out all of the components instead of only the x component

I've never used vPython, but doesn't the paragraph headed "Vectors" on that site indicate that if a is a vPython vector object then you may refer to its components as a.x, a.y, a.z etc?
Reply 12
Original post by localmemelord
how would you write the x component of :
a = sphere(pos = vector(1,2,3)) *example

I was looking forever and figured it out through trial and error. VPython uses 'vector_name.x' to call on the x component and so on.

# Define a vector
my_vector = vector(1, 2, 3)

# Access individual components
x_component = my_vector.x
y_component = my_vector.y
z_component = my_vector.z

Quick Reply

Latest

Trending

Trending