Infohazard.Core 1.4.1
Infohazard Core Utility Library
Loading...
Searching...
No Matches
Infohazard.Core.MathUtility Class Reference

Contains utility methods for working with mathematical types and solving math equations. More...

Static Public Member Functions

static float RoundToNearest (float value, float factor)
 Round a value to the nearest multiple of a given factor. More...
 
static float SignZero (float value)
 Same as Mathf.Sign, except that if the input is zero, it returns zero. More...
 
static float NormalizeAngle (float angle)
 Normalize an angle to a value between 0 and 360. More...
 
static Vector3 NormalizeAngles (Vector3 angles)
 Normalize a set of euler angles to values between 0 and 360. More...
 
static float NormalizeInnerAngle (float angle)
 Normalize an angle to a value between -180 and 180. More...
 
static Vector3 NormalizeInnerAngles (Vector3 angles)
 Normalize a set of euler angles to values between -180 and 180. More...
 
static float ClampInnerAngle (float angle, float min, float max)
 Normalize an angle to a value between -180 and 180, then clamp it in the given range. More...
 
static Vector3 ClampInnerAngles (Vector3 angles, Vector3 min, Vector3 max)
 Normalize a set of euler angles to values between -180 and 180, then clamp them in the given ranges. More...
 
static Vector3 Multiply (Vector3 left, Vector3 right)
 Multiply the components of left by the components of right. More...
 
static Vector3 Divide (Vector3 left, Vector3 right)
 Divide the components of left by the components of right. More...
 
static Vector3 Reciprocal (Vector3 vector)
 Take the reciprocal of each component of a vector. More...
 
static Vector3 Divide (float left, Vector3 right)
 Divide a float by each component of a vector. More...
 
static Vector3 RoundToNearest (Vector3 vector, float factor)
 Round a each component of a vector to the nearest multiple of a given factor. More...
 
static bool GetNearestPointOnLines (Ray line1, Ray line2, out float t1, out float t2)
 Find the point along each line where the lines come closest to each other. More...
 
static float GetNearestPointOnLine (Ray line, Vector3 p)
 Get the point on a line where it is nearest to a position. More...
 
static bool GetNearestPointOnSegment (Vector3 v1, Vector3 v2, Vector3 point, out Vector3 pointOnSegment)
 Find the point on a bounded line segment where it is nearest to a position, and return whether that point is in the segment's bounds. More...
 
static Vector3 GetNearestPointOnTriangleIncludingBounds (Vector3 v1, Vector3 v2, Vector3 v3, Vector3 point)
 Find the point on a triangle (including its bounds) where it is nearest to a position. More...
 
static bool GetNearestPointOnTriangle (Vector3 v1, Vector3 v2, Vector3 v3, Vector3 point, out Vector3 pointOnTriangle)
 Find the point on a triangle where it is nearest to a position, and return whether that point is in the triangle's bounds. More...
 
static bool IsPointInsideBound (Vector3 v1, Vector3 v2, Vector3 normal, Vector3 point)
 Returns true if a given point is on the inner side (defined by a given normal) of a segment. More...
 
static bool DoesSegmentIntersectTriangle (Vector3 v1, Vector3 v2, Vector3 v3, Vector3 s1, Vector3 s2, out float t)
 Raycast a line segment against a triangle, and return whether they intersect. More...
 
static Vector3 WorldToCanvasPoint (this Camera camera, Canvas canvas, Vector3 point)
 Projects a point in the world onto a canvas in camera or overlay space. More...
 
static Vector3 GetPerpendicularVector (this Vector3 vector)
 Returns a vector that is perpendicular to the given vector. More...
 
static int Dot (Vector3Int v1, Vector3Int v2)
 Dot product of two int vectors. More...
 
static Vector3 WithX (this Vector3 v, float x)
 Replace the X component of a vector. More...
 
static Vector3 WithY (this Vector3 v, float y)
 Replace the Y component of a vector. More...
 
static Vector3 WithZ (this Vector3 v, float z)
 Replace the Z component of a vector. More...
 
static Vector2 WithX (this Vector2 v, float x)
 Replace the X component of a vector. More...
 
