Exemplo n.º 1
0
        public void Vector2dMagnitudeWorks()
        {
            var v = new Vector2d(2, 2);
            double mag = v.Magnitude();

            Assert.Equal(2.83, Math.Round(mag, 2));
        }
Exemplo n.º 2
0
        public void Vector2dLengthWorks()
        {
            var v = new Vector2d(2, 2);
            double length = v.Length();

            Assert.Equal(2.83, Math.Round(length, 2));
        }
Exemplo n.º 3
0
        public void Vector2dDotProductWorks()
        {
            var v = new Vector2d(3, 4);
            var v2 = new Vector2d(8, 7);

            Assert.Equal(v.Dot(v2), 52);
        }
Exemplo n.º 4
0
        public void Vector2DistanceWorks()
        {
            var v1 = new Vector2d(1, 1);
            var v2 = new Vector2d(13, 8);

            Assert.True(v1.Distance(v2).Equivalent(new Vector2d(12, 7)));
        }
Exemplo n.º 5
0
        public void Vector2dNormalizedWorks()
        {
            var v = new Vector2d(12, 23);
            var v2 = v.Normalized();

            Assert.NotEqual(v, v2);
            Assert.True(v.Equivalent(new Vector2d(12, 23)));
            Assert.Equal(1, v.Normalized().Magnitude());
        }
Exemplo n.º 6
0
        public Vector2d RotateAround(Vector2d point, double angle, int precision = 2)
        {
            var ca = Math.Cos(angle);
            var sa = Math.Sin(angle);

            return new Vector2d(
                Math.Round(ca * (X - point.X) - sa * (Y - point.Y) + point.X, precision),
                Math.Round(sa * (X - point.X) + ca * (Y - point.Y) + point.Y, precision)
            );
        }
Exemplo n.º 7
0
        public void Vector2dCloneWorks()
        {
            var v = new Vector2d(1, 2);
            var v2 = v.Clone();

            v2.X = 3;

            Assert.NotEqual(v, v2);
            Assert.False(v.Equivalent(v2));
            Assert.True(v.Equivalent(new Vector2d(1, 2)));
        }
Exemplo n.º 8
0
        public void GetMinMaxProjectionsWorks()
        {
            var axis = new Vector2d(8, 0);
            var vertices = new Vector2d[] {
                new Vector2d(2, 0),  // TL
                new Vector2d(5, 3),  // TR
                new Vector2d(0, -2), // BL
                new Vector2d(3, 5)   // BR
            };

            double expectedMax = vertices[1].ProjectOnto(axis).Dot(axis);
            double expectedMin = vertices[2].ProjectOnto(axis).Dot(axis);

            var maxmin = Vector2dHelpers.GetMinMaxProjections(axis, vertices);

            Assert.True(maxmin.Min == expectedMin);
            Assert.True(maxmin.Max == expectedMax);
        }
Exemplo n.º 9
0
        public static MinMax GetMinMaxProjections(Vector2d axis, Vector2d[] vertices)
        {
            double min = vertices[0].ProjectOnto(axis).Dot(axis);
            double max = min;

            for(int i = 1;i < vertices.Length;i++)
            {
                var vertex = vertices[i];
                double value = vertex.ProjectOnto(axis).Dot(axis);

                if (value < min)
                {
                    min = value;
                }
                else if (value > max)
                {
                    max = value;
                }
            }

            return new MinMax(min, max);
        }
Exemplo n.º 10
0
 public Size2d Add(Vector2d v1)
 {
     return this + v1;
 }
Exemplo n.º 11
0
 public Size2d Multiply(Vector2d v1)
 {
     return this * v1;
 }
Exemplo n.º 12
0
 public override bool ContainsPoint(Vector2d point)
 {
     return Position.Distance(point).Magnitude() < Radius;
 }
Exemplo n.º 13
0
        public void Vector2dMultipliesCorrectly()
        {
            var v = new Vector2d(2, 3);

            Assert.False(Vector2d.Zero == v * 0);
            Assert.False(Vector2d.Zero == v.Multiply(0));
            Assert.True(Vector2d.Zero.Equivalent(v * 0));
            Assert.True(Vector2d.Zero.Equivalent(v.Multiply(0)));
            Assert.True((v * v).Equivalent(new Vector2d(4, 9)));
            Assert.True(v.Multiply(v).Equivalent(new Vector2d(4, 9)));
            Assert.True((v * .5).Equivalent(new Vector2d(1, 1.5)));
            Assert.True(v.Multiply(.5).Equivalent(new Vector2d(1, 1.5)));
        }
Exemplo n.º 14
0
 public bool Equivalent(Vector2d v1)
 {
     return v1.X == X && v1.Y == Y;
 }
Exemplo n.º 15
0
 /// <summary>
 /// Calculates the Dot product of the current vector and the one provided
 /// </summary>
 /// <param name="v1">Vector to dot product with</param>
 /// <returns>Dot product</returns>
 public double Dot(Vector2d v1)
 {
     return v1.X * X + v1.Y * Y;
 }
Exemplo n.º 16
0
 public Vector2d ProjectOnto(Vector2d v)
 {
     return (this.Dot(v) / v.Dot(v)) * v;
 }
Exemplo n.º 17
0
        public void Vector2dSignWorks()
        {
            var v = new Vector2d(3, -4);

            Assert.True(v.Sign().Equivalent(new Vector2d(1, -1)));
        }
Exemplo n.º 18
0
        public override bool ContainsPoint(Vector2d point)
        {
            double savedRotation = Rotation;

            if (Rotation != 0)
            {
                Rotation = 0;
                point = point.RotateAround(Position, -savedRotation);
            }

            Vector2d myTopLeft = TopLeft,
                     myBotRight = BotRight;

            Rotation = savedRotation;

            return point.X <= myBotRight.X && point.X >= myTopLeft.X && point.Y <= myBotRight.Y && point.Y >= myTopLeft.Y;
        }
Exemplo n.º 19
0
        public void Vector2dSubtractsCorrectly()
        {
            var v = new Vector2d(2, 3);

            Assert.False(v == v - 0);
            Assert.False(v == v.Subtract(0));
            Assert.False(-v == v.SubtractFrom(0));
            Assert.True(v.Equivalent(v - 0));
            Assert.True(v.Equivalent(v.Subtract(0)));
            Assert.True((-v).Equivalent(0- v));
            Assert.True((-v).Equivalent(v.SubtractFrom(0)));
            Assert.True((v - v).Equivalent(Vector2d.Zero));
            Assert.True((v.Subtract(v)).Equivalent(Vector2d.Zero));
            Assert.True((v - .1).Equivalent(new Vector2d(1.9, 2.9)));
            Assert.True((v.Subtract(.1)).Equivalent(new Vector2d(1.9, 2.9)));
            Assert.True((.1 - v).Equivalent(new Vector2d(-1.9, -2.9)));
            Assert.True((v.SubtractFrom(.1)).Equivalent(new Vector2d(-1.9, -2.9)));
        }
Exemplo n.º 20
0
 public Size2d Divide(Vector2d v1)
 {
     return this / v1;
 }
Exemplo n.º 21
0
 public CollisionData(Vector2d at, Collidable with)
 {
     At = at;
     With = with;
 }
Exemplo n.º 22
0
        public void Vector2dTriggerWorks()
        {
            var v = new Vector2d(2, 3);
            double total = 0;
            Action<double> sum = (val) =>
            {
                total += val;
            };

            v.Trigger(sum);

            Assert.Equal(5, total);
        }
Exemplo n.º 23
0
 public Size2d Subtract(Vector2d v1)
 {
     return this - v1;
 }
Exemplo n.º 24
0
        public void Vector2dDividesCorrectly()
        {
            var v = new Vector2d(2, 4);

            Assert.False(v == v / 1);
            Assert.False(v == v.Divide(1));
            Assert.True(v.Equivalent(v / 1));
            Assert.True(v.Equivalent(v.Divide(1)));
            Assert.True(new Vector2d(.5, .25).Equivalent(1 / v));
            Assert.True(new Vector2d(.5, .25).Equivalent(v.DivideFrom(1)));
            Assert.True(new Vector2d(20, 40).Equivalent(v / .1));
            Assert.True(new Vector2d(20, 40).Equivalent(v.Divide(.1)));
        }
Exemplo n.º 25
0
 public Size2d SubtractFrom(Vector2d v1)
 {
     return v1 - this;
 }
Exemplo n.º 26
0
        public void Vector2dAbsWorks()
        {
            var v = new Vector2d(3, -4);

            Assert.True(v.Abs().Equivalent(new Vector2d(3, 4)));
        }
Exemplo n.º 27
0
 public Size2d DivideFrom(Vector2d v1)
 {
     return v1 / this;
 }
Exemplo n.º 28
0
 public abstract bool ContainsPoint(Vector2d point);
Exemplo n.º 29
0
        public override bool Intersects(BoundingRectangle obj)
        {
            if (Rotation == 0 && obj.Rotation == 0)
            {
                Vector2d myTopLeft = TopLeft,
                         myBotRight = BotRight,
                         theirTopLeft = obj.TopLeft,
                         theirBotRight = obj.BotRight;

                return theirTopLeft.X <= myBotRight.X && theirBotRight.X >= myTopLeft.X && theirTopLeft.Y <= myBotRight.Y && theirBotRight.Y >= myTopLeft.Y;

            }
            else if (obj.Position.Distance(Position).Magnitude() <= obj.Size.Radius + Size.Radius) // Check if we're somewhat close to the object that we might be colliding with
            {
                var axisList = new Vector2d[] { TopRight - TopLeft, TopRight - BotRight, obj.TopLeft - obj.BotLeft, obj.TopLeft - obj.TopRight };
                var myVertices = Vertices;
                var theirVertices = obj.Vertices;

                foreach (var axi in axisList)
                {
                    var myProjections = Vector2dHelpers.GetMinMaxProjections(axi, myVertices);
                    var theirProjections = Vector2dHelpers.GetMinMaxProjections(axi, theirVertices);

                    // No collision
                    if (theirProjections.Max < myProjections.Min || myProjections.Max < theirProjections.Min)
                    {
                        return false;
                    }
                }

                return true;
            }

            return false;
        }
Exemplo n.º 30
0
 public Vector2d Distance(Vector2d v1)
 {
     return new Vector2d(Math.Abs(v1.X - X), Math.Abs(v1.Y - Y));
 }