Exemple #1
0
        public void Invert(ref Matrix3 minv)
        {
            //Credit: https://stackoverflow.com/a/18504573

            var d = Determinant;

            if (FloatMath.CloseTo(d, 0))
            {
                throw new InvalidOperationException("Matrix is singular and cannot be inverted.");
            }

            var m = this;

            // computes the inverse of a matrix m
            double det = m.R0C0 * (m.R1C1 * m.R2C2 - m.R2C1 * m.R1C2) -
                         m.R0C1 * (m.R1C0 * m.R2C2 - m.R1C2 * m.R2C0) +
                         m.R0C2 * (m.R1C0 * m.R2C1 - m.R1C1 * m.R2C0);

            var invdet = 1 / det;

            minv.R0C0 = (float)((m.R1C1 * m.R2C2 - m.R2C1 * m.R1C2) * invdet);
            minv.R0C1 = (float)((m.R0C2 * m.R2C1 - m.R0C1 * m.R2C2) * invdet);
            minv.R0C2 = (float)((m.R0C1 * m.R1C2 - m.R0C2 * m.R1C1) * invdet);
            minv.R1C0 = (float)((m.R1C2 * m.R2C0 - m.R1C0 * m.R2C2) * invdet);
            minv.R1C1 = (float)((m.R0C0 * m.R2C2 - m.R0C2 * m.R2C0) * invdet);
            minv.R1C2 = (float)((m.R1C0 * m.R0C2 - m.R0C0 * m.R1C2) * invdet);
            minv.R2C0 = (float)((m.R1C0 * m.R2C1 - m.R2C0 * m.R1C1) * invdet);
            minv.R2C1 = (float)((m.R2C0 * m.R0C1 - m.R0C0 * m.R2C1) * invdet);
            minv.R2C2 = (float)((m.R0C0 * m.R1C1 - m.R1C0 * m.R0C1) * invdet);
        }
Exemple #2
0
 /// <summary>
 ///     Compares two objects for equality by value.
 /// </summary>
 public static bool operator ==(UIBox2 a, UIBox2 b)
 {
     return(FloatMath.CloseTo(a.Bottom, b.Bottom) &&
            FloatMath.CloseTo(a.Right, b.Right) &&
            FloatMath.CloseTo(a.Top, b.Top) &&
            FloatMath.CloseTo(a.Left, b.Left));
 }
Exemple #3
0
        /// <summary>
        ///     Creates a new instance of a Ray.
        /// </summary>
        /// <param name="position">Starting position of the ray.</param>
        /// <param name="direction">Unit direction vector that the ray is pointing.</param>
        public Ray(Vector2 position, Vector2 direction)
        {
            _position  = position;
            _direction = direction;

            DebugTools.Assert(FloatMath.CloseTo(_direction.LengthSquared, 1));
        }
Exemple #4
0
        /// <summary>
        ///     Creates a new instance of a Ray.
        /// </summary>
        /// <param name="position">Starting position of the ray.</param>
        /// <param name="direction">Unit direction vector that the ray is pointing.</param>
        public Ray(Vector2 position, Vector2 direction, int collisionMask)
        {
            _position      = position;
            _direction     = direction;
            _collisionMask = collisionMask;

            DebugTools.Assert(FloatMath.CloseTo(_direction.LengthSquared, 1));
        }
Exemple #5
0
        private bool Contains(float x, float y)
        {
            var dx = Position.X - x;
            var dy = Position.Y - y;

            var d2 = dx * dx + dy * dy;
            var r2 = Radius * Radius;

            // Instead of d2 <= r2, use FloatMath.CloseTo to allow for some tolerance.
            return((d2 < r2) || FloatMath.CloseTo(d2, r2));
        }
Exemple #6
0
        private static bool EqualsApprox(Angle a, Angle b)
        {
            // reduce both angles
            var aReduced = Reduce(a.Theta);
            var bReduced = Reduce(b.Theta);

            var aPositive = FlipPositive(aReduced);
            var bPositive = FlipPositive(bReduced);

            // The second two expressions cover an edge case where one number is barely non-negative while the other number is negative.
            // In this case, the negative number will get FlipPositived to ~2pi and the comparison will give a false negative.
            return(FloatMath.CloseTo(aPositive, bPositive) ||
                   FloatMath.CloseTo(aPositive + MathHelper.TwoPi, bPositive) ||
                   FloatMath.CloseTo(aPositive, bPositive + MathHelper.TwoPi));
        }
Exemple #7
0
 public bool IsEmpty()
 {
     return(FloatMath.CloseTo(Width, 0.0f) && FloatMath.CloseTo(Height, 0.0f));
 }