static Vector2 WithY (this Vector2 v, float y)
 Replace the Y component of a vector. More...
 
static Vector3 WithZ (this Vector2 v, float z)
 Convert a Vector2 to a Vector3 with the given Z. More...
 
static Vector3 AsXY (this Vector2 v)
 Get a Vector3 with the components (x, y, 0). More...
 
static Vector3 AsYX (this Vector2 v)
 Get a Vector3 with the components (y, x, 0). More...
 
static Vector3 AsXZ (this Vector2 v)
 Get a Vector3 with the components (x, 0, y). More...
 
static Vector3 AsZX (this Vector2 v)
 Get a Vector3 with the components (y, 0, x). More...
 
static Vector3 AsYZ (this Vector2 v)
 Get a Vector3 with the components (0, x, y). More...
 
static Vector3 AsZY (this Vector2 v)
 Get a Vector3 with the components (0, y, x). More...
 
static Vector2 ToXY (this Vector3 v)
 Get a Vector2 with the components (x, y). More...
 
static Vector2 ToYX (this Vector3 v)
 Get a Vector2 with the components (y, x). More...
 
static Vector2 ToXZ (this Vector3 v)
 Get a Vector2 with the components (x, z). More...
 
static Vector2 ToZX (this Vector3 v)
 Get a Vector2 with the components (z, x). More...
 
static Vector2 ToYZ (this Vector3 v)
 Get a Vector2 with the components (y, z). More...
 
static Vector2 ToZY (this Vector3 v)
 Get a Vector2 with the components (z, y). More...
 
static Vector4 ToV4Pos (this Vector3 vector)
 Get a Vector4 with the components (x, y, z, 1). More...
 
static Vector4 ToV4 (this Vector3 vector)
 Get a Vector4 with the components (x, y, z, 0). More...
 
static Quaternion XYRotation (Vector3 right, Vector3 upHint)
 Get a quaternion based on a right vector and approximate up vector. More...
 
static Quaternion YXRotation (Vector3 up, Vector3 rightHint)
 Get a quaternion based on a up vector and approximate right vector. More...
 
static Quaternion XZRotation (Vector3 right, Vector3 forwardHint)
 Get a quaternion based on a right vector and approximate forward vector. More...
 
static Quaternion ZXRotation (Vector3 forward, Vector3 rightHint)
 Get a quaternion based on a forward vector and approximate right vector. More...
 
static Quaternion YZRotation (Vector3 up, Vector3 forwardHint)
 Get a quaternion based on a up vector and approximate forward vector. More...
 
static Quaternion ZYRotation (Vector3 forward, Vector3 upHint)
 Get a quaternion based on a forward vector and approximate up vector. More...
 
static void GetCorners (this Bounds bounds, Vector3[] corners)
 Get the eight corners of a bounding box and save them in the given array. More...
 
static bool BoundsToScreenRect (Transform transform, Bounds bounds, Func< Vector3, Vector3 > worldToScreen, out Rect rect)
 Get a screen rect that encapsulates the given bounds. More...
 
static void SplitHorizontal (Rect rect, float gap, out Rect out1, out Rect out2, float div=0.5f)
 Split a rect into two halves horizontally, with given gap between the halves. More...
 
static void SplitHorizontal (Rect rect, float gap, out Rect out1, out Rect out2, out Rect out3, float div1=1.0f/3.0f, float div2=2.0f/3.0f)
 Split a rect into three thirds horizontally, with given gap between the thirds. More...
 

Static Public Attributes

static Complex
 Evaluate all cubic roots of this Complex. More...
 
static Complex r1
 Solve a quadratic equation (find x such that the result is zero) in the form ax^2 + bx + c = 0. More...
 
static readonly Vector3[] BoundsCornerArray = new Vector3[8]
 A static array that can be used to store the output of GetCorners, as long as the values are copied from the array right away. More...
 

Detailed Description

Contains utility methods for working with mathematical types and solving math equations.

Member Function Documentation

◆ AsXY()

static Vector3 Infohazard.Core.MathUtility.AsXY ( this Vector2  v)
static

