VPython 2D shapes

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.

Shapes

VPython shapes are 2D objects extruded into the 3D space. 2D shapes can be defined by a vector (x,y)(x,y). Since VPython is a library that visualizes 3D objects, two-dimensional shapes have to be extruded into three-dimensional space.

Following are some shapes in VPython:

Triangle

The syntax to make a triangle object is as follows:

shapes.triangle(pos, langth, thickness)
Syntax for VPython triangle

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:

A triangle with pos=[0,0], length=5 and thickness=1
A triangle with pos=[0,0], length=5 and thickness=1

Ngon

An ngon object creates a shape with provided nn sides. The syntax for creating an ngon is similar to that of a triangle, but it has an additional parameter:

shapes.ngon(pos, np, length, thickness)
Syntax for VPython ngon

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:

An ngon with pos=[0,0], np=9, length=5, and thickness=1
An ngon with pos=[0,0], np=9, length=5, and thickness=1

Note: An ngon with np=3 is a triangle, np=4 is a square, np=5 is a pentagon and, so on.

Trapezoid

The syntax for creating a trapezoid is as follows:

shapes.trapezoid(pos, width, height, top)
Syntax for VPython trapezoid

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.

Star

The syntax for creating a trapezoid is as follows:

shapes.star(n, radius, iradius)
Syntax for VPython star

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

Code execusion

To execute the provided example code, follow the steps below:

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

Example code

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)

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved