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 shapes are 2D objects extruded into the 3D space. 2D shapes can be defined by a vectorÂ
Following are some shapes in VPython:
The syntax to make a triangle object is as follows:
shapes.triangle(pos, langth, thickness)
where parameters are:
pos
: a two-dimensional position vector that defines the location of the shape. For example, pos=[-2,4]
.
length
: a positive value that defines the overall size of the triangle.
thickness
: a positive value that establishes a cavity inside the triangle.
For example:
An ngon object creates a shape with providedÂ
shapes.ngon(pos, np, length, thickness)
where parameters are:
pos
: a two-dimensional position vector that defines the location of the shape. For example, pos=[-2,4]
.
np
: a positive integer that defines the number of sides.
length
: a positive value that defines the overall size of the ngon.
thickness
: a positive value that establishes a cavity inside the ngon.
For example:
Note: An ngon with
np=3
is a triangle,np=4
is a square,np=5
is a pentagon and, so on.
The syntax for creating a trapezoid is as follows:
shapes.trapezoid(pos, width, height, top)
where parameters are:
pos
: a two-dimensional position vector that defines the location of the shape. For example, pos=[-2,4]
.
width
: a positive value that defines the width of the trapezoid's lower end.
height
: a positive value that sets the overall height of the trapezoid.
top
: a positive value that defines the width of the trapezoid's upper end.
A trapezoid with
pos=[0,0]
, width=5
, height=1
, top=3
A trapezoid with
pos=[0,0]
, width=5
, height=1
, top=7
Note: If
width=top
, then trapezoid will become a cuboid.
The syntax for creating a trapezoid is as follows:
shapes.star(n, radius, iradius)
where parameters are:
n
: a positive integer that defines the star's number of corners.
radius
: a positive value that defines the outer radius of the star.
iradius
: a positive value that defines the inner radius of the star.
A star with
n=7
, radius=8
, iradius=9
A star with
n=7
, radius=8
, iradius=1
To execute the provided example code, follow the steps below:
The example code for the above shapes is as follows:
from vpython import * t = shapes.triangle(pos=[3,3], length=3) triangle_1 = extrusion(path=[vector(0,0,0), vector (0,0,0.01)], shape=t, color=color.red) n = shapes.ngon(pos=[-3,-3], np=7, length=2) ngon_1 = extrusion(path=[vector(0,0,0), vector (0,0,0.01)], shape=n, color=color.yellow) z = shapes.trapezoid(pos=[3,-3], width=3, height=1, top=1) trapezoid_1 = extrusion(path=[vector(0,0,0), vector (0,0,0.01)], shape=z, color=color.cyan) s = shapes.star(pos=[-3,3], n=7, radius=3) star_1 = extrusion(path=[vector(0,0,0), vector (0,0,0.01)], shape=s, color=color.blue)
Note: Read more about
Free Resources