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.
Extrusion is a fundamental operation in computer graphics and 3D modeling. It involves creating a three-dimensional object by pulling the 2D shape outwards to give it depth and volume. The resulting object is often referred to as an extruded shape.
Syntax for the extrusion()
method is as follows:
extrusion(path[], shape, color)
where the parameters are:
path[]
: This parameter takes two vectors as input. The first vector defines the direction of the object, and the second one defines the thickness.
shape
: This parameter inputs the 2D object being extruded into 3D space.
color
: It sets the color of the object being extruded.
To execute the provided example code, we must follow the steps mentioned below:
The following example explains how the extrusion()
method works:
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)
Line 1: Importing the VPython library.
Line 3: A 2D triangle
object is created and stored in the variable t
.
Lines 4–7: The extrusion()
method then places the triangle at the origin's position and extends it to a 0.01 scale on the z-axis. The shape
argument takes t
as an input which is our triangle. We have also provided red color to the triangle.
Line 9: A 2D ngon
object is created and stored in the variable n
.
Lines 10–13: The extrusion()
method then places the ngon at the origin's position and extends it to a 0.01 scale on the z-axis. The shape
argument takes t
as an input which is our ngon
. We have also provided yellow color to the ngon
.
Line 15: A 2D trapezoid
object is created and stored in the variable z
.
Lines 16–19: The extrusion()
method then places the trapezoid at the origin's position and extends it to a 0.01 scale on the z-axis. The shape
argument takes z
as an input which is our trapezoid. We have also provided cyan color to the trapezoid.
Line 21: A 2D star
object is created and stored in the variable s
.
Lines 22–25: The extrusion()
method then places the star at the origin's position and extends it to a 0.01 scale on the z-axis. The star
argument takes s
as an input which is our star. We have also provided blue color to the trapezoid.
Free Resources