loci.visbio.util

## Class MathUtil

• ```public final class MathUtil
extends Object```
MathUtil contains useful mathematical functions.
• ### Method Summary

All Methods
Modifier and Type Method and Description
`static float[]` ```add(float[] v1, float[] v2)```
`static boolean` ```areDifferent(float[] a, float[] b)```
Tests whether two arrays of floats are different.
`static boolean` ```areOpposite(float[] v1, float[] v2)```
Tests whether two vectors are coordinate-wise opposite.
`static boolean` ```areSame(float[] a, float[] b)```
Tests whether two arrays of floats are identical.
`static float[]` ```computePtOnSegment(float[] a, float[] b, float weight)```
Computes a point along the line segment a[]-b[] based on the parameter weight.
`static float` ```cross2D(float[] v1, float[] v2)```
Computes the cross product of two 2D vectors.
`static float` ```dot(float[] a, float[] b)```
Computes the dot product of two N-D vectors.
`static double` ```getDistance(double[] p, double[] v)```
Computes the minimum distance between the point p and the point v.
`static double` ```getDistance(double[] p, double[] q, double[] m)```
Gets the distance between the points p and q, using the given conversion values between pixels and microns.
`static double` ```getDistance(double[] a, double[] b, double[] v, boolean segment)```
Computes the minimum distance between the point v and the line a-b.
`static double` ```getDistance(float[] p, float[] v)```
Computes the minimum distance between the point p and the point v in double precision.
`static double[]` ```getDistSegWt(double[][] nodes, double x, double y)```
Gets distance to a curve of points connected by straight line segments.
`static double[]` ```getDistSegWt(float[][] nodes, float x, float y)```
Gets distance to a curve of points connected by straight line segments.
`static int` ```getNearest(double val, int min, int max, int step)```
Rounds the value to nearest value along the given progression.
`static double[]` ```getProjection(double[] a, double[] b, double[] v, boolean segment)```
Computes the projection of the point v onto the line segment a-b.
`static float[]` ```getRightBisectorVector2D(float[] p1, float[] p2, float[] p3)```
Gets a unit vector which bisects `(p1 - p2)` and `(p3 - p2)`.
`static float[]` ```getRightPerpendicularVector2D(float[] p2, float[] p1)```
Gets a vector perpendicular to the vector `p2-p1`, pointing to the right with respect to the direction of `p2-p1`.
`static String` ```getValueWithUnit(BigInteger value, int places)```
Returns the given number as a string complete with unit (e.g., kilo, mega, etc.), to the specififed number of decimal places.
`static boolean` ```inside(float[] a, float[] b1, float[] b2)```
Whether the point a is inside the N-D box implied by points b2 and b2 (i.e., in 2D, whether a is inside the box with diagonal b1-b2; in 3D, whether a is inside the cube with diagonal b1-b2).
`static float` `mag(float[] a)`
Computes the magnitude of an N-D vector.
`static float` ```orient2D(float[] p1, float[] p2, float[] p3)```
Obtains the z-coordinate of the cross product of the 2D vectors `p2-p1` and `p3-p2`, useful for determining whether the curve `p1->p2->p3` is curving to the right or left.
`static float[]` ```scalarMultiply(float[] v, float s)```
Multiplies an N-D vector by a scalar.
`static float[]` `unit(float[] v)`
Computes the N-D unit vector in the direction of the vector supplied.
`static float[]` ```vector(float[] p2, float[] p1)```
Creates the vector p2-(minus) p1.
• ### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
• ### Method Detail

• #### getDistance

```public static double getDistance(double[] p,
double[] q,
double[] m)```
Gets the distance between the points p and q, using the given conversion values between pixels and microns.
Parameters:
`p` - Coordinates of the first endpoint
`q` - Coordinates of the second endpoint
`m` - Conversion values between microns and pixels
• #### getDistance

```public static double getDistance(double[] a,
double[] b,
double[] v,
boolean segment)```
Computes the minimum distance between the point v and the line a-b.
Parameters:
`a` - Coordinates of the line's first endpoint
`b` - Coordinates of the line's second endpoint
`v` - Coordinates of the standalone point
`segment` - Whether distance computation should be constrained to the given line segment
• #### getDistance

```public static double getDistance(double[] p,
double[] v)```
Computes the minimum distance between the point p and the point v.
Parameters:
`p` - Coordinates of the first point
`v` - Coordinates of the second point
• #### getDistance

