Arrow with 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.

Arrow object

The arrow object in VPython is used to represent a vector as an arrow in a 3D space. It has a shaft and head.

A sample VPython arrow object
A sample VPython arrow object

Syntax

The arrow object is created using the following syntax:

arrow(pos=vector(x, y, z),
axis=vector(dx, dy, dz),
opacity=A,
length=B,
shaftwidth=C,
headlength=D,
headwidth=E,
color=Color)
  • pos is a vector that defines the position of the arrow in three-dimensional space.

  • axis is also a vector, but it defines the direction in which the arrow will be pointing.

  • opacity determines how visible the arrow should be. A is a floating point value ranging from 00 to 11 where 00 corresponds to the most transparent object and 11 corresponds to an opaque object.

  • length determines the overall length of the arrow object. B can be any positive value.

  • shaftwidth sets the width of the arrow's body. C can be any positive value.

  • headlength defines the length of the arrowhead. D can be any positive integer value.

  • headwidth sets the width of the arrowhead. E can be any positive value.

  • color sets the color of the arrow object. The value of the color parameter can be any RGB vector (r, g, b) or a pre-defined color value such as color.red or color.orange, etc.

Code execution

To execute the provided example code, we must follow the following steps:

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

Example code

The example code for the arrow object is as follows:

from vpython import *
  
arrow(pos = vector(0, 0, 0),
      opacity = 1,
      length = 3,
      color = color.yellow)

arrow(pos = vector(4, 0, 0),
      axis = vector(1,1,1),
      opacity = 0.3,
      length = 3,
      color = color.orange)


arrow(pos = vector(0, 2, 0),
      opacity = 1,
      length = 6,
      color = color.red)
  
arrow(pos = vector(0, 5, 0),
      opacity = 1,
      length = 3,
      shaftwidth = 4,
      color = color.blue)

arrow(pos = vector(0, -3, 0),
      opacity = 1,
      length = 3,
      headwidth = 4,
      color = color.cyan)

arrow(pos = vector(0, -6, 0),
      opacity = 1,
      length = 3,
      headlength = 4,
      color = color.magenta)


# Create X-axis arrow
arrow_x = arrow(pos=vector(0, 0, 0), axis=vector(1, 0, 0), color=color.red, shaftwidth=0.05, length = 10)

# Create Y-axis arrow
arrow_y = arrow(pos=vector(0, 0, 0), axis=vector(0, 1, 0), color=color.green, shaftwidth=0.05, length = 10)

# Create Z-axis arrow
arrow_z = arrow(pos=vector(0, 0, 0), axis=vector(0, 0, 1), color=color.blue, shaftwidth=0.05, length = 10)
Example code to compare multiple arrows with different parameters

Code explanation

  • Line 1: Importing the VPython library.

  • Lines 3–6: We declared this arrow object as a reference so that you could distinguish how other parameters work.

  • Lines 8–12: This orange-colored arrow has less opacity and will appear transparent. Moreover, it has the axis vector of (1, 1, 1), which means it will be pointing in the direction of it.

  • Lines 15–18: This red-colored arrow has a greater length than the others.

  • Lines 20–24: The shaftwidth of this arrow has been increased, making its body slightly wider than the other arrows.

  • Lines 26–30: This cyan-colored arrow's headwidth has increased somewhat.

  • Lines 32–36: The last arrow has a greater headlength as compared to the other arrows.

  • Lines 40–46: These lines create the reference coordinate axes.

Note: Read more about

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved