Example #1
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains cubic interpolation of the specified vectors.
 /// </summary>
 /// <param name="value1">Source <see cref="Vector2D"/>.</param>
 /// <param name="value2">Source <see cref="Vector2D"/>.</param>
 /// <param name="amount">Weighting value.</param>
 /// <returns>Cubic interpolation of the specified vectors.</returns>
 public static Vector2D SmoothStep(Vector2D value1, Vector2D value2, double amount)
 {
     return(new Vector2D(
                MathHelperD.SmoothStep(value1.X, value2.X, amount),
                MathHelperD.SmoothStep(value1.Y, value2.Y, amount)
                ));
 }
Example #2
0
 /// <summary>
 /// Clamps the specified value within a range.
 /// </summary>
 /// <param name="value1">The value to clamp.</param>
 /// <param name="min">The min value.</param>
 /// <param name="max">The max value.</param>
 /// <returns>The clamped value.</returns>
 public static Vector2D Clamp(Vector2D value1, Vector2D min, Vector2D max)
 {
     return(new Vector2D(
                MathHelperD.Clamp(value1.X, min.X, max.X),
                MathHelperD.Clamp(value1.Y, min.Y, max.Y)
                ));
 }
Example #3
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains cubic interpolation of the specified vectors.
 /// </summary>
 /// <param name="value1">Source <see cref="Vector2D"/>.</param>
 /// <param name="value2">Source <see cref="Vector2D"/>.</param>
 /// <param name="amount">Weighting value.</param>
 /// <param name="result">Cubic interpolation of the specified vectors as an output parameter.</param>
 public static void SmoothStep(
     ref Vector2D value1,
     ref Vector2D value2,
     double amount,
     out Vector2D result
     )
 {
     result.X = MathHelperD.SmoothStep(value1.X, value2.X, amount);
     result.Y = MathHelperD.SmoothStep(value1.Y, value2.Y, amount);
 }
Example #4
0
 /// <summary>
 /// Clamps the specified value within a range.
 /// </summary>
 /// <param name="value1">The value to clamp.</param>
 /// <param name="min">The min value.</param>
 /// <param name="max">The max value.</param>
 /// <param name="result">The clamped value as an output parameter.</param>
 public static void Clamp(
     ref Vector2D value1,
     ref Vector2D min,
     ref Vector2D max,
     out Vector2D result
     )
 {
     result.X = MathHelperD.Clamp(value1.X, min.X, max.X);
     result.Y = MathHelperD.Clamp(value1.Y, min.Y, max.Y);
 }
Example #5
0
        /// <summary>
        /// Interpolates between two values using a cubic equation.
        /// </summary>
        /// <param name="value1">Source value.</param>
        /// <param name="value2">Source value.</param>
        /// <param name="amount">Weighting value.</param>
        /// <returns>Interpolated value.</returns>
        public static double SmoothStep(double value1, double value2, double amount)
        {
            /* It is expected that 0 < amount < 1.
             * If amount < 0, return value1.
             * If amount > 1, return value2.
             */
            double result = MathHelperD.Clamp(amount, 0f, 1f);

            result = MathHelperD.Hermite(value1, 0f, value2, 0f, result);

            return(result);
        }
Example #6
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains hermite spline interpolation.
 /// </summary>
 /// <param name="value1">The first position vector.</param>
 /// <param name="tangent1">The first tangent vector.</param>
 /// <param name="value2">The second position vector.</param>
 /// <param name="tangent2">The second tangent vector.</param>
 /// <param name="amount">Weighting factor.</param>
 /// <param name="result">The hermite spline interpolation vector as an output parameter.</param>
 public static void Hermite(
     ref Vector2D value1,
     ref Vector2D tangent1,
     ref Vector2D value2,
     ref Vector2D tangent2,
     double amount,
     out Vector2D result
     )
 {
     result.X = MathHelperD.Hermite(value1.X, tangent1.X, value2.X, tangent2.X, amount);
     result.Y = MathHelperD.Hermite(value1.Y, tangent1.Y, value2.Y, tangent2.Y, amount);
 }