Get a Vector3 with the components (x, y, 0).

◆ AsXZ()

static Vector3 Infohazard.Core.MathUtility.AsXZ ( this Vector2  v)
static

Get a Vector3 with the components (x, 0, y).

◆ AsYX()

static Vector3 Infohazard.Core.MathUtility.AsYX ( this Vector2  v)
static

Get a Vector3 with the components (y, x, 0).

◆ AsYZ()

static Vector3 Infohazard.Core.MathUtility.AsYZ ( this Vector2  v)
static

Get a Vector3 with the components (0, x, y).

◆ AsZX()

static Vector3 Infohazard.Core.MathUtility.AsZX ( this Vector2  v)
static

Get a Vector3 with the components (y, 0, x).

◆ AsZY()

static Vector3 Infohazard.Core.MathUtility.AsZY ( this Vector2  v)
static

Get a Vector3 with the components (0, y, x).

◆ BoundsToScreenRect()

static bool Infohazard.Core.MathUtility.BoundsToScreenRect ( Transform  transform,
Bounds  bounds,
Func< Vector3, Vector3 >  worldToScreen,
out Rect  rect 
)
static

Get a screen rect that encapsulates the given bounds.

Parameters
transformParent that the bounds are attached to (can be null).
boundsThe input bounds.
worldToScreenA function that converts world points to screen points, such as Camera.WorldToScreenPoint.
rectA screen rect that encapsulates the bounds.
Returns
Whether a screen rect could be calculated (false if completely off screen).

◆ ClampInnerAngle()

static float Infohazard.Core.MathUtility.ClampInnerAngle ( float  angle,
float  min,
float  max 
)
static

Normalize an angle to a value between -180 and 180, then clamp it in the given range.

Parameters
angleInput angle.
minMin clamp value (applied after normalize).
maxMax Clamp value (applied after normalize).
Returns
Angle between min and max.

◆ ClampInnerAngles()

static Vector3 Infohazard.Core.MathUtility.ClampInnerAngles ( Vector3  angles,
Vector3  min,
Vector3  max 
)
static

Normalize a set of euler angles to values between -180 and 180, then clamp them in the given ranges.

Parameters
anglesInput angles.
minMin clamp values (applied after normalize).
maxMax Clamp values (applied after normalize).
Returns
Angles between min and max.

◆ Divide() [1/2]

static Vector3 Infohazard.Core.MathUtility.Divide ( float  left,
Vector3  right 
)
static

Divide a float by each component of a vector.

◆ Divide() [2/2]

static Vector3 Infohazard.Core.MathUtility.Divide ( Vector3  left,
Vector3  right 
)
static

Divide the components of left by the components of right.

◆ DoesSegmentIntersectTriangle()

static bool Infohazard.Core.MathUtility.DoesSegmentIntersectTriangle ( Vector3  v1,
Vector3  v2,
Vector3  v3,
Vector3  s1,
Vector3  s2,
out float  t 
)
static

Raycast a line segment against a triangle, and return whether they intersect.

Parameters
v1The first triangle point.
v2The second triangle point.
v3The third triangle point.
s1The start of the segment.
s2The end of the segment.
tThe point along the input segment where it intersects the triangle, or -1.
Returns
Whether the segment intersects the triangle.

◆ Dot()

static int Infohazard.Core.MathUtility.Dot ( Vector3Int  v1,
Vector3Int  v2 
)
static

Dot product of two int vectors.

◆ GetCorners()

static void Infohazard.Core.MathUtility.GetCorners ( this Bounds  bounds,
Vector3[]  corners 
)
static

Get the eight corners of a bounding box and save them in the given array.

You can use BoundsCornerArray to avoid allocating here.

Parameters
boundsThe input bounds.
cornersArray to save the values in.

◆ GetNearestPointOnLine()

static float Infohazard.Core.MathUtility.GetNearestPointOnLine ( Ray  line,
Vector3  p 
)
static

Get the point on a line where it is nearest to a position.

Parameters
lineThe input line.
pThe input position.
Returns
THe point along the line where it is nearest to the position.

◆ GetNearestPointOnLines()

