mag()#

Calculates the magnitude (or length) of a vector.

Examples#

example picture for mag()
 1def setup():
 2    x1 = 20
 3    x2 = 80
 4    y1 = 30
 5    y2 = 70
 6
 7    py5.line(0, 0, x1, y1)
 8    py5.println(py5.mag(x1, y1))  # Prints "36.05551"
 9    py5.line(0, 0, x2, y1)
10    py5.println(py5.mag(x2, y1))  # Prints "85.44004"
11    py5.line(0, 0, x1, y2)
12    py5.println(py5.mag(x1, y2))  # Prints "72.8011"
13    py5.line(0, 0, x2, y2)
14    py5.println(py5.mag(x2, y2))  # Prints "106.30146"

Description#

Calculates the magnitude (or length) of a vector. A vector is a direction in space commonly used in computer graphics and linear algebra. Because it has no “start” position, the magnitude of a vector can be thought of as the distance from the coordinate (0, 0) to its (x, y) value. Therefore, mag() is a shortcut for writing dist(0, 0, x, y).

Syntax#

mag(a: Union[float, npt.NDArray], b: Union[float, npt.NDArray], /) -> float
mag(a: Union[float, npt.NDArray], b: Union[float, npt.NDArray], c: Union[float, npt.NDArray], /) -> float

Parameters#

  • a: Union[float, npt.NDArray] - first value

  • b: Union[float, npt.NDArray] - second value

  • c: Union[float, npt.NDArray] - third value

Updated on February 26, 2022 13:22:44pm UTC