Dart Documentationbox2dMathBox

MathBox class

class MathBox {
 static const double TWO_PI = Math.PI * 2.0;

 MathBox();

 /**
  * Return the distance between the two given vectors, but squared.
  */
 static double distanceSquared(Vector2 v1, Vector2 v2) {
   double dx = (v1.x - v2.x);
   double dy = (v1.y - v2.y);
   return dx * dx + dy * dy;
 }

 /**
  * Return the distance between the two given vectors.
  */
 static double distance(Vector2 v1, Vector2 v2) {
   return Math.sqrt(distanceSquared(v1, v2));
 }

 /** Returns the closest value to [a] that is in between [low] and [high] */
 static double clamp(double a, double low, double high) {
   return Math.max(low, Math.min(a, high));
 }

 /**
  * Given a value within the range specified by [fromMin] and [fromMax],
  * returns a value with the same relative position in the range specified
  * from [toMin] and [toMax]. For example, given a [val] of 2 in the
  * "from range" of 0-4, and a "to range" of 10-20, would return 15.
  */
 static double translateAndScale(double val, double fromMin, double fromMax,
                                 double toMin, double toMax) {
   final double mult = (val - fromMin) / (fromMax - fromMin);
   final double res = toMin + mult * (toMax - toMin);
   return res;
 }
}

Static Properties

const double TWO_PI #

static const double TWO_PI = Math.PI * 2.0

Static Methods

double distanceSquared(Vector2 v1, Vector2 v2) #

Return the distance between the two given vectors, but squared.

static double distanceSquared(Vector2 v1, Vector2 v2) {
 double dx = (v1.x - v2.x);
 double dy = (v1.y - v2.y);
 return dx * dx + dy * dy;
}

double distance(Vector2 v1, Vector2 v2) #

Return the distance between the two given vectors.

static double distance(Vector2 v1, Vector2 v2) {
 return Math.sqrt(distanceSquared(v1, v2));
}

double clamp(double a, double low, double high) #

Returns the closest value to a that is in between low and high

static double clamp(double a, double low, double high) {
 return Math.max(low, Math.min(a, high));
}

double translateAndScale(double val, double fromMin, double fromMax, double toMin, double toMax) #

Given a value within the range specified by fromMin and fromMax, returns a value with the same relative position in the range specified from toMin and toMax. For example, given a val of 2 in the "from range" of 0-4, and a "to range" of 10-20, would return 15.

static double translateAndScale(double val, double fromMin, double fromMax,
                               double toMin, double toMax) {
 final double mult = (val - fromMin) / (fromMax - fromMin);
 final double res = toMin + mult * (toMax - toMin);
 return res;
}

Constructors

new MathBox() #

MathBox();