static bool Infohazard.Core.MathUtility.GetNearestPointOnLines ( Ray  line1,
Ray  line2,
out float  t1,
out float  t2 
)
static

Find the point along each line where the lines come closest to each other.

If the lines are parallel, then return false.

Parameters
line1The first line.
line2The second line.
t1The point along the first line where they are closest to intersecting.
t2The point along the second line where they are closest to intersecting.
Returns
False if the lines are parallel, true otherwise.

◆ GetNearestPointOnSegment()

static bool Infohazard.Core.MathUtility.GetNearestPointOnSegment ( Vector3  v1,
Vector3  v2,
Vector3  point,
out Vector3  pointOnSegment 
)
static

Find the point on a bounded line segment where it is nearest to a position, and return whether that point is in the segment's bounds.

Does not return points on the ends of the segment. If the nearest point on the segment's line is outside the segment, will fail and not return a valid point.

Parameters
v1The start of the segment.
v2The end of the segment.
pointThe point to search for.
pointOnSegmentThe point on the segment closest to the input point.
Returns
Whether the nearest point is within the segment's bounds.

◆ GetNearestPointOnTriangle()

static bool Infohazard.Core.MathUtility.GetNearestPointOnTriangle ( Vector3  v1,
Vector3  v2,
Vector3  v3,
Vector3  point,
out Vector3  pointOnTriangle 
)
static

Find the point on a triangle where it is nearest to a position, and return whether that point is in the triangle's bounds.

Does not return points on the edge of the triangle. If the nearest point on the triangle's plane is outside the triangle, will fail and not return a valid point.

Parameters
v1The first triangle point.
v2The second triangle point.
v3The third triangle point.
pointThe point to search for.
pointOnTriangleThe point on the triangle closest to the input point.
Returns
Whether the nearest point is within the triangle's bounds.

◆ GetNearestPointOnTriangleIncludingBounds()

static Vector3 Infohazard.Core.MathUtility.GetNearestPointOnTriangleIncludingBounds ( Vector3  v1,
Vector3  v2,
Vector3  v3,
Vector3  point 
)
static

Find the point on a triangle (including its bounds) where it is nearest to a position.

If nearest point is on the triangle's bounds, that point will be returned, unlike GetNearestPointOnTriangle.

Parameters
v1The first triangle point.
v2The second triangle point.
v3The third triangle point.
pointThe point to search for.
Returns
The nearest point on the triangle to the given point.

◆ GetPerpendicularVector()

static Vector3 Infohazard.Core.MathUtility.GetPerpendicularVector ( this Vector3  vector)
static

Returns a vector that is perpendicular to the given vector.

Parameters
vectorInput vector.
Returns
A perpendicular vector.

◆ IsPointInsideBound()

static bool Infohazard.Core.MathUtility.IsPointInsideBound ( Vector3  v1,
Vector3  v2,
Vector3  normal,
Vector3  point 
)
static

Returns true if a given point is on the inner side (defined by a given normal) of a segment.

Parameters
v1The start of the segment.
v2The end of the segment.
normalThe normal, defining which side is inside.
pointThe point to search for.
Returns
Whether the point is on the inner side.

◆ Multiply()

static Vector3 Infohazard.Core.MathUtility.Multiply ( Vector3  left,
Vector3  right 
)
static

Multiply the components of left by the components of right.

◆ NormalizeAngle()

static float Infohazard.Core.MathUtility.NormalizeAngle ( float  angle)
static

Normalize an angle to a value between 0 and 360.

Parameters
angleInput angle.
Returns
Angle between 0 and 360.

◆ NormalizeAngles()

static Vector3 Infohazard.Core.MathUtility.NormalizeAngles ( Vector3  angles)
static

Normalize a set of euler angles to values between 0 and 360.

Parameters
anglesInput angles.
Returns
Angles between 0 and 360.

◆ NormalizeInnerAngle()

static float Infohazard.Core.MathUtility.NormalizeInnerAngle ( float  angle)
static

Normalize an angle to a value between -180 and 180.

Parameters
angleInput angle.
Returns
Angle between -180 and 180.

◆ NormalizeInnerAngles()

