Wanzyee Studio
MathUtility

Include some of common math, about counting, geometry, lerp. More...

Static Public Member Functions

static int Sum (params int[] values)
 Count the sum of int values. More...
 
static float Sum (params float[] values)
 Count the sum of float values. More...
 
static Vector2 Sum (params Vector2[] values)
 Count the sum of UnityEngine.Vector2 values. More...
 
static Vector3 Sum (params Vector3[] values)
 Count the sum of UnityEngine.Vector3 values. More...
 
static Quaternion Sum (params Quaternion[] values)
 Count the sum of UnityEngine.Quaternion values. More...
 
static float SumDistance (params Vector2[] values)
 Count the sum of distances between series of UnityEngine.Vector2. More...
 
static float SumDistance (params Vector3[] values)
 Count the sum of distances between series of UnityEngine.Vector3. More...
 
static float Average (params float[] values)
 Count the average of float values. More...
 
static Vector2 Average (params Vector2[] values)
 Count the average of UnityEngine.Vector2 values. More...
 
static Vector3 Average (params Vector3[] values)
 Count the average of UnityEngine.Vector3 values. More...
 
static Quaternion Average (params Quaternion[] values)
 Count the average of UnityEngine.Quaternion values. More...
 
static Color Average (params Color[] values)
 Count the average of UnityEngine.Color values. More...
 
static Matrix4x4 Average (params Matrix4x4[] values)
 Count the average of UnityEngine.Matrix4x4 values. More...
 
static Vector2 Max (params Vector2[] values)
 Returns a vector that is made from the largest components of all vectors. More...
 
static Vector2 Min (params Vector2[] values)
 Returns a vector that is made from the smallest components of all vectors. More...
 
static Vector3 Max (params Vector3[] values)
 Returns a vector that is made from the largest components of all vectors. More...
 
static Vector3 Min (params Vector3[] values)
 Returns a vector that is made from the smallest components of all vectors. More...
 
static float Lerp (float from, float to, float t, bool clamp=true)
 Interpolates between from and to by t. More...
 
static float InverseLerp (float from, float to, float value, bool clamp=true)
 Calculates the Lerp parameter between of two values. More...
 
static Vector3 Lerp (Vector3 from, Vector3 to, float t, bool clamp=true)
 Interpolates between from and to by t. More...
 
static Vector3 Slerp (Vector3 from, Vector3 to, float t, bool clamp=true)
 Spherically interpolates between from and to by t. More...
 
static Quaternion Lerp (Quaternion from, Quaternion to, float t, bool clamp=true)
 Interpolates between from and to by t. More...
 
static Quaternion Slerp (Quaternion from, Quaternion to, float t, bool clamp=true)
 Spherically interpolates between from and to by t. More...
 
static Matrix4x4 Lerp (Matrix4x4 from, Matrix4x4 to, float t, bool clamp=true)
 Interpolates between from and to by t. More...
 
static Matrix4x4 Slerp (Matrix4x4 from, Matrix4x4 to, float t, bool clamp=true)
 Interpolates between from and to by t. More...
 
static Quaternion SignQuaternion (Quaternion rotation)
 Get new rotation with the corrected sign by converting to and from euler angles. More...
 
static float ProjectFactor (Vector3 point, Vector3 from, Vector3 to, bool clamp=false)
 Projects a point onto a line segment. More...
 
static Vector3 ProjectPoint (Vector3 point, Vector3 from, Vector3 to, bool clamp=false)
 Projects a point onto a line segment. More...
 
static bool PointsInLine (Vector3 p1, Vector3 p2, Vector3 p3, float approx=0f)
 Check if 3 points lying on the same line. More...
 
static bool VectorsInLine (Vector3 v1, Vector3 v2, float approx=0f)
 Check if 2 vectors lying on the same line. More...
 
static bool PointsInPlane (Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float approx=0f)
 Check if 4 points lying in the same plane. More...
 
static bool VectorsInPlane (Vector3 v1, Vector3 v2, Vector3 v3, float approx=0f)
 Check if 3 vectors lying in the same plane. More...
 
static bool PointsToCircle (Vector3 p1, Vector3 p2, Vector3 p3, out Vector3 origin, out float radius, out Vector3 normal)
 Calculate 3 Points to a circle, normal vector by lhs order. More...
 

Detailed Description

Include some of common math, about counting, geometry, lerp.

Member Function Documentation

static int Sum ( params int[]  values)
static

Count the sum of int values.

Parameters
valuesValues.
Returns
The sum.
static float Sum ( params float[]  values)
static

Count the sum of float values.

Parameters
valuesValues.
Returns
The sum.
static Vector2 Sum ( params Vector2[]  values)
static

Count the sum of UnityEngine.Vector2 values.

Parameters
valuesValues.
Returns
The sum.
static Vector3 Sum ( params Vector3[]  values)
static

Count the sum of UnityEngine.Vector3 values.

Parameters
valuesValues.
Returns
The sum.
static Quaternion Sum ( params Quaternion[]  values)
static

Count the sum of UnityEngine.Quaternion values.

Parameters
valuesValues.
Returns
The sum.
static float SumDistance ( params Vector2[]  values)
static

Count the sum of distances between series of UnityEngine.Vector2.

Parameters
valuesValues.
Returns
The sum.
static float SumDistance ( params Vector3[]  values)
static

Count the sum of distances between series of UnityEngine.Vector3.

Parameters
valuesValues.
Returns
The sum.
static float Average ( params float[]  values)
static

Count the average of float values.

Parameters
valuesValues.
Returns
The average.
static Vector2 Average ( params Vector2[]  values)
static

Count the average of UnityEngine.Vector2 values.

