VPython objects

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.

Objects

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:

Sphere

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)
Syntax to create a sphere

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.

Box

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))
Syntax to create a box

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.

Cylinder

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)
Syntax to create a cylinder

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.

Cone

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)
Syntax to create a cone

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.

Text

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)
Syntax to display the text

"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.

Example code

To execute the code, follow the following steps:

Once you click the "Run" button, follow the encircled link
1 of 4

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

Copyright ©2024 Educative, Inc. All rights reserved