static Vector3 Infohazard.Core.MathUtility.NormalizeInnerAngles ( Vector3  angles)
static

Normalize a set of euler angles to values between -180 and 180.

Parameters
anglesInput angles.
Returns
Angles between -180 and 180.

◆ Reciprocal()

static Vector3 Infohazard.Core.MathUtility.Reciprocal ( Vector3  vector)
static

Take the reciprocal of each component of a vector.

◆ RoundToNearest() [1/2]

static float Infohazard.Core.MathUtility.RoundToNearest ( float  value,
float  factor 
)
static

Round a value to the nearest multiple of a given factor.

Parameters
valueInput value.
factorValue to round to a multiple of.
Returns
Rounded value.

◆ RoundToNearest() [2/2]

static Vector3 Infohazard.Core.MathUtility.RoundToNearest ( Vector3  vector,
float  factor 
)
static

Round a each component of a vector to the nearest multiple of a given factor.

Parameters
vectorInput values.
factorValue to round to a multiple of.
Returns
Rounded values.

◆ SignZero()

static float Infohazard.Core.MathUtility.SignZero ( float  value)
static

Same as Mathf.Sign, except that if the input is zero, it returns zero.

Parameters
valueA number to get the sign of.
Returns
1 if the number is positive, -1 if the number is negative, 0 if the number is 0.

◆ SplitHorizontal() [1/2]

static void Infohazard.Core.MathUtility.SplitHorizontal ( Rect  rect,
float  gap,
out Rect  out1,
out Rect  out2,
float  div = 0.5f 
)
static

Split a rect into two halves horizontally, with given gap between the halves.

Parameters
rectRect to split.
gapGap between the split halves.
out1Output rect 1.
out2Output rect 2.
divThe ratio of the total space taken up by the left rect.

◆ SplitHorizontal() [2/2]

static void Infohazard.Core.MathUtility.SplitHorizontal ( Rect  rect,
float  gap,
out Rect  out1,
out Rect  out2,
out Rect  out3,
float  div1 = 1.0f / 3.0f,
float  div2 = 2.0f / 3.0f 
)
static

Split a rect into three thirds horizontally, with given gap between the thirds.

Parameters
rectRect to split.
gapGap between the split halves.
out1Output rect 1.
out2Output rect 2.
out3Output rect 3.
div1The ratio of the total space taken up by the left rect.
div2The ratio of the total space taken up by the left and center rect.

◆ ToV4()

static Vector4 Infohazard.Core.MathUtility.ToV4 ( this Vector3  vector)
static

Get a Vector4 with the components (x, y, z, 0).

◆ ToV4Pos()

static Vector4 Infohazard.Core.MathUtility.ToV4Pos ( this Vector3  vector)
static

Get a Vector4 with the components (x, y, z, 1).

◆ ToXY()

static Vector2 Infohazard.Core.MathUtility.ToXY ( this Vector3  v)
static

Get a Vector2 with the components (x, y).

◆ ToXZ()

static Vector2 Infohazard.Core.MathUtility.ToXZ ( this Vector3  v)
static

Get a Vector2 with the components (x, z).

◆ ToYX()

static Vector2 Infohazard.Core.MathUtility.ToYX ( this Vector3  v)
static

Get a Vector2 with the components (y, x).

◆ ToYZ()

static Vector2 Infohazard.Core.MathUtility.ToYZ ( this Vector3  v)
static

Get a Vector2 with the components (y, z).

◆ ToZX()

static Vector2 Infohazard.Core.MathUtility.ToZX ( this Vector3  v)
static

Get a Vector2 with the components (z, x).

◆ ToZY()

static Vector2 Infohazard.Core.MathUtility.ToZY ( this Vector3  v)
static

Get a Vector2 with the components (z, y).

◆ WithX() [1/2]

static Vector2 Infohazard.Core.MathUtility.WithX ( this Vector2  v,
float  x 
)
static

Replace the X component of a vector.

◆ WithX() [2/2]

static Vector3 Infohazard.Core.MathUtility.WithX ( this Vector3  v,
float  x 
)
static

Replace the X component of a vector.

◆ WithY() [1/2]

