A* Pathfinding Project  4.0.4
The A* Pathfinding Project for Unity 3D
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Events Macros Groups Pages
VectorMath Class Reference

Various vector math utility functions. More...

Detailed Description

Various vector math utility functions.

Version
A lot of functions in the Polygon class have been moved to this class the names have changed slightly and everything now consistently assumes a left handed coordinate system now instead of sometimes using a left handed one and sometimes using a right handed one. This is why the 'Left' methods in the Polygon class redirect to methods named 'Right'. The functionality is exactly the same.

Note the difference between segments and lines. Lines are infinitely long but segments have only a finite length.

Static Public Member Functions

static Vector3 ClampMagnitudeXZ (Vector3 v, float maxMagnitude)
 
static Vector3 ClosestPointOnLine (Vector3 lineStart, Vector3 lineEnd, Vector3 point)
 Returns the closest point on the line.
 
static float ClosestPointOnLineFactor (Vector3 lineStart, Vector3 lineEnd, Vector3 point)
 Factor along the line which is closest to the point.
 
static float ClosestPointOnLineFactor (Int3 lineStart, Int3 lineEnd, Int3 point)
 Factor along the line which is closest to the point.
 
static float ClosestPointOnLineFactor (Int2 lineStart, Int2 lineEnd, Int2 point)
 Factor of the nearest point on the segment.
 
static Vector3 ClosestPointOnSegment (Vector3 lineStart, Vector3 lineEnd, Vector3 point)
 Returns the closest point on the segment.
 
static Vector3 ClosestPointOnSegmentXZ (Vector3 lineStart, Vector3 lineEnd, Vector3 point)
 Returns the closest point on the segment in the XZ plane.
 
static bool IsClockwiseMarginXZ (Vector3 a, Vector3 b, Vector3 c)
 Returns if the points a in a clockwise order.
 
static bool IsClockwiseOrColinear (Int2 a, Int2 b, Int2 c)
 Returns true if the points a in a clockwise order or if they are colinear.
 
static bool IsClockwiseOrColinearXZ (Int3 a, Int3 b, Int3 c)
 Returns true if the points a in a clockwise order or if they are colinear.
 
static bool IsClockwiseXZ (Vector3 a, Vector3 b, Vector3 c)
 Returns if the points a in a clockwise order.
 
static bool IsClockwiseXZ (Int3 a, Int3 b, Int3 c)
 Returns if the points a in a clockwise order.
 
static bool IsColinear (Vector2 a, Vector2 b, Vector2 c)
 Returns if the points are colinear (lie on a straight line)
 
static bool IsColinearAlmostXZ (Int3 a, Int3 b, Int3 c)
 Returns if the points are colinear (lie on a straight line)
 
static bool IsColinearXZ (Int3 a, Int3 b, Int3 c)
 Returns if the points are colinear (lie on a straight line)
 
static bool IsColinearXZ (Vector3 a, Vector3 b, Vector3 c)
 Returns if the points are colinear (lie on a straight line)
 
static float LineCircleIntersectionFactor (Vector3 circleCenter, Vector3 linePoint1, Vector3 linePoint2, float radius)
 Intersection of a line and a circle.
 
static Vector3 LineDirIntersectionPointXZ (Vector3 start1, Vector3 dir1, Vector3 start2, Vector3 dir2)
 Intersection point between two infinite lines.
 
static Vector3 LineDirIntersectionPointXZ (Vector3 start1, Vector3 dir1, Vector3 start2, Vector3 dir2, out bool intersects)
 Intersection point between two infinite lines.
 
static bool LineIntersectionFactorXZ (Int3 start1, Int3 end1, Int3 start2, Int3 end2, out float factor1, out float factor2)
 Returns the intersection factors for line 1 and line 2.
 
static bool LineIntersectionFactorXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, out float factor1, out float factor2)
 Returns the intersection factors for line 1 and line 2.
 
static float LineIntersectionFactorXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2)
 Returns the intersection factor for line 1 with line 2.
 
static Vector2 LineIntersectionPoint (Vector2 start1, Vector2 end1, Vector2 start2, Vector2 end2)
 Returns the intersection point between the two lines.
 
static Vector2 LineIntersectionPoint (Vector2 start1, Vector2 end1, Vector2 start2, Vector2 end2, out bool intersects)
 Returns the intersection point between the two lines.
 
static Vector3 LineIntersectionPointXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2)
 Returns the intersection point between the two lines.
 
static Vector3 LineIntersectionPointXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, out bool intersects)
 Returns the intersection point between the two lines.
 
static float LineRayIntersectionFactorXZ (Int3 start1, Int3 end1, Int3 start2, Int3 end2)
 Returns the intersection factor for line 1 with ray 2.
 
static float MagnitudeXZ (Vector3 v)
 
static Vector3 Normalize (Vector3 v, out float magnitude)
 Normalize vector and also return the magnitude.
 
static Vector2 Normalize (Vector2 v, out float magnitude)
 Normalize vector and also return the magnitude.
 
static bool RaySegmentIntersectXZ (Int3 start1, Int3 end1, Int3 start2, Int3 end2)
 Returns if the ray (start1, end1) intersects the segment (start2, end2).
 
static bool ReversesFaceOrientations (Matrix4x4 matrix)
 True if the matrix will reverse orientations of faces.
 
static bool ReversesFaceOrientationsXZ (Matrix4x4 matrix)
 True if the matrix will reverse orientations of faces in the XZ plane.
 
static bool RightOrColinear (Vector2 a, Vector2 b, Vector2 p)
 Returns if p lies on the right side of the line a - b.
 
static bool RightOrColinear (Int2 a, Int2 b, Int2 p)
 Returns if p lies on the right side of the line a - b.
 
static bool RightOrColinearXZ (Vector3 a, Vector3 b, Vector3 p)
 Returns if p lies on the left side of the line a - b.
 
static bool RightOrColinearXZ (Int3 a, Int3 b, Int3 p)
 Returns if p lies on the left side of the line a - b.
 
static bool RightXZ (Vector3 a, Vector3 b, Vector3 p)
 Returns if p lies on the right side of the line a - b.
 
static bool RightXZ (Int3 a, Int3 b, Int3 p)
 Returns if p lies on the right side of the line a - b.
 
static Vector3 SegmentIntersectionPointXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2, out bool intersects)
 Returns the intersection point between the two line segments in XZ space.
 
static bool SegmentIntersectsBounds (Bounds bounds, Vector3 a, Vector3 b)
 Does the line segment intersect the bounding box.
 
static bool SegmentsIntersect (Int2 start1, Int2 end1, Int2 start2, Int2 end2)
 Returns if the line segment start2 - end2 intersects the line segment start1 - end1.
 
static bool SegmentsIntersectXZ (Int3 start1, Int3 end1, Int3 start2, Int3 end2)
 Returns if the line segment start2 - end2 intersects the line segment start1 - end1.
 
static bool SegmentsIntersectXZ (Vector3 start1, Vector3 end1, Vector3 start2, Vector3 end2)
 Returns if the two line segments intersects.
 
static long SignedTriangleAreaTimes2XZ (Int3 a, Int3 b, Int3 c)
 Signed area of a triangle in the XZ plane multiplied by 2.
 
static float SignedTriangleAreaTimes2XZ (Vector3 a, Vector3 b, Vector3 c)
 Signed area of a triangle in the XZ plane multiplied by 2.
 
static float SqrDistancePointSegment (Vector3 a, Vector3 b, Vector3 p)
 Returns the squared distance between p and the segment a-b.
 
static float SqrDistancePointSegmentApproximate (int x, int z, int px, int pz, int qx, int qz)
 Returns the approximate shortest squared distance between x,z and the segment p-q.
 
static float SqrDistancePointSegmentApproximate (Int3 a, Int3 b, Int3 p)
 Returns the approximate shortest squared distance between x,z and the segment p-q.
 
static float SqrDistanceSegmentSegment (Vector3 s1, Vector3 e1, Vector3 s2, Vector3 e2)
 3D minimum distance between 2 segments.
 
static float SqrDistanceXZ (Vector3 a, Vector3 b)
 Squared distance between two points in the XZ plane.
 

Member Function Documentation

static Vector3 ClampMagnitudeXZ ( Vector3  v,
float  maxMagnitude 
)
static
static Vector3 ClosestPointOnLine ( Vector3  lineStart,
Vector3  lineEnd,
Vector3  point 
)
static

Returns the closest point on the line.

The line is treated as infinite.

See Also
ClosestPointOnSegment
ClosestPointOnLineFactor
static float ClosestPointOnLineFactor ( Vector3  lineStart,
Vector3  lineEnd,
Vector3  point 
)
static

Factor along the line which is closest to the point.

Returned value is in the range [0,1] if the point lies on the segment otherwise it just lies on the line. The closest point can be calculated using (end-start)*factor + start.

See Also
ClosestPointOnLine
ClosestPointOnSegment
static float ClosestPointOnLineFactor ( Int3  lineStart,
Int3  lineEnd,
Int3  point 
)
static

Factor along the line which is closest to the point.

Returned value is in the range [0,1] if the point lies on the segment otherwise it just lies on the line. The closest point can be calculated using (end-start)*factor + start

static float ClosestPointOnLineFactor ( Int2  lineStart,
Int2  lineEnd,
Int2  point 
)
static

Factor of the nearest point on the segment.

Returned value is in the range [0,1] if the point lies on the segment otherwise it just lies on the line. The closest point can be calculated using (end-start)*factor + start;

static Vector3 ClosestPointOnSegment ( Vector3  lineStart,
Vector3  lineEnd,
Vector3  point 
)
static

Returns the closest point on the segment.

The segment is NOT treated as infinite.

See Also
ClosestPointOnLine
ClosestPointOnSegmentXZ
static Vector3 ClosestPointOnSegmentXZ ( Vector3  lineStart,
Vector3  lineEnd,
Vector3  point 
)
static

Returns the closest point on the segment in the XZ plane.

The y coordinate of the result will be the same as the y coordinate of the point parameter.

The segment is NOT treated as infinite.

See Also
ClosestPointOnSegment
ClosestPointOnLine
static bool IsClockwiseMarginXZ ( Vector3  a,
Vector3  b,
Vector3  c 
)
static

Returns if the points a in a clockwise order.

Will return true even if the points are colinear or very slightly counter-clockwise (if the signed area of the triangle formed by the points has an area less than or equals to float.Epsilon)

static bool IsClockwiseOrColinear ( Int2  a,
Int2  b,
Int2  c 
)
static

Returns true if the points a in a clockwise order or if they are colinear.

static bool IsClockwiseOrColinearXZ ( Int3  a,
Int3  b,
Int3  c 
)
static

Returns true if the points a in a clockwise order or if they are colinear.

static bool IsClockwiseXZ ( Vector3  a,
Vector3  b,
Vector3  c 
)
static

Returns if the points a in a clockwise order.

static bool IsClockwiseXZ ( Int3  a,
Int3  b,
Int3  c 
)
static

Returns if the points a in a clockwise order.

static bool IsColinear ( Vector2  a,
Vector2  b,
Vector2  c 
)
static

Returns if the points are colinear (lie on a straight line)

static bool IsColinearAlmostXZ ( Int3  a,
Int3  b,
Int3  c 
)
static

Returns if the points are colinear (lie on a straight line)

static bool IsColinearXZ ( Int3  a,
Int3  b,
Int3  c 
)
static

Returns if the points are colinear (lie on a straight line)

static bool IsColinearXZ ( Vector3  a,
Vector3  b,
Vector3  c 
)
static

Returns if the points are colinear (lie on a straight line)

static float LineCircleIntersectionFactor ( Vector3  circleCenter,
Vector3  linePoint1,
Vector3  linePoint2,
float  radius 
)
static

Intersection of a line and a circle.

Returns the greatest t such that segmentStart+t*(segmentEnd-segmentStart) lies on the circle.

In case the line does not intersect with the circle, the closest point on the line to the circle will be returned.

Note
Works for line and sphere in 3D space as well.
See Also
http://mathworld.wolfram.com/Circle-LineIntersection.html
https://en.wikipedia.org/wiki/Intersection_(Euclidean_geometry)#A_line_and_a_circle
static Vector3 LineDirIntersectionPointXZ ( Vector3  start1,
Vector3  dir1,
Vector3  start2,
Vector3  dir2 
)
static

Intersection point between two infinite lines.

Note that start points and directions are taken as parameters instead of start and end points. Lines are treated as infinite. If the lines are parallel 'start1' will be returned. Intersections are calculated on the XZ plane.

See Also
LineIntersectionPointXZ
static Vector3 LineDirIntersectionPointXZ ( Vector3  start1,
Vector3  dir1,
Vector3  start2,
Vector3  dir2,
out bool  intersects 
)
static

Intersection point between two infinite lines.

Note that start points and directions are taken as parameters instead of start and end points. Lines are treated as infinite. If the lines are parallel 'start1' will be returned. Intersections are calculated on the XZ plane.

See Also
LineIntersectionPointXZ
static bool LineIntersectionFactorXZ ( Int3  start1,
Int3  end1,
Int3  start2,
Int3  end2,
out float  factor1,
out float  factor2 
)
static

Returns the intersection factors for line 1 and line 2.

The intersection factors is a distance along the line start - end where the other line intersects it.

