public void Vector2dMagnitudeWorks() { var v = new Vector2d(2, 2); double mag = v.Magnitude(); Assert.Equal(2.83, Math.Round(mag, 2)); }
public void Vector2dLengthWorks() { var v = new Vector2d(2, 2); double length = v.Length(); Assert.Equal(2.83, Math.Round(length, 2)); }
public void Vector2dDotProductWorks() { var v = new Vector2d(3, 4); var v2 = new Vector2d(8, 7); Assert.Equal(v.Dot(v2), 52); }
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))); }
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()); }
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) ); }
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))); }
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); }
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); }
public Size2d Add(Vector2d v1) { return this + v1; }
public Size2d Multiply(Vector2d v1) { return this * v1; }
public override bool ContainsPoint(Vector2d point) { return Position.Distance(point).Magnitude() < Radius; }
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))); }
public bool Equivalent(Vector2d v1) { return v1.X == X && v1.Y == Y; }
/// <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; }
public Vector2d ProjectOnto(Vector2d v) { return (this.Dot(v) / v.Dot(v)) * v; }
public void Vector2dSignWorks() { var v = new Vector2d(3, -4); Assert.True(v.Sign().Equivalent(new Vector2d(1, -1))); }
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; }
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))); }
public Size2d Divide(Vector2d v1) { return this / v1; }
public CollisionData(Vector2d at, Collidable with) { At = at; With = with; }
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); }
public Size2d Subtract(Vector2d v1) { return this - v1; }
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))); }
public Size2d SubtractFrom(Vector2d v1) { return v1 - this; }
public void Vector2dAbsWorks() { var v = new Vector2d(3, -4); Assert.True(v.Abs().Equivalent(new Vector2d(3, 4))); }
public Size2d DivideFrom(Vector2d v1) { return v1 / this; }
public abstract bool ContainsPoint(Vector2d point);
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; }
public Vector2d Distance(Vector2d v1) { return new Vector2d(Math.Abs(v1.X - X), Math.Abs(v1.Y - Y)); }