Beispiel #1
0
        /// <summary>
        /// Returns the refraction of a vector off a surface that has the specified normal, and refractive index.
        /// </summary>
        /// <param name="vector">The source vector.</param>
        /// <param name="normal">Normal of the surface.</param>
        /// <param name="index">The refractive index, destination index over source index.</param>
        /// <returns>The refracted vector.</returns>
        /// <remarks>Refract only gives the direction of a refraction off a surface, it does not determine
        /// whether the original vector was close enough to the surface to hit it.</remarks>
        public static Vector2f Refract(Vector2h vector, Vector2h normal, float index)
        {
            var cos1     = Dot(vector, normal);
            var radicand = 1 - (index * index) * (1 - (cos1 * cos1));

            if (radicand < 0)
            {
                return(Vector2f.Zero);
            }
            return((index * vector) + ((Functions.Sqrt(radicand) - index * cos1) * normal));
        }
Beispiel #2
0
        /// <summary>
        /// Transforms a vector in cartesian coordinates to polar coordinates.
        /// </summary>
        /// <param name="value">The vector to transform.</param>
        /// <returns>The polar coordinates of value.</returns>
        public static PolarCoordinate CartesianToPolar(Vector2h value)
        {
            double theta = Functions.Atan2(value.Y, value.X);

            if (theta < 0)
            {
                theta += 2 * Constants.Pi;
            }
            return(new PolarCoordinate(
                       theta,
                       (double)Functions.Sqrt(value.X * value.X + value.Y * value.Y)));
        }
Beispiel #3
0
        /// <summary>
        /// Computes the normalized value (or unit) of a vector.
        /// </summary>
        /// <param name="value">A vector.</param>
        /// <returns>The normalized value of value.</returns>
        public static Vector2f Normalize(Vector2h value)
        {
            var absolute = Absolute(value);

            if (absolute <= float.Epsilon)
            {
                return(Vector2h.Zero);
            }
            else
            {
                return((Vector2f)value / absolute);
            }
        }
Beispiel #4
0
 /// <summary>
 /// Returns a vector where each component is the fractional part of the specified component.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The fractional of value.</returns>
 public static Vector2f Fractional(Vector2h value)
 {
     return(new Vector2f(Functions.Fractional(value.X), Functions.Fractional(value.Y)));
 }
Beispiel #5
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2i Map(Vector2h value, Func <Half, int> mapping)
 {
     return(new Vector2i(mapping(value.X), mapping(value.Y)));
 }
Beispiel #6
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2f Map(Vector2h value, Func <Half, float> mapping)
 {
     return(new Vector2f(mapping(value.X), mapping(value.Y)));
 }
Beispiel #7
0
 /// <summary>
 /// Computes the absolute squared value of a vector and returns the result.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The absolute squared value of value.</returns>
 public static float AbsoluteSquared(Vector2h value)
 {
     return(Dot(value, value));
 }
Beispiel #8
0
 /// <summary>
 /// Determines whether any component of a vector is non-zero.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>true if any components are non-zero; false otherwise.</returns>
 public static bool Any(Vector2h value)
 {
     return(value.X != 0 || value.Y != 0);
 }
Beispiel #9
0
 /// <summary>
 /// Calculates the dot product (inner product) of two vectors.
 /// </summary>
 /// <param name="left">First source vector.</param>
 /// <param name="right">Second source vector.</param>
 /// <returns>The dot product of the two vectors.</returns>
 public static float Dot(Vector2h left, Vector2h right)
 {
     return(left.X * right.X + left.Y * right.Y);
 }
Beispiel #10
0
 /// <summary>
 /// Returns the reflection of a vector off a surface that has the specified normal.
 /// </summary>
 /// <param name="vector">The source vector.</param>
 /// <param name="normal">Normal of the surface.</param>
 /// <returns>The reflected vector.</returns>
 /// <remarks>Reflect only gives the direction of a reflection off a surface, it does not determine
 /// whether the original vector was close enough to the surface to hit it.</remarks>
 public static Vector2f Reflect(Vector2h vector, Vector2h normal)
 {
     return(vector - ((2 * Dot(vector, normal)) * normal));
 }