intersectionPoint = start1 + factor1 * (end1-start1)
intersectionPoint2 = start2 + factor2 * (end2-start2)

Lines are treated as infinite.
false is returned if the lines are parallel and true if they are not. Only the XZ coordinates are used.

static bool LineIntersectionFactorXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2,
out float  factor1,
out float  factor2 
)
static

Returns the intersection factors for line 1 and line 2.

The intersection factors is a distance along the line start - end where the other line intersects it.

intersectionPoint = start1 + factor1 * (end1-start1)
intersectionPoint2 = start2 + factor2 * (end2-start2)

Lines are treated as infinite.
false is returned if the lines are parallel and true if they are not. Only the XZ coordinates are used.

static float LineIntersectionFactorXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2 
)
static

Returns the intersection factor for line 1 with line 2.

The intersection factor is a distance along the line start1 - end1 where the line start2 - end2 intersects it.

intersectionPoint = start1 + intersectionFactor * (end1-start1)

. Lines are treated as infinite.
-1 is returned if the lines are parallel (note that this is a valid return value if they are not parallel too)

static Vector2 LineIntersectionPoint ( Vector2  start1,
Vector2  end1,
Vector2  start2,
Vector2  end2 
)
static

Returns the intersection point between the two lines.

Lines are treated as infinite. start1 is returned if the lines are parallel

static Vector2 LineIntersectionPoint ( Vector2  start1,
Vector2  end1,
Vector2  start2,
Vector2  end2,
out bool  intersects 
)
static

Returns the intersection point between the two lines.

Lines are treated as infinite. start1 is returned if the lines are parallel

static Vector3 LineIntersectionPointXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2 
)
static

Returns the intersection point between the two lines.

Lines are treated as infinite. start1 is returned if the lines are parallel

static Vector3 LineIntersectionPointXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2,
out bool  intersects 
)
static

Returns the intersection point between the two lines.

Lines are treated as infinite. start1 is returned if the lines are parallel

static float LineRayIntersectionFactorXZ ( Int3  start1,
Int3  end1,
Int3  start2,
Int3  end2 
)
static

Returns the intersection factor for line 1 with ray 2.

The intersection factors is a factor distance along the line start - end where the other line intersects it.

intersectionPoint = start1 + factor * (end1-start1)

Lines are treated as infinite.

The second "line" is treated as a ray, meaning only matches on start2 or forwards towards end2 (and beyond) will be returned If the point lies on the wrong side of the ray start, Nan will be returned.

NaN is returned if the lines are parallel.

static float MagnitudeXZ ( Vector3  v)
static
static Vector3 Normalize ( Vector3  v,
out float  magnitude 
)
static

Normalize vector and also return the magnitude.

This is more efficient than calculating the magnitude and normalizing separately

static Vector2 Normalize ( Vector2  v,
out float  magnitude 
)
static

Normalize vector and also return the magnitude.

This is more efficient than calculating the magnitude and normalizing separately

static bool RaySegmentIntersectXZ ( Int3  start1,
Int3  end1,
Int3  start2,
Int3  end2 
)
static

Returns if the ray (start1, end1) intersects the segment (start2, end2).

false is returned if the lines are parallel. Only the XZ coordinates are used.

Todo:
Double check that this actually works
static bool ReversesFaceOrientations ( Matrix4x4  matrix)
static

True if the matrix will reverse orientations of faces.

Scaling by a negative value along an odd number of axes will reverse the orientation of e.g faces on a mesh. This must be counter adjusted by for example the recast rasterization system to be able to handle meshes with negative scales properly.

We can find out if they are flipped by finding out how the signed volume of a unit cube is transformed when applying the matrix

If the (signed) volume turns out to be negative that also means that the orientation of it has been reversed.

See Also
https://en.wikipedia.org/wiki/Normal_(geometry)
https://en.wikipedia.org/wiki/Parallelepiped
static bool ReversesFaceOrientationsXZ ( Matrix4x4  matrix)
static

True if the matrix will reverse orientations of faces in the XZ plane.

Almost the same as ReversesFaceOrientations, but this method assumes that scaling a face with a negative scale along the Y axis does not reverse the orientation of the face.

This is used for navmesh cuts.

Scaling by a negative value along one axis or rotating it so that it is upside down will reverse the orientation of the cut, so we need to be reverse it again as a countermeasure. However if it is flipped along two axes it does not need to be reversed. We can handle all these cases by finding out how a unit square formed by our forward axis and our rightward axis is transformed in XZ space when applying the local to world matrix. If the (signed) area of the unit square turns out to be negative that also means that the orientation of it has been reversed. The signed area is calculated using a cross product of the vectors.