```public static double getDistance(float[] p,
float[] v)```
Computes the minimum distance between the point p and the point v in double precision.
Parameters:
`p` - Coordinates of the first point
`v` - Coordinates of the second point
• #### getProjection

```public static double[] getProjection(double[] a,
double[] b,
double[] v,
boolean segment)```
Computes the projection of the point v onto the line segment a-b.
Parameters:
`a` - Coordinates of the segment's first endpoint
`b` - Coordinates of the segment's second endpoint
`v` - Coordinates of the point to be projected
`segment` - Whether the projection should be constrained to the given line segment
• #### getDistSegWt

```public static double[] getDistSegWt(double[][] nodes,
double x,
double y)```
Gets distance to a curve of points connected by straight line segments. Also returns a segment index and wieght describing the location of the nearest point on the curve (see below).
Parameters:
`x` - x coordinate of point in question
`y` - y coordinate of point in question
Returns:
an array double[3] with element 0 the distance to the nearest point on the curve, element 1 the node index i of one end of closest line segment (the other end being i+1), and element 2 the weight (between zero and one) for determining the location of the closest point by interpolation along the segment (i, i+1).
• #### getDistSegWt

```public static double[] getDistSegWt(float[][] nodes,
float x,
float y)```
Gets distance to a curve of points connected by straight line segments. Also returns a segment index and wieght describing the location of the nearest point on the curve (see below). All math takes place in double precision (floats upcast).
Parameters:
`x` - x coordinate of point in question
`y` - y coordinate of point in question
Returns:
an array double[3] with element 0 the distance to the nearest point on the curve, element 1 the node index i of one end of closest line segment (the other end being i+1), and element 2 the weight (between zero and one) for determining the location of the closest point by interpolation along the segment (i, i+1).
• #### computePtOnSegment

```public static float[] computePtOnSegment(float[] a,
float[] b,
float weight)```
Computes a point along the line segment a[]-b[] based on the parameter weight.
• #### getNearest

```public static int getNearest(double val,
int min,
int max,
int step)```
Rounds the value to nearest value along the given progression.
• #### getValueWithUnit

```public static String getValueWithUnit(BigInteger value,
int places)```
Returns the given number as a string complete with unit (e.g., kilo, mega, etc.), to the specififed number of decimal places.
• #### cross2D

```public static float cross2D(float[] v1,
float[] v2)```
Computes the cross product of two 2D vectors.
• #### unit

`public static float[] unit(float[] v)`
Computes the N-D unit vector in the direction of the vector supplied.
• #### dot

```public static float dot(float[] a,
float[] b)```
Computes the dot product of two N-D vectors.
• #### mag

`public static float mag(float[] a)`
Computes the magnitude of an N-D vector.
• #### vector

```public static float[] vector(float[] p2,
float[] p1)```
Creates the vector p2-(minus) p1.

```public static float[] add(float[] v1,
float[] v2)```
• #### scalarMultiply

```public static float[] scalarMultiply(float[] v,
float s)```
Multiplies an N-D vector by a scalar.
• #### areOpposite

```public static boolean areOpposite(float[] v1,
float[] v2)```
Tests whether two vectors are coordinate-wise opposite.
• #### areSame

```public static boolean areSame(float[] a,
float[] b)```
Tests whether two arrays of floats are identical.
• #### areDifferent

```public static boolean areDifferent(float[] a,
float[] b)```
Tests whether two arrays of floats are different.
• #### inside

```public static boolean inside(float[] a,
float[] b1,
float[] b2)```
Whether the point a is inside the N-D box implied by points b2 and b2 (i.e., in 2D, whether a is inside the box with diagonal b1-b2; in 3D, whether a is inside the cube with diagonal b1-b2).
• #### orient2D

```public static float orient2D(float[] p1,
float[] p2,
float[] p3)```
Obtains the z-coordinate of the cross product of the 2D vectors `p2-p1` and `p3-p2`, useful for determining whether the curve `p1->p2->p3` is curving to the right or left.
• #### getRightPerpendicularVector2D

```public static float[] getRightPerpendicularVector2D(float[] p2,
float[] p1)```
Gets a vector perpendicular to the vector `p2-p1`, pointing to the right with respect to the direction of `p2-p1`.
• #### getRightBisectorVector2D

```public static float[] getRightBisectorVector2D(float[] p1,
float[] p2,
float[] p3)```
Gets a unit vector which bisects `(p1 - p2)` and `(p3 - p2)`. This vector points to the 'right' side of the line defined by `p1->p2->p3`.