static Vector2 Infohazard.Core.MathUtility.WithY ( this Vector2  v,
float  y 
)
static

Replace the Y component of a vector.

◆ WithY() [2/2]

static Vector3 Infohazard.Core.MathUtility.WithY ( this Vector3  v,
float  y 
)
static

Replace the Y component of a vector.

◆ WithZ() [1/2]

static Vector3 Infohazard.Core.MathUtility.WithZ ( this Vector2  v,
float  z 
)
static

Convert a Vector2 to a Vector3 with the given Z.

◆ WithZ() [2/2]

static Vector3 Infohazard.Core.MathUtility.WithZ ( this Vector3  v,
float  z 
)
static

Replace the Z component of a vector.

◆ WorldToCanvasPoint()

static Vector3 Infohazard.Core.MathUtility.WorldToCanvasPoint ( this Camera  camera,
Canvas  canvas,
Vector3  point 
)
static

Projects a point in the world onto a canvas in camera or overlay space.

Similar to Camera.WorldToScreenPoint, but scaled to the size of the canvas and its viewport. Logs an error if the canvas is in world space, as that is not supported.

Parameters
cameraThe camera to use for reference.
canvasThe canvas to use for reference.
pointThe world point to find on the canvas.
Returns
The point on the canvas, usable as an anchoredPosition.

◆ XYRotation()

static Quaternion Infohazard.Core.MathUtility.XYRotation ( Vector3  right,
Vector3  upHint 
)
static

Get a quaternion based on a right vector and approximate up vector.

◆ XZRotation()

static Quaternion Infohazard.Core.MathUtility.XZRotation ( Vector3  right,
Vector3  forwardHint 
)
static

Get a quaternion based on a right vector and approximate forward vector.

◆ YXRotation()

static Quaternion Infohazard.Core.MathUtility.YXRotation ( Vector3  up,
Vector3  rightHint 
)
static

Get a quaternion based on a up vector and approximate right vector.

◆ YZRotation()

static Quaternion Infohazard.Core.MathUtility.YZRotation ( Vector3  up,
Vector3  forwardHint 
)
static

Get a quaternion based on a up vector and approximate forward vector.

◆ ZXRotation()

static Quaternion Infohazard.Core.MathUtility.ZXRotation ( Vector3  forward,
Vector3  rightHint 
)
static

Get a quaternion based on a forward vector and approximate right vector.

◆ ZYRotation()

static Quaternion Infohazard.Core.MathUtility.ZYRotation ( Vector3  forward,
Vector3  upHint 
)
static

Get a quaternion based on a forward vector and approximate up vector.

Member Data Documentation

◆ BoundsCornerArray

readonly Vector3 [] Infohazard.Core.MathUtility.BoundsCornerArray = new Vector3[8]
static

A static array that can be used to store the output of GetCorners, as long as the values are copied from the array right away.

◆ Complex

static Infohazard.Core.MathUtility.Complex
static

Evaluate all cubic roots of this Complex.

Parameters
complexThe number to get the cube roots of.
Returns
All three complex cube roots.

◆ r1

static Complex Infohazard.Core.MathUtility.r1
static

Solve a quadratic equation (find x such that the result is zero) in the form ax^2 + bx + c = 0.

Solve a quartic equation (find x such that the result is zero) of the form ax^4 + bx^3 + cx^2 + dx + e = 0.

Solve a cubic equation (find x such that the result is zero) in the form ax^3 + bx^2 + cx + d = 0.

Parameters
aThe coefficient for the x^2 term.
bThe coefficient for the x term.
cThe constant term.
Returns
The two roots of the quadratic equation, which may be complex.
Parameters
aThe coefficient for the x^3 term.
bThe coefficient for the x^2 term.
cThe coefficient for the x term.
dThe constant term.
Returns
The three roots of the cubic, which may be complex.
Parameters
aThe coefficient for the x^4 term.
bThe coefficient for the x^3 term.
cThe coefficient for the x^2 term.
dThe coefficient for the x term.
eThe constant term.
Returns
The four roots of the quartic, which may be complex.

The documentation for this class was generated from the following file: