VPython (Visual Python) is a 3D graphics library that allows us to create and visualize three-dimensional objects on the screen. It is primarily used to visualize the impact of physics equations on the objects' motion.
Note: Read more about VPython.
VPython objects are the fundamental elements we can manipulate to create different three-dimensional models for simulation purposes.
Following are some of the fundamental objects:
The sphere
object represents a 3D sphere defined by its pos
(short for postion) and radius
. The syntax to display a sphere is as follows:
sphere(pos=vector(x,y,z), radius=A, color=color.B)
x
, y
, and z
are the integers that define the coordinates of the position vector for the sphere, A
is the floating point number for the sphere's radius, and B
represents a color.
The box
object represents a cuboid defined by its pos
(short for position), axis
, length
, height
, and width
. The syntax to display a box is as follows:
box(pos=vector(x,y,z), axis=vector(a,b,c), length=A, height=B, width=C, up=vector(d,e,f))
x
, y
, and z
are the integers that define the coordinates of the position vector for the box. a
, b
, and c
are the integers that define the position vector of the axis of the box. A
, B
, and C
are floating points that define the box's length, height, and width. d
, e
, and f
are the integers that define the position vector of the top face of the box in 3D space.
The cylinder
object represents a three-dimensional cylinder defined by its pos
(short for position), axis
, and radius
. The syntax to display a cylinder is as follows:
cylinder(pos=vector(x,y,z), axis=vector(a,b,c), radius=A)
x
, y
, and z
are the integers that define the coordinates of the position vector for the cylinder. a
, b
, and c
are the integers that define the position vector of the cylinder's axis. A
is the floating point that defines the cylinder's radius.
The cone
object represents a three-dimensional cone defined by its pos
(short for position), axis
, and radius
. The syntax to display a cone is the same as that of a cylinder:
cone(pos=vector(x,y,z), axis=vector(a,b,c), radius=A)
x
, y
, and z
are the integers that define the coordinates of the position vector for the cone. a
, b
, and c
are the integers that define the position vector of the cone's axis. A
is the floating point that defines the cone's radius.
The text
object displays the text on the screen as graphics depending upon its alignment and height. The syntax to display a cone is the same as that of a cylinder:
text(text="xyz", align="abc", color=color.A, height=B)
"xyz"
is the text we want to display. "abc"
is the alignment of the text. A
is the color and B
is the height of the text.
To execute the code, follow the following steps:
Following is the implementation of every object discussed above:
from vpython import * mybox = box(pos=vector(7,27,0), axis=vector(5,5,55), length=10.0, height=20.0, width=30.0, up=vector(5,5,10)) ball = sphere(pos=vector(-55,-5,0), radius=10, color=color.red) rod = cylinder(pos=vector(80,2,1), axis=vector(5,0,5), radius=10) conettoo = cone(pos=vector(0,-40,0), axis=vector(5,10,5), radius=5, color=color.green) text(text='My text is\ngreen', align='center', color=color.green,height=10.0)
Free Resources