Extrusion 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.
Extrusion
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
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.
Code execution
To execute the provided example code, we must follow the steps mentioned below:
Example code
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)Code explanation
Line 1: Importing the VPython library.
Line 3: A 2D
triangleobject is created and stored in the variablet.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. Theshapeargument takestas an input which is our triangle. We have also provided red color to the triangle.Line 9: A 2D
ngonobject is created and stored in the variablen.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. Theshapeargument takestas an input which is ourngon. We have also provided yellow color to thengon.Line 15: A 2D
trapezoidobject is created and stored in the variablez.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. Theshapeargument takeszas an input which is our trapezoid. We have also provided cyan color to the trapezoid.Line 21: A 2D
starobject is created and stored in the variables.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. Thestarargument takessas an input which is our star. We have also provided blue color to the trapezoid.
Continue reading
Free Resources