mag()

Calculates the magnitude (or length) of a vector.

Examples

example picture for mag()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
def setup():
    x1 = 20
    x2 = 80
    y1 = 30
    y2 = 70

    py5.line(0, 0, x1, y1)
    py5.println(py5.mag(x1, y1))  # Prints "36.05551"
    py5.line(0, 0, x2, y1)
    py5.println(py5.mag(x2, y1))  # Prints "85.44004"
    py5.line(0, 0, x1, y2)
    py5.println(py5.mag(x1, y2))  # Prints "72.8011"
    py5.line(0, 0, x2, y2)
    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: float, b: float) -> float
mag(a: float, b: float, c: float) -> float

Parameters

  • a: float - first value

  • b: float - second value

  • c: float - third value

Updated on September 11, 2021 16:51:34pm UTC