Example #7
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains CatmullRom interpolation of the specified vectors.
 /// </summary>
 /// <param name="value1">The first vector in interpolation.</param>
 /// <param name="value2">The second vector in interpolation.</param>
 /// <param name="value3">The third vector in interpolation.</param>
 /// <param name="value4">The fourth vector in interpolation.</param>
 /// <param name="amount">Weighting factor.</param>
 /// <param name="result">The result of CatmullRom interpolation as an output parameter.</param>
 public static void CatmullRom(
     ref Vector2D value1,
     ref Vector2D value2,
     ref Vector2D value3,
     ref Vector2D value4,
     double amount,
     out Vector2D result
     )
 {
     result.X = MathHelperD.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount);
     result.Y = MathHelperD.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount);
 }
Example #8
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle.
 /// </summary>
 /// <param name="value1">The first vector of 2d-triangle.</param>
 /// <param name="value2">The second vector of 2d-triangle.</param>
 /// <param name="value3">The third vector of 2d-triangle.</param>
 /// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 2d-triangle.</param>
 /// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 2d-triangle.</param>
 /// <param name="result">The cartesian translation of barycentric coordinates as an output parameter.</param>
 public static void Barycentric(
     ref Vector2D value1,
     ref Vector2D value2,
     ref Vector2D value3,
     double amount1,
     double amount2,
     out Vector2D result
     )
 {
     result.X = MathHelperD.Barycentric(value1.X, value2.X, value3.X, amount1, amount2);
     result.Y = MathHelperD.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2);
 }
Example #9
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains CatmullRom interpolation of the specified vectors.
 /// </summary>
 /// <param name="value1">The first vector in interpolation.</param>
 /// <param name="value2">The second vector in interpolation.</param>
 /// <param name="value3">The third vector in interpolation.</param>
 /// <param name="value4">The fourth vector in interpolation.</param>
 /// <param name="amount">Weighting factor.</param>
 /// <returns>The result of CatmullRom interpolation.</returns>
 public static Vector2D CatmullRom(
     Vector2D value1,
     Vector2D value2,
     Vector2D value3,
     Vector2D value4,
     double amount
     )
 {
     return(new Vector2D(
                MathHelperD.CatmullRom(value1.X, value2.X, value3.X, value4.X, amount),
                MathHelperD.CatmullRom(value1.Y, value2.Y, value3.Y, value4.Y, amount)
                ));
 }
Example #10
0
 /// <summary>
 /// Creates a new <see cref="Vector2D"/> that contains the cartesian coordinates of a vector specified in barycentric coordinates and relative to 2d-triangle.
 /// </summary>
 /// <param name="value1">The first vector of 2d-triangle.</param>
 /// <param name="value2">The second vector of 2d-triangle.</param>
 /// <param name="value3">The third vector of 2d-triangle.</param>
 /// <param name="amount1">Barycentric scalar <c>b2</c> which represents a weighting factor towards second vector of 2d-triangle.</param>
 /// <param name="amount2">Barycentric scalar <c>b3</c> which represents a weighting factor towards third vector of 2d-triangle.</param>
 /// <returns>The cartesian translation of barycentric coordinates.</returns>
 public static Vector2D Barycentric(
     Vector2D value1,
     Vector2D value2,
     Vector2D value3,
     double amount1,
     double amount2
     )
 {
     return(new Vector2D(
                MathHelperD.Barycentric(value1.X, value2.X, value3.X, amount1, amount2),
                MathHelperD.Barycentric(value1.Y, value2.Y, value3.Y, amount1, amount2)
                ));
 }
Example #11
0
        private void UpdateVisibleArea()
        {
            var inverseViewMatrix = MatrixD.Invert(Transform);

            var tl = Vector2D.Transform(Vector2D.Zero, inverseViewMatrix);
            var tr = Vector2D.Transform(new Vector2D(Bounds.X, 0), inverseViewMatrix);
            var bl = Vector2D.Transform(new Vector2D(0, Bounds.Y), inverseViewMatrix);
            var br = Vector2D.Transform(new Vector2D(Bounds.Width, Bounds.Height), inverseViewMatrix);

            var min = new Vector2D(
                MathHelperD.Min(tl.X, MathHelperD.Min(tr.X, MathHelperD.Min(bl.X, br.X))),
                MathHelperD.Min(tl.Y, MathHelperD.Min(tr.Y, MathHelperD.Min(bl.Y, br.Y))));
            var max = new Vector2D(
                MathHelperD.Max(tl.X, MathHelperD.Max(tr.X, MathHelperD.Max(bl.X, br.X))),
                MathHelperD.Max(tl.Y, MathHelperD.Max(tr.Y, MathHelperD.Max(bl.Y, br.Y))));

            VisibleArea = new RectangleD(min.X, min.Y, (max.X - min.X), (max.Y - min.Y));
        }
Example #12
0
        // Adapted from http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
        public double?Intersects(BoundingBoxD box)
        {
            double?tMin = null, tMax = null;

            if (MathHelperD.WithinEpsilon(Direction.X, 0.0f))
            {
                if (Position.X < box.Min.X || Position.X > box.Max.X)
                {
                    return(null);
                }
            }
            else
            {
                tMin = (box.Min.X - Position.X) / Direction.X;
                tMax = (box.Max.X - Position.X) / Direction.X;

                if (tMin > tMax)
                {
                    double?temp = tMin;
                    tMin = tMax;
                    tMax = temp;
                }
            }

            if (MathHelperD.WithinEpsilon(Direction.Y, 0.0f))
            {
                if (Position.Y < box.Min.Y || Position.Y > box.Max.Y)
                {
                    return(null);
                }
            }
            else
            {
                double tMinY = (box.Min.Y - Position.Y) / Direction.Y;
                double tMaxY = (box.Max.Y - Position.Y) / Direction.Y;

                if (tMinY > tMaxY)
                {
                    double temp = tMinY;
                    tMinY = tMaxY;
                    tMaxY = temp;
                }

                if ((tMin.HasValue && tMin > tMaxY) ||
                    (tMax.HasValue && tMinY > tMax))
                {
                    return(null);
                }

                if (!tMin.HasValue || tMinY > tMin)
                {
                    tMin = tMinY;
                }
                if (!tMax.HasValue || tMaxY < tMax)
                {
                    tMax = tMaxY;
                }
            }

            if (MathHelperD.WithinEpsilon(Direction.Z, 0.0f))
            {
                if (Position.Z < box.Min.Z || Position.Z > box.Max.Z)
                {
                    return(null);
                }
            }
            else
            {
                double tMinZ = (box.Min.Z - Position.Z) / Direction.Z;
                double tMaxZ = (box.Max.Z - Position.Z) / Direction.Z;

                if (tMinZ > tMaxZ)
                {
                    double temp = tMinZ;
                    tMinZ = tMaxZ;
                    tMaxZ = temp;
                }

                if ((tMin.HasValue && tMin > tMaxZ) ||
                    (tMax.HasValue && tMinZ > tMax))
                {
                    return(null);
                }

                if (!tMin.HasValue || tMinZ > tMin)
                {
                    tMin = tMinZ;
                }
                if (!tMax.HasValue || tMaxZ < tMax)
                {
                    tMax = tMaxZ;
                }
            }

            /* Having a positive tMin and a negative tMax means the ray is inside the
             * box we expect the intesection distance to be 0 in that case.
             */
            if ((tMin.HasValue && tMin < 0) && tMax > 0)
            {
                return(0);
            }

            /* A negative tMin means that the intersection point is behind the ray's
             * origin. We discard these as not hitting the AABB.
             */
            if (tMin < 0)
            {
                return(null);
            }

            return(tMin);
        }