Beispiel #11
0
 /// <summary>
 /// Returns the Cartesian coordinate for one axis of a point that is defined
 /// by a given triangle and two normalized barycentric (areal) coordinates.
 /// </summary>
 /// <param name="value1">The coordinate of vertex 1 of the defining triangle.</param>
 /// <param name="value2">The coordinate of vertex 2 of the defining triangle.</param>
 /// <param name="value3">The coordinate of vertex 3 of the defining triangle.</param>
 /// <param name="amount1">The normalized barycentric (areal) coordinate b2, equal to the weighting
 /// factor for vertex 2, the coordinate of which is specified in value2.</param>
 /// <param name="amount2">The normalized barycentric (areal) coordinate b3, equal to the weighting
 /// factor for vertex 3, the coordinate of which is specified in value3.</param>
 /// <returns>Cartesian coordinate of the specified point.</returns>
 public static Vector2f Barycentric(Vector2h value1, Vector2h value2, Vector2h value3, float amount1, float amount2)
 {
     return(((1 - amount1 - amount2) * value1) + (amount1 * value2) + (amount2 * value3));
 }
Beispiel #12
0
 /// <summary>
 /// Calculates the reciprocal of each component in the vector.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>A vector with the reciprocal of each of values components.</returns>
 public static Vector2f Reciprocal(Vector2h value)
 {
     return(new Vector2f(1 / value.X, 1 / value.Y));
 }
Beispiel #13
0
 /// <summary>
 /// Returns a vector where each component is rounded to the nearest integral value.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <param name="digits">The number of fractional digits in the return value.</param>
 /// <param name="mode">Specification for how to round value if it is midway between two other numbers.</param>
 /// <returns>The result of rounding value.</returns>
 public static Vector2f Round(Vector2h value, int digits, MidpointRounding mode)
 {
     return(new Vector2f(Functions.Round(value.X, digits, mode), Functions.Round(value.Y, digits, mode)));
 }
Beispiel #14
0
 /// <summary>
 /// Returns a vector where each component is rounded to the nearest integral value.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <param name="digits">The number of fractional digits in the return value.</param>
 /// <returns>The result of rounding value.</returns>
 public static Vector2f Round(Vector2h value, int digits)
 {
     return(new Vector2f(Functions.Round(value.X, digits), Functions.Round(value.Y, digits)));
 }
Beispiel #15
0
 /// <summary>
 /// Returns a vector where each component is rounded to the nearest integral value.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The result of rounding value.</returns>
 public static Vector2f Round(Vector2h value)
 {
     return(new Vector2f(Functions.Round(value.X), Functions.Round(value.Y)));
 }
Beispiel #16
0
 /// <summary>
 /// Divides a vector by a scalar and returns the result.
 /// </summary>
 /// <param name="vector">The vector to be divided (the dividend).</param>
 /// <param name="scalar">The scalar to divide by (the divisor).</param>
 /// <returns>The result of dividing left by right (the quotient).</returns>
 public static Vector2f Divide(Vector2h vector, float scalar)
 {
     return(new Vector2f(vector.X / scalar, vector.Y / scalar));
 }
Beispiel #17
0
 /// <summary>
 /// Returns a value that indicates whether two vectors are equal.
 /// </summary>
 /// <param name="left">The first vector to compare.</param>
 /// <param name="right">The second vector to compare.</param>
 /// <returns>true if the left and right are equal; otherwise, false.</returns>
 public static bool Equals(Vector2h left, Vector2h right)
 {
     return(left == right);
 }
