15 public static class Constants {
16 public const float PI = 3.1415926536f;
17 public const float DEG_TO_RAD = 0.0174532925f;
18 public const float RAD_TO_DEG = 57.295779513f;
19 public const float EPSILON = 1.192092896e-07f;
36 public struct Vector : IEquatable<Vector> {
39 return new Vector(v1.
x + v2.
x, v1.
y + v2.
y, v1.
z + v2.
z);
43 return new Vector(v1.
x - v2.
x, v1.
y - v2.
y, v1.
z - v2.
z);
47 return new Vector(v1.
x * scalar, v1.
y * scalar, v1.
z * scalar);
51 return new Vector(v1.
x * scalar, v1.
y * scalar, v1.
z * scalar);
55 return new Vector(v1.
x / scalar, v1.
y / scalar, v1.
z / scalar);
71 return new float[] {
x,
y,
z };
103 return (
float)
Math.Sqrt((
x - other.
x) * (
x - other.
x) +
104 (
y - other.
y) * (
y - other.
y) +
105 (
z - other.
z) * (
z - other.
z));
122 if (denom <= Constants.EPSILON) {
125 float val =
Dot(other) / (float)
Math.Sqrt(denom);
128 }
else if (val <= -1.0f) {
131 return (
float)
Math.Acos(val);
142 return (
x * other.
x) + (
y * other.
y) + (
z * other.
z);
156 return new Vector((
y * other.
z) - (
z * other.
y),
157 (
z * other.
x) - (
x * other.
z),
158 (
x * other.
y) - (
y * other.
x));
166 return "(" +
x +
", " +
y +
", " +
z +
")";
174 return x.NearlyEquals(v.
x) &&
y.NearlyEquals(v.
y) &&
z.NearlyEquals(v.
z);
187 return !(
float.IsNaN(
x) ||
float.IsInfinity(
x) ||
188 float.IsNaN(
y) ||
float.IsInfinity(
y) ||
189 float.IsNaN(
z) ||
float.IsInfinity(
z));
197 public float this[uint index] {
205 throw new IndexOutOfRangeException();
214 throw new IndexOutOfRangeException();
230 get {
return (
float)
Math.Sqrt(
x *
x +
y *
y +
z *
z); }
238 get {
return x *
x +
y *
y +
z *
z; }
253 get {
return (
float)
Math.Atan2(
y, -
z); }
273 get {
return (
float)
Math.Atan2(
x, -
y); }
288 get {
return (
float)
Math.Atan2(
x, -
z); }
302 if (denom <= Constants.EPSILON) {
305 denom = 1.0f / (float)
Math.Sqrt(denom);
306 return new Vector(
x * denom,
y * denom,
z * denom);
368 a.
x + t * (b.
x - a.
x),
369 a.
y + t * (b.
y - a.
y),
370 a.
z + t * (b.
z - a.
z)
378 hash = hash * 23 +
x.GetHashCode();
379 hash = hash * 23 +
y.GetHashCode();
380 hash = hash * 23 +
z.GetHashCode();
The Vector struct represents a three-component mathematical vector or point such as a direction or po...
static readonly Vector Zero
The zero vector: (0, 0, 0)
override bool Equals(Object obj)
override string ToString()
Returns a string containing this vector in a human readable format: (x, y, z).
static readonly Vector Ones
The ones vector: (1, 1, 1)
Vector(Vector vector)
Copies the specified Vector.
Vector Normalized
A normalized copy of this vector.
static readonly Vector ZAxis
The z-axis unit vector: (0, 0, 1)
Vector(float x, float y, float z)
Creates a new Vector with the specified component values.
static readonly Vector Forward
The unit vector pointing forward along the negative z-axis: (0, 0, -1)
static Vector Lerp(Vector a, Vector b, float t)
float AngleTo(Vector other)
The angle between this vector and the specified vector in radians.
static readonly Vector Down
The unit vector pointing down along the negative y-axis: (0, -1, 0)
static readonly Vector Left
The unit vector pointing left along the negative x-axis: (-1, 0, 0)
static Vector operator-(Vector v1, Vector v2)
static Vector operator/(Vector v1, float scalar)
float Dot(Vector other)
The dot product of this vector with another vector.
float DistanceTo(Vector other)
The distance between the point represented by this Vector object and a point represented by the speci...
static readonly Vector Right
The unit vector pointing right along the positive x-axis: (1, 0, 0)
override int GetHashCode()
bool IsValid()
Returns true if all of the vector's components are finite. If any component is NaN or infinite,...
bool Equals(Vector v)
Compare Vector equality component-wise.
static bool operator!=(Vector v1, Vector v2)
static readonly Vector YAxis
The y-axis unit vector: (0, 1, 0)
float Yaw
The yaw angle in radians.
float Pitch
The pitch angle in radians.
Vector Cross(Vector other)
The cross product of this vector and the specified vector.
static readonly Vector XAxis
The x-axis unit vector: (1, 0, 0)
static readonly Vector Backward
The unit vector pointing backward along the positive z-axis: (0, 0, 1)
static readonly Vector Up
The unit vector pointing up along the positive y-axis: (0, 1, 0)
static bool operator==(Vector v1, Vector v2)
static Vector operator*(Vector v1, float scalar)
float MagnitudeSquared
The square of the magnitude, or length, of this vector.
static Vector operator+(Vector v1, Vector v2)
float Roll
The roll angle in radians.
float Magnitude
The magnitude, or length, of this vector.