Name

mag()

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 coordinate (0,0) to its (x,y) value. Therefore, mag() is a shortcut for writing dist(0, 0, x, y).

Examples

  • size(400, 400);
    
    float x1 = 80;
    float x2 = 320;
    float y1 = 120;
    float y2 = 280;
    
    line(0, 0, x1, y1);
    println(mag(x1, y1));  // Prints "144.22205"
    line(0, 0, x2, y1);
    println(mag(x2, y1));  // Prints "341.76016"
    line(0, 0, x1, y2);
    println(mag(x1, y2));  // Prints "291.2044"
    line(0, 0, x2, y2);
    println(mag(x2, y2));  // Prints "425.20584"
    Image output for example 1

Syntax

  • mag(a, b)
  • mag(a, b, c)

Parameters

  • a(float)first value
  • b(float)second value
  • c(float)third value

Return

  • float

Related