static bool RightOrColinear ( Vector2  a,
Vector2  b,
Vector2  p 
)
static

Returns if p lies on the right side of the line a - b.

Also returns true if the points are colinear.

static bool RightOrColinear ( Int2  a,
Int2  b,
Int2  p 
)
static

Returns if p lies on the right side of the line a - b.

Also returns true if the points are colinear.

static bool RightOrColinearXZ ( Vector3  a,
Vector3  b,
Vector3  p 
)
static

Returns if p lies on the left side of the line a - b.

Uses XZ space. Also returns true if the points are colinear.

static bool RightOrColinearXZ ( Int3  a,
Int3  b,
Int3  p 
)
static

Returns if p lies on the left side of the line a - b.

Uses XZ space. Also returns true if the points are colinear.

static bool RightXZ ( Vector3  a,
Vector3  b,
Vector3  p 
)
static

Returns if p lies on the right side of the line a - b.

Uses XZ space. Does not return true if the points are colinear.

static bool RightXZ ( Int3  a,
Int3  b,
Int3  p 
)
static

Returns if p lies on the right side of the line a - b.

Uses XZ space. Does not return true if the points are colinear.

static Vector3 SegmentIntersectionPointXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2,
out bool  intersects 
)
static

Returns the intersection point between the two line segments in XZ space.

Lines are NOT treated as infinite. start1 is returned if the line segments do not intersect The point will be returned along the line [start1, end1] (this matters only for the y coordinate).

static bool SegmentIntersectsBounds ( Bounds  bounds,
Vector3  a,
Vector3  b 
)
static

Does the line segment intersect the bounding box.

The line is NOT treated as infinite.

Author
Slightly modified code from http://www.3dkingdoms.com/weekly/weekly.php?a=21
static bool SegmentsIntersect ( Int2  start1,
Int2  end1,
Int2  start2,
Int2  end2 
)
static

Returns if the line segment start2 - end2 intersects the line segment start1 - end1.

If only the endpoints coincide, the result is undefined (may be true or false).

static bool SegmentsIntersectXZ ( Int3  start1,
Int3  end1,
Int3  start2,
Int3  end2 
)
static

Returns if the line segment start2 - end2 intersects the line segment start1 - end1.

If only the endpoints coincide, the result is undefined (may be true or false).

Note
XZ space
static bool SegmentsIntersectXZ ( Vector3  start1,
Vector3  end1,
Vector3  start2,
Vector3  end2 
)
static

Returns if the two line segments intersects.

The lines are NOT treated as infinite (just for clarification)

See Also
IntersectionPoint
static long SignedTriangleAreaTimes2XZ ( Int3  a,
Int3  b,
Int3  c 
)
static

Signed area of a triangle in the XZ plane multiplied by 2.

This will be negative for clockwise triangles and positive for counter-clockwise ones

static float SignedTriangleAreaTimes2XZ ( Vector3  a,
Vector3  b,
Vector3  c 
)
static

Signed area of a triangle in the XZ plane multiplied by 2.

This will be negative for clockwise triangles and positive for counter-clockwise ones.

static float SqrDistancePointSegment ( Vector3  a,
Vector3  b,
Vector3  p 
)
static

Returns the squared distance between p and the segment a-b.

The line is not considered infinite.

static float SqrDistancePointSegmentApproximate ( int  x,
int  z,
int  px,
int  pz,
int  qx,
int  qz 
)
static

Returns the approximate shortest squared distance between x,z and the segment p-q.

The segment is not considered infinite. This function is not entirely exact, but it is about twice as fast as DistancePointSegment2.

Todo:
Is this actually approximate? It looks exact.
static float SqrDistancePointSegmentApproximate ( Int3  a,
Int3  b,
Int3  p 
)
static

Returns the approximate shortest squared distance between x,z and the segment p-q.

The segment is not considered infinite. This function is not entirely exact, but it is about twice as fast as DistancePointSegment2.

Todo:
Is this actually approximate? It looks exact.
static float SqrDistanceSegmentSegment ( Vector3  s1,
Vector3  e1,
Vector3  s2,
Vector3  e2 
)
static

3D minimum distance between 2 segments.

Input: two 3D line segments S1 and S2

Returns
the shortest squared distance between S1 and S2
static float SqrDistanceXZ ( Vector3  a,
Vector3  b 
)
static

Squared distance between two points in the XZ plane.


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