Example #1
0
        public bool Equals(VectorF other)
        {
            if (object.ReferenceEquals(this, other))
            {
                return(true);
            }
            if (object.ReferenceEquals(null, other))
            {
                return(false);
            }

            var otherComponents = other._components;

            if (_components.Length != otherComponents.Length)
            {
                return(false);
            }

#if HAS_CODECONTRACTS
            Assume(_components.Length == otherComponents.Length);
#endif

            for (int dimension = 0; dimension < _components.Length; dimension++)
            {
                if (!_components[dimension].Equals(otherComponents[dimension]))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #2
0
        public float GetDistanceSquared(VectorF other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }

            var otherComponenets = other._components;

            if (otherComponenets.Length != _components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            if (_components.Length == 0)
            {
                return(0.0f);
            }

            var sum = MathEx.Square(_components[0] - otherComponenets[0]);

            for (var i = 1; i < _components.Length; i++)
            {
                sum += MathEx.Square(_components[i] - otherComponenets[i]);
            }

            return(sum);
        }
Example #3
0
        public float GetDot(VectorF right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }

            var rightComponents = right._components;

            if (_components.Length != rightComponents.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            if (_components.Length == 0)
            {
                return(0.0f);
            }

            var sum = _components[0] * rightComponents[0];

            for (int i = 1; i < _components.Length; i++)
            {
                sum += _components[i] * rightComponents[i];
            }

            return(sum);
        }
Example #4
0
        public VectorF(VectorF source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            _components = Clone(source._components);
        }
Example #5
0
        public VectorF GetScaled(float scalar)
        {
            var scaled           = new VectorF(_components.Length);
            var scaledComponents = scaled._components;

            for (int i = 0; i < scaledComponents.Length; i++)
            {
                scaledComponents[i] = _components[i] * scalar;
            }

            return(scaled);
        }
Example #6
0
        public VectorF GetProjected(VectorF other)
        {
            if (other.Dimensions != Dimensions)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            var scalarDenominator = GetMagnitudeSquared();

            return(scalarDenominator == 0.0f
                ? other
                : GetScaled(GetDot(other) / scalarDenominator));
        }
Example #7
0
        public VectorF GetNegative()
        {
            var negated = new VectorF(_components.Length);

            var negatedComponents = negated._components;

            for (var i = 0; i < _components.Length; i++)
            {
                negatedComponents[i] = -_components[i];
            }

            return(negated);
        }
Example #8
0
        public VectorF GetQuotient(float divisor)
        {
            var quotient = new VectorF(_components.Length);

            var quotientComponents = quotient._components;

            for (int i = 0; i < _components.Length; i++)
            {
                quotientComponents[i] = _components[i] / divisor;
            }

            return(quotient);
        }
Example #9
0
        public float GetAngleBetween(VectorF other)
        {
            if (other == null)
            {
                throw new ArgumentNullException(nameof(other));
            }
            if (other._components.Length != _components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(other));
            }

            return((float)Math.Acos(
                       GetDot(other)
                       / (float)Math.Sqrt(GetMagnitudeSquared() * other.GetMagnitudeSquared())));
        }
Example #10
0
        public static VectorF CreateUnit(int size, int dimension)
        {
            if (size <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(size));
            }
            if (dimension >= size)
            {
                throw new ArgumentOutOfRangeException(nameof(dimension));
            }

            var result = new VectorF(size);

            result.Set(dimension, 1.0f);
            return(result);
        }
Example #11
0
        public void Subtract(VectorF right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                _components[i] -= rightComponents[i];
            }
        }
Example #12
0
        public VectorF GetDiffernce(VectorF right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var difference      = new VectorF(_components.Length);
            var diffComponents  = difference._components;
            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                diffComponents[i] = _components[i] - rightComponents[i];
            }

            return(difference);
        }
Example #13
0
        public VectorF GetSum(VectorF right)
        {
            if (right == null)
            {
                throw new ArgumentNullException(nameof(right));
            }
            if (_components.Length != right._components.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(right));
            }

            var sum             = new VectorF(_components.Length);
            var sumComponents   = sum._components;
            var rightComponents = right._components;

            for (int i = 0; i < _components.Length; i++)
            {
                sumComponents[i] = _components[i] + rightComponents[i];
            }

            return(sum);
        }
Example #14
0
 public float GetDistance(VectorF other)
 {
     return((float)Math.Sqrt(GetDistanceSquared(other)));
 }