Parameters
valuesValues.
Returns
The average.
static Vector3 Average ( params Vector3[]  values)
static

Count the average of UnityEngine.Vector3 values.

Parameters
valuesValues.
Returns
The average.
static Quaternion Average ( params Quaternion[]  values)
static

Count the average of UnityEngine.Quaternion values.

Use average of forward and upward to make smooth.

Parameters
valuesValues.
Returns
The average.
static Color Average ( params Color[]  values)
static

Count the average of UnityEngine.Color values.

Parameters
valuesValues.
Returns
The average.
static Matrix4x4 Average ( params Matrix4x4[]  values)
static

Count the average of UnityEngine.Matrix4x4 values.

Parameters
valuesValues.
Returns
The average.
static Vector2 Max ( params Vector2[]  values)
static

Returns a vector that is made from the largest components of all vectors.

Parameters
valuesValues.
Returns
The max vector.
static Vector2 Min ( params Vector2[]  values)
static

Returns a vector that is made from the smallest components of all vectors.

Parameters
valuesValues.
Returns
The min vector.
static Vector3 Max ( params Vector3[]  values)
static

Returns a vector that is made from the largest components of all vectors.

Parameters
valuesValues.
Returns
The max vector.
static Vector3 Min ( params Vector3[]  values)
static

Returns a vector that is made from the smallest components of all vectors.

Parameters
valuesValues.
Returns
The min vector.
static float Lerp ( float  from,
float  to,
float  t,
bool  clamp = true 
)
static

Interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static float InverseLerp ( float  from,
float  to,
float  value,
bool  clamp = true 
)
static

Calculates the Lerp parameter between of two values.

Return zero if value from is the same as to.

Parameters
fromValue from.
toValue to.
valueValue.
clampIf set to true clamp.
Returns
The lerp time.
static Vector3 Lerp ( Vector3  from,
Vector3  to,
float  t,
bool  clamp = true 
)
static

Interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Vector3 Slerp ( Vector3  from,
Vector3  to,
float  t,
bool  clamp = true 
)
static

Spherically interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Quaternion Lerp ( Quaternion  from,
Quaternion  to,
float  t,
bool  clamp = true 
)
static

Interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Quaternion Slerp ( Quaternion  from,
Quaternion  to,
float  t,
bool  clamp = true 
)
static

Spherically interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Matrix4x4 Lerp ( Matrix4x4  from,
Matrix4x4  to,
float  t,
bool  clamp = true 
)
static

Interpolates between from and to by t.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Matrix4x4 Slerp ( Matrix4x4  from,
Matrix4x4  to,
float  t,
bool  clamp = true 
)
static

Interpolates between from and to by t.

Spherically interpolates axes of matrix., but position and last row linearly.

Parameters
fromValue from.
toValue to.
tTime.
clampIf set to true clamp.
Returns
The lerp result.
static Quaternion SignQuaternion ( Quaternion  rotation)
static

Get new rotation with the corrected sign by converting to and from euler angles.

Rotations present the same from different source might have contrast sign. Use this to get the same signed rotation with the same present.

  1. It might have negative w from Transform.rotation.
  2. It might have negative w from Quaternion.LookRotation().
  3. It always has positive w from Random.rotation.
Parameters
rotationRotation.
Returns
The corrected rotation.
static float ProjectFactor ( Vector3  point,
Vector3  from,
Vector3  to,
bool  clamp = false 
)
static

Projects a point onto a line segment.

Return 0 if the point on the begin of the segment, return 1 if on the end.

Parameters
pointPoint.
fromSegment from.
toSegment to.
clampIf set to true clamp between 0 and 1.
Returns
The lerp factor.
static Vector3 ProjectPoint ( Vector3  point,
Vector3  from,
Vector3  to,
bool  clamp = false 
)
static

Projects a point onto a line segment.

Parameters
pointPoint.
fromSegment from.
toSegment to.
clampIf set to true clamp the point locate on the segment.
Returns
The point on line.
static bool PointsInLine ( Vector3  p1,
Vector3  p2,
Vector3  p3,
float  approx = 0f 
)
static

Check if 3 points lying on the same line.

Parameters
p1Point 1.
p2Point 2.
p3Point 3.
approxApprox range.
Returns
true, if points lying on the same line.
static bool VectorsInLine ( Vector3  v1,
Vector3  v2,
float  approx = 0f 
)
static

Check if 2 vectors lying on the same line.

Parameters
v1Vector 1.
v2Vector 2.
approxApprox range.
Returns
true, if vectors lying on the same line.
static bool PointsInPlane ( Vector3  p0,
Vector3  p1,
Vector3  p2,
Vector3  p3,
float  approx = 0f 
)
static

Check if 4 points lying in the same plane.

Parameters
p0Point 0.
p1Point 1.
p2Point 2.
p3Point 3.
approxApprox range.
Returns
true, if points lying in the same plane.
static bool VectorsInPlane ( Vector3  v1,
Vector3  v2,
Vector3  v3,
float  approx = 0f 
)
static

Check if 3 vectors lying in the same plane.

Parameters
v1Vector 1.
v2Vector 2.
v3Vector 3.
approxApprox range.
Returns
true, if vectors lying in the same plane.
static bool PointsToCircle ( Vector3  p1,
Vector3  p2,
Vector3  p3,
out Vector3  origin,
out float  radius,
out Vector3  normal 
)
static

Calculate 3 Points to a circle, normal vector by lhs order.

Parameters
p1Point 1.
p2Point 2.
p3Point 3.
originOrigin point.
radiusRadius.
normalNormal vector.
Returns
false, if points on the same line, not able to make a circle.