point2line
index
 
Name squareMagnitude ( )
Description Returns the square magnitude (square length) of the vector.

The square root function which is used when calling magnitude() is horrendously slow, so try to avoid calculating vector lengths whenever you can. A common problem in computer graphics is to find the shortest vector in a list, in this case you only need to calculate the square magnitude (x*x + y*y) for each of them and find the smallest value from that (since the vector with the shortest length will also have the smallest squared length).
Syntax
squareMagnitude();
Returns square magnitude of this vector (float)
Usage Web & Application
Related magnitude ( )
setMagnitude ( )
Examples
/**
*  using squareMagnitude() to make a cheap distance test.
*  use isGreaterThan and isLessThan as convenience methods for the same.
*/

import point2line.*;

Vect2 mouse;
Vect2 center;
float radius, diameter;

void setup(){
  size( 200, 200 );
  smooth();
  
  mouse = new Vect2();
  center = new Vect2( width*0.5, height*0.5 );
  radius = width * 0.4;
  diameter = radius * 2;
}

void draw()
{
  // test for hover //
  mouse.set( mouseX, mouseY );
  mouse.subtract( center );
  boolean isHovering = ( mouse.squareMagnitude() < radius * radius );

  // display //
  background( 0 );
  if( isHovering ) fill( 0, 255, 0 );
   else fill( 255, 0, 0 );
  ellipse( center.x, center.y, diameter, diameter );
}