Beispiel #18
0
 /// <summary>
 /// Performs a linear interpolation between two values.
 /// </summary>
 /// <param name="value1">First value.</param>
 /// <param name="value2">Second value.</param>
 /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="value2"/>.</param>
 /// <returns>The linear interpolation of the two values.</returns>
 public static Vector2f Lerp(Vector2h value1, Vector2h value2, float amount)
 {
     return((1 - amount) * value1 + amount * value2);
 }
Beispiel #19
0
 /// <summary>
 /// Determines whether all components of a vector are non-zero.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>true if all components are non-zero; false otherwise.</returns>
 public static bool All(Vector2h value)
 {
     return(value.X != 0 && value.Y != 0);
 }
Beispiel #20
0
 /// <summary>
 /// Writes the given <see cref="Vector2h"/> to an <see cref="Ibasa.IO.BinaryWriter">.
 /// </summary>
 public static void Write(this Ibasa.IO.BinaryWriter writer, Vector2h vector)
 {
     writer.Write(vector.X);
     writer.Write(vector.Y);
 }
Beispiel #21
0
 /// <summary>
 /// Determines whether any components of a vector satisfy a condition.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <param name="predicate">A function to test each component for a condition.</param>
 /// <returns>true if any component of the vector passes the test in the specified
 /// predicate; otherwise, false.</returns>
 public static bool Any(Vector2h value, Predicate <Half> predicate)
 {
     return(predicate(value.X) || predicate(value.Y));
 }
Beispiel #22
0
 /// <summary>
 /// Returns the additive inverse of a vector.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The negative of value.</returns>
 public static Vector2f Negative(Vector2h value)
 {
     return(new Vector2f(-value.X, -value.Y));
 }
Beispiel #23
0
 /// <summary>
 /// Computes the absolute value (or modulus or magnitude) of a vector and returns the result.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The absolute value of value.</returns>
 public static float Absolute(Vector2h value)
 {
     return(Functions.Sqrt(AbsoluteSquared(value)));
 }
Beispiel #24
0
 /// <summary>
 /// Adds two vectors and returns the result.
 /// </summary>
 /// <param name="left">The first value to add.</param>
 /// <param name="right">The second value to add.</param>
 /// <returns>The sum of left and right.</returns>
 public static Vector2f Add(Vector2h left, Vector2h right)
 {
     return(new Vector2f(left.X + right.X, left.Y + right.Y));
 }
Beispiel #25
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2d Map(Vector2h value, Func <Half, double> mapping)
 {
     return(new Vector2d(mapping(value.X), mapping(value.Y)));
 }
Beispiel #26
0
 /// <summary>
 /// Subtracts one vectors from another and returns the result.
 /// </summary>
 /// <param name="left">The value to subtract from (the minuend).</param>
 /// <param name="right">The value to subtract (the subtrahend).</param>
 /// <returns>The result of subtracting right from left (the difference).</returns>
 public static Vector2f Subtract(Vector2h left, Vector2h right)
 {
     return(new Vector2f(left.X - right.X, left.Y - right.Y));
 }
Beispiel #27
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2l Map(Vector2h value, Func <Half, long> mapping)
 {
     return(new Vector2l(mapping(value.X), mapping(value.Y)));
 }
Beispiel #28
0
 /// <summary>
 /// Returns the product of a vector and scalar.
 /// </summary>
 /// <param name="vector">The vector to multiply.</param>
 /// <param name="scalar">The scalar to multiply.</param>
 /// <returns>The product of the left and right parameters.</returns>
 public static Vector2f Multiply(Vector2h vector, float scalar)
 {
     return(new Vector2f(vector.X * scalar, vector.Y * scalar));
 }
Beispiel #29
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2s Map(Vector2h value, Func <Half, short> mapping)
 {
     return(new Vector2s(mapping(value.X), mapping(value.Y)));
 }
Beispiel #30
0
 /// <summary>
 /// Returns a vector where each component is the integral part of the specified component.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The integral of value.</returns>
 public static Vector2f Truncate(Vector2h value)
 {
     return(new Vector2f(Functions.Truncate(value.X), Functions.Truncate(value.Y)));
 }