Struct Vector3
A 3D vector.
Inherited Members
Namespace: Elements.Geometry
Assembly: Hypar.Elements.dll
Syntax
public struct Vector3 : IComparable<Vector3>, IEquatable<Vector3>
Constructors
Vector3(Vector3)
Create a Vector3 by copying the components of another Vector3.
Declaration
public Vector3(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The Vector3 to copy. |
Vector3(Double, Double)
Create a vector from x, and y coordinates.
Declaration
public Vector3(double x, double y)
Parameters
Type | Name | Description |
---|---|---|
System.Double | x | The x coordinate of the vector. |
System.Double | y | Thy y coordinate of the vector. |
Vector3(Double, Double, Double)
Create a vector.
Declaration
[JsonConstructor]
public Vector3(double x, double y, double z)
Parameters
Type | Name | Description |
---|---|---|
System.Double | x | The x component. |
System.Double | y | The y component. |
System.Double | z | The z component. |
Fields
COS_ANGLE_EPSILON
A tolerance for angle comparison operation of cos of 0.001 degrees.
Declaration
public const double COS_ANGLE_EPSILON = 0.99999999984769128
Field Value
Type | Description |
---|---|
System.Double |
EPSILON
A tolerance for comparison operations of 1e-5.
Declaration
public const double EPSILON = 1E-05
Field Value
Type | Description |
---|---|
System.Double |
Properties
Max
The largest possible value of a Vector3.
Declaration
public static readonly Vector3 Max { get; }
Property Value
Type | Description |
---|---|
Vector3 |
Min
The smallest possible value of a Vector3.
Declaration
public static readonly Vector3 Min { get; }
Property Value
Type | Description |
---|---|
Vector3 |
Origin
Create a vector at the origin.
Declaration
public static readonly Vector3 Origin { get; }
Property Value
Type | Description |
---|---|
Vector3 |
X
The X component of the vector.
Declaration
[JsonProperty("X", Required = Required.Always)]
public double X { get; set; }
Property Value
Type | Description |
---|---|
System.Double |
XAxis
Create a vector along the X axis.
Declaration
public static readonly Vector3 XAxis { get; }
Property Value
Type | Description |
---|---|
Vector3 |
Y
The Y component of the vector.
Declaration
[JsonProperty("Y", Required = Required.Always)]
public double Y { get; set; }
Property Value
Type | Description |
---|---|
System.Double |
YAxis
Create a vector along the Y axis.
Declaration
public static readonly Vector3 YAxis { get; }
Property Value
Type | Description |
---|---|
Vector3 |
Z
The Z component of the vector.
Declaration
[JsonProperty("Z", Required = Required.Always)]
public double Z { get; set; }
Property Value
Type | Description |
---|---|
System.Double |
ZAxis
Create a vector along the Z axis.
Declaration
public static readonly Vector3 ZAxis { get; }
Property Value
Type | Description |
---|---|
Vector3 |
Methods
AngleTo(Vector3)
The angle in degrees from this vector to the provided vector. Note that for angles in the plane that can be greater than 180 degrees, you should use Vector3.PlaneAngleTo.
Declaration
public double AngleTo(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to measure the angle. |
Returns
Type | Description |
---|---|
System.Double | The angle in degrees between 0 and 180. |
AreCollinear(Vector3, Vector3, Vector3)
Check whether three points are on the same line.
Declaration
[Obsolete("Use AreCollinearByDistance or AreCollinearByAngle instead")]
public static bool AreCollinear(Vector3 a, Vector3 b, Vector3 c)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first point. |
Vector3 | b | The second point. |
Vector3 | c | The third point. |
Returns
Type | Description |
---|---|
System.Boolean | True if the points are on the same line, false otherwise. |
AreCollinearByAngle(Vector3, Vector3, Vector3, Double)
Check whether three points are on the same line within certain angle. Order is important since unsigned abc angle is checked. This function is much faster than AreCollinearByDistance but angle deviation scales with the distance of points being compared. If points are far away from each other they might appear collinear even if there are large distance offsets between them.
Declaration
public static bool AreCollinearByAngle(Vector3 a, Vector3 b, Vector3 c, double cosAngleTolerance = 0.99999999984769128)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first point. |
Vector3 | b | The second point. |
Vector3 | c | The third point. |
System.Double | cosAngleTolerance | Angle tolerance as cos. |
Returns
Type | Description |
---|---|
System.Boolean |
AreCollinearByDistance(Vector3, Vector3, Vector3, Double)
Check whether three points are on the same line within certain distance. This function is slower than AreCollinearByAngle and less suitable for high complexity code.
Declaration
public static bool AreCollinearByDistance(Vector3 a, Vector3 b, Vector3 c, double tolerance = 1E-05)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first point. |
Vector3 | b | The second point. |
Vector3 | c | The third point. |
System.Double | tolerance | Distance tolerance. |
Returns
Type | Description |
---|---|
System.Boolean | True if the points are on the same line, false otherwise. |
AreCoplanar(Vector3, Vector3, Vector3, Vector3)
Are four provided points on the same plane?
Declaration
public static bool AreCoplanar(Vector3 a, Vector3 b, Vector3 c, Vector3 d)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | |
Vector3 | b | |
Vector3 | c | |
Vector3 | d |
Returns
Type | Description |
---|---|
System.Boolean |
AtNEqualSpacesAlongLine(Line, Int32, Boolean)
Create vectors at n equal spaces along the provided line.
Declaration
public static IList<Vector3> AtNEqualSpacesAlongLine(Line line, int n, bool includeEnds = false)
Parameters
Type | Name | Description |
---|---|---|
Line | line | The line. |
System.Int32 | n | The number of samples along the line. |
System.Boolean | includeEnds | A flag indicating whether or not to include points for the start and end of the line. |
Returns
Type | Description |
---|---|
System.Collections.Generic.IList<Vector3> |
Average(Vector3)
Compute the average of this Vector3 and v.
Declaration
public Vector3 Average(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to compute the average. |
Returns
Type | Description |
---|---|
Vector3 | A vector which is the average of this and v. |
CCW(Vector3, Vector3, Vector3)
Check whether three points are wound CCW in two dimensions.
Declaration
public static double CCW(Vector3 a, Vector3 b, Vector3 c)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first point. |
Vector3 | b | The second point. |
Vector3 | c | The third point. |
Returns
Type | Description |
---|---|
System.Double | Greater than 0 if the points are CCW, less than 0 if they are CW, and 0 if they are collinear. |
ClosestPointOn(Line, Boolean)
Get the closest point on the line from this point.
Declaration
public Vector3 ClosestPointOn(Line line, bool infinite = false)
Parameters
Type | Name | Description |
---|---|---|
Line | line | The line on which to find the closest point. |
System.Boolean | infinite | If true, line will be treated as infinite. (False by default) |
Returns
Type | Description |
---|---|
Vector3 | The closest point on the line from this point. |
CompareTo(Vector3)
Implement IComparable interface.
Declaration
public int CompareTo(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v |
Returns
Type | Description |
---|---|
System.Int32 |
ComputeDefaultBasisVectors()
Compute basis vectors for this vector. By default, the cross product of the world Z axis and this vector are used to compute the U direction. If this vector is parallel the world Z axis, then the world Y axis is used instead.
Declaration
public (Vector3 U, Vector3 V) ComputeDefaultBasisVectors()
Returns
Type | Description |
---|---|
System.ValueTuple<Vector3, Vector3> |
Cross(Vector3)
Compute the cross product of this vector and v.
Declaration
public Vector3 Cross(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to compute the cross product. |
Returns
Type | Description |
---|---|
Vector3 |
Cross(Double, Double, Double)
Compute the cross product of this vector and a vector composed of the provided components.
Declaration
public Vector3 Cross(double x, double y, double z)
Parameters
Type | Name | Description |
---|---|---|
System.Double | x | X |
System.Double | y | Y |
System.Double | z | Z |
Returns
Type | Description |
---|---|
Vector3 |
DistanceTo(Line)
Find the distance from this point to the line.
Declaration
public double DistanceTo(Line line)
Parameters
Type | Name | Description |
---|---|---|
Line | line |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Line, out Vector3)
Find the distance from this point to the line, and output the location of the closest point on that line. Using formula from https://diego.assencio.com/?index=ec3d5dfdfc0b6a0d147a656f0af332bd
Declaration
public double DistanceTo(Line line, out Vector3 closestPoint)
Parameters
Type | Name | Description |
---|---|---|
Line | line | The line to find the distance to. |
Vector3 | closestPoint | The point on the line that is closest to this point. |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Plane)
The distance from this point to the plane. The distance will be negative when this point lies "behind" the plane.
Declaration
public double DistanceTo(Plane p)
Parameters
Type | Name | Description |
---|---|---|
Plane | p | The plane. |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Polygon)
Find the shortest distance from this point to any point within the polygon
Declaration
public double DistanceTo(Polygon polygon)
Parameters
Type | Name | Description |
---|---|---|
Polygon | polygon | The polygon for computing the distance |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Polygon, out Vector3)
Find the shortest distance from this point to any point within the polygon, and output the location of the closest point on that polygon.
Declaration
public double DistanceTo(Polygon polygon, out Vector3 closestPoint)
Parameters
Type | Name | Description |
---|---|---|
Polygon | polygon | The polygon for computing the distance |
Vector3 | closestPoint | Point within the polygon that is closest to this point |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Polyline)
Find the shortest distance from this point to any point on the polyline, and output the location of the closest point on that polyline.
Declaration
public double DistanceTo(Polyline polyline)
Parameters
Type | Name | Description |
---|---|---|
Polyline | polyline | The polyline for computing the distance. |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Polyline, out Vector3)
Find the shortest distance from this point to any point on the polyline, and output the location of the closest point on that polyline.
Declaration
public double DistanceTo(Polyline polyline, out Vector3 closestPoint)
Parameters
Type | Name | Description |
---|---|---|
Polyline | polyline | The polyline for computing the distance. |
Vector3 | closestPoint | The point on the polyline that is closest to this point. |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Ray)
The distance from this point to the ray. The ray is treated as being infinitely long.
Declaration
public double DistanceTo(Ray ray)
Parameters
Type | Name | Description |
---|---|---|
Ray | ray | The target ray. |
Returns
Type | Description |
---|---|
System.Double |
DistanceTo(Vector3)
The distance from this point to v.
Declaration
public double DistanceTo(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The target vector. |
Returns
Type | Description |
---|---|
System.Double | The distance between this vector and the provided vector. |
DistanceTo((Vector3 from, Vector3 to), out Vector3)
Find the distance from this point to the edge, and output the location of the closest point on that line. Using formula from https://diego.assencio.com/?index=ec3d5dfdfc0b6a0d147a656f0af332bd
Declaration
public double DistanceTo((Vector3 from, Vector3 to) edge, out Vector3 closestPoint)
Parameters
Type | Name | Description |
---|---|---|
System.ValueTuple<Vector3, Vector3> | edge | The edge to find the distance to. |
Vector3 | closestPoint | The point on the line that is closest to this point. |
Returns
Type | Description |
---|---|
System.Double |
Dot(Vector3)
Compute the dot product of this vector and v.
Declaration
public double Dot(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to compute the dot product. |
Returns
Type | Description |
---|---|
System.Double | A value between 1 and -1. |
Dot(Double, Double, Double)
Compute the dot product of this vector and a vector composed of the provided components.
Declaration
public double Dot(double x, double y, double z)
Parameters
Type | Name | Description |
---|---|---|
System.Double | x | X |
System.Double | y | Y |
System.Double | z | Z |
Returns
Type | Description |
---|---|
System.Double | A value between 1 and -1. |
Equals(Vector3)
Is this vector equal to the provided vector?
Declaration
public bool Equals(Vector3 other)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | other | The vector to test. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if all components of the two vectors are within Epsilon, otherwise false. |
Equals(Object)
Is this vector equal to the provided vector?
Declaration
public override bool Equals(object other)
Parameters
Type | Name | Description |
---|---|---|
System.Object | other | The vector to test. |
Returns
Type | Description |
---|---|
System.Boolean | Returns true if all components of the two vectors are within Epsilon, otherwise false. |
Overrides
GetHashCode()
Get the hash code for the vector.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
System.Int32 |
Overrides
IsAlmostEqualTo(Vector3, Double)
Determine whether this vector's components are equal to those of v, within tolerance.
Declaration
public bool IsAlmostEqualTo(Vector3 v, double tolerance = 1E-05)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector to compare. |
System.Double | tolerance | Optional custom tolerance value. |
Returns
Type | Description |
---|---|
System.Boolean | True if the difference of this vector and the supplied vector's components are all within Tolerance, otherwise false. |
IsAlmostEqualTo(Double, Double, Double)
Determine whether this vector's components are equal to the provided components, within tolerance.
Declaration
public bool IsAlmostEqualTo(double x, double y, double z = 0)
Parameters
Type | Name | Description |
---|---|---|
System.Double | x | The x component to compare. |
System.Double | y | The y component to compare. |
System.Double | z | The z component to compare. |
Returns
Type | Description |
---|---|
System.Boolean | True if the difference of this vector and the supplied vector's components are all within Tolerance, otherwise false. |
IsNaN()
Are any components of this vector NaN?
Declaration
public bool IsNaN()
Returns
Type | Description |
---|---|
System.Boolean | True if any components are NaN otherwise false. |
IsParallelTo(Vector3, Double)
Determine whether this vector is parallel to v.
Declaration
public bool IsParallelTo(Vector3 v, double tolerance = 1E-05)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector to compare to this vector. |
System.Double | tolerance | The amount of tolerance in the parallel comparison. |
Returns
Type | Description |
---|---|
System.Boolean | True if the vectors are parallel, otherwise false. |
IsUnitized()
Is this vector of unit length?
Declaration
public bool IsUnitized()
Returns
Type | Description |
---|---|
System.Boolean | True if the vector is of unit length, otherwise false. |
IsZero()
Is this vector zero length?
Declaration
public bool IsZero()
Returns
Type | Description |
---|---|
System.Boolean | True if this vector's components are all less than Epsilon. |
Length()
Get the length of this vector.
Declaration
public double Length()
Returns
Type | Description |
---|---|
System.Double |
LengthSquared()
Get the squared length of this vector.
Declaration
public double LengthSquared()
Returns
Type | Description |
---|---|
System.Double |
Negate()
Construct a new vector which is the inverse of this vector.
Declaration
public Vector3 Negate()
Returns
Type | Description |
---|---|
Vector3 | A new vector which is the inverse of this vector. |
PlaneAngleTo(Vector3)
Calculate a counter-clockwise plane angle between this vector and the provided vector in the XY plane.
Declaration
public double PlaneAngleTo(Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to measure the angle. |
Returns
Type | Description |
---|---|
System.Double | Angle in degrees between 0 and 360, or NaN if the projected input vectors are invalid. |
PlaneAngleTo(Vector3, Vector3)
Calculate a counter-clockwise plane angle between this vector and the provided vector, projected to the plane perpendicular to the provided normal.
Declaration
public double PlaneAngleTo(Vector3 v, Vector3 normal)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector with which to measure the angle. |
Vector3 | normal | The normal of the plane in which you wish to calculate the angle. |
Returns
Type | Description |
---|---|
System.Double | Angle in degrees between 0 and 360, or NaN if the projected input vectors are invalid. |
Project(Plane)
Project this vector onto the plane.
Declaration
public Vector3 Project(Plane p)
Parameters
Type | Name | Description |
---|---|---|
Plane | p | The plane on which to project the point. |
Returns
Type | Description |
---|---|
Vector3 |
ProjectAlong(Vector3, Plane)
Project this vector onto the plane along a vector.
Declaration
public Vector3 ProjectAlong(Vector3 v, Plane p)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector along which t project. |
Plane | p | The plane on which to project. |
Returns
Type | Description |
---|---|
Vector3 | A point on the plane. |
ProjectOnto(Vector3)
Project vector a onto this vector.
Declaration
public Vector3 ProjectOnto(Vector3 a)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The vector to project onto this vector. |
Returns
Type | Description |
---|---|
Vector3 | A new vector which is the projection of a onto this vector. |
ToArray()
Convert a vector's components to an array.
Declaration
public double[] ToArray()
Returns
Type | Description |
---|---|
System.Double[] | An array of comprised of the x, y, and z components of this vector. |
ToString()
A string representation of the vector.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
System.String | The string representation of this vector. |
Overrides
TripleProduct(Vector3, Vector3)
Check if two vectors are coplanar.
Declaration
public double TripleProduct(Vector3 b, Vector3 c)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | b | The second vector. |
Vector3 | c | The third vector. |
Returns
Type | Description |
---|---|
System.Double | True is the vectors are coplanar, otherwise false. |
Unitized()
Return a new vector which is the unitized version of this vector.
Declaration
public Vector3 Unitized()
Returns
Type | Description |
---|---|
Vector3 |
Operators
Addition(Vector3, Vector3)
Add two vectors.
Declaration
public static Vector3 operator +(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
Vector3 | A vector which is the sum of a and b. |
Division(Vector3, Double)
Divide a vector by a scalar.
Declaration
public static Vector3 operator /(Vector3 v, double a)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector to divide. |
System.Double | a | The scalar divisor. |
Returns
Type | Description |
---|---|
Vector3 | A vector whose magnitude is multiplied by a. |
Equality(Vector3, Vector3)
Are the two vectors the same within Epsilon?
Declaration
public static bool operator ==(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | |
Vector3 | b |
Returns
Type | Description |
---|---|
System.Boolean |
GreaterThan(Vector3, Vector3)
Compute whether all components of vector a are greater than those of vector b.
Declaration
public static bool operator>(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
System.Boolean | True if all of a's components are greater than those of b, otherwise false. |
GreaterThanOrEqual(Vector3, Vector3)
Compute whether all components of vector a are greater than or equal to those of vector b.
Declaration
public static bool operator >=(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
System.Boolean | True if all of a's components are greater than or equal to all of those of b, otherwise false. |
Implicit((Double X, Double Y, Double Z) to Vector3)
Automatically convert a tuple of three doubles into a Vector3.
Declaration
public static implicit operator Vector3((double X, double Y, double Z) vector)
Parameters
Type | Name | Description |
---|---|---|
System.ValueTuple<System.Double, System.Double, System.Double> | vector | An (X,Y,Z) tuple of doubles. |
Returns
Type | Description |
---|---|
Vector3 |
Implicit((Double X, Double Y) to Vector3)
Automatically convert a tuple of two doubles into a Vector3.
Declaration
public static implicit operator Vector3((double X, double Y) vector)
Parameters
Type | Name | Description |
---|---|---|
System.ValueTuple<System.Double, System.Double> | vector | An (X,Y) tuple of doubles. |
Returns
Type | Description |
---|---|
Vector3 |
Implicit((Int32 X, Int32 Y, Int32 Z) to Vector3)
Automatically convert a tuple of three ints into a Vector3.
Declaration
public static implicit operator Vector3((int X, int Y, int Z) vector)
Parameters
Type | Name | Description |
---|---|---|
System.ValueTuple<System.Int32, System.Int32, System.Int32> | vector | An (X,Y,Z) tuple of ints. |
Returns
Type | Description |
---|---|
Vector3 |
Implicit((Int32 X, Int32 Y) to Vector3)
Automatically convert a tuple of two ints into a Vector3.
Declaration
public static implicit operator Vector3((int X, int Y) vector)
Parameters
Type | Name | Description |
---|---|---|
System.ValueTuple<System.Int32, System.Int32> | vector | An (X,Y) tuple of ints. |
Returns
Type | Description |
---|---|
Vector3 |
Inequality(Vector3, Vector3)
Are the two vectors not the same within Epsilon?
Declaration
public static bool operator !=(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | |
Vector3 | b |
Returns
Type | Description |
---|---|
System.Boolean |
LessThan(Vector3, Vector3)
Compute whether all components of vector a are less than those of vector b.
Declaration
public static bool operator <(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
System.Boolean | True if all of a's components are less than those of b, otherwise false. |
LessThanOrEqual(Vector3, Vector3)
Compute whether all components of vector a are less than or equal to those of vector b.
Declaration
public static bool operator <=(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
System.Boolean | True if all of a's components are less than or equal to all of those of b, otherwise false. |
Multiply(Vector3, Double)
Multiply a vector and a scalar.
Declaration
public static Vector3 operator *(Vector3 v, double a)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | v | The vector to multiply. |
System.Double | a | The scalar value to multiply. |
Returns
Type | Description |
---|---|
Vector3 | A vector whose magnitude is multiplied by a. |
Multiply(Double, Vector3)
Multiply a scalar and a vector.
Declaration
public static Vector3 operator *(double a, Vector3 v)
Parameters
Type | Name | Description |
---|---|---|
System.Double | a | The scalar value to multiply. |
Vector3 | v | The vector to multiply. |
Returns
Type | Description |
---|---|
Vector3 | A vector whose magnitude is multiplied by a. |
Subtraction(Vector3, Vector3)
Subtract two vectors.
Declaration
public static Vector3 operator -(Vector3 a, Vector3 b)
Parameters
Type | Name | Description |
---|---|---|
Vector3 | a | The first vector. |
Vector3 | b | The second vector. |
Returns
Type | Description |
---|---|
Vector3 | A vector which is the difference between a and b. |