Circles in VPython
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.
Circles
In VPython, a circle is a closed curve consisting of all points in a plane at a fixed distance (radius) from a central point (center). A circle is a 2D object, which means it lies entirely within a single plane. However, it has to be viewed in a 3D space in VPython.
Syntax
The syntax for the VPython circle is:
shapes.circle(pos, radius, np, thickness)
where parameters are:
pos: A 2D vectorthat defines the circle's position in the two-dimensional plane. radius: A positive value that sets the radius of the circle.np: A positive integer that specifies how circular the circumference contour should be. By default, it is set to. thickness: A positive floating point value that creates a cavity inside the circle making it a ring.
Note: Since VPython is a 3D graphic library, we have to extrude the circle object into 3D space using
extrusion()method.
Code execution
To execute the provided example code, we have to follow the steps mentioned below:
Example code
from vpython import *
t = shapes.circle(pos=[3,3], radius=3, np=12, thickness=0.6)
triangle_1 = extrusion(path=[vector(0,0,0),
vector (0,0,0.01)],
shape=t,
color=color.red)
s = shapes.circle(pos=[-2,-1], radius=3, np=34)
triangle_2 = extrusion(path=[vector(0,0,0),
vector (0,0,0.01)],
shape=s,
color=color.yellow)Code explanation
Line 1: Importing VPython library.
Line 3: Creating a circle object with some
thicknessand lessnpvalue.Lines 4–7: Extruding the 2D circle in a 3D space.
pathsets the axis of the circle andvectorsets the thickness of the circle along the, , and -axis, respectively. The assigned color is red.Line 10: Creating another circle with
np=34value and no thickness.Lines 11–14: Extruding the 2D circle in a 3D space.
pathsets the axis of the circle andvectorsets the thickness of the circle along the, , and -axis, respectively. The assigned color is yellow.
Continue reading
Free Resources