Skip to content

Commit

Permalink
Added position updater to shapes
Browse files Browse the repository at this point in the history
  • Loading branch information
exectails committed Jul 18, 2024
1 parent 1b8fcdb commit 20dd8cf
Show file tree
Hide file tree
Showing 19 changed files with 381 additions and 13 deletions.
42 changes: 40 additions & 2 deletions Yggdrasil.Test/Geometry/Shapes/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,48 @@ public void GetEdgePoints()
new Vector2(499, 700),
};

//for (var i = 0; i < 8; ++i)
// Assert.Equal(expected[i], shape.GetEdgePoints(8)[i]);
Assert.Equal(expected, shape.GetEdgePoints(8));
}

[Fact]
public void UpdatePosition()
{
var shape = new Circle(new Vector2(500, 500), 200);
shape.UpdatePosition(new Vector2(600, 600));

var expected = new Vector2[]
{
new Vector2(400, 600),
new Vector2(600, 400),
new Vector2(800, 600),
new Vector2(600, 800),
};

Assert.Equal(expected, shape.GetEdgePoints(4));

expected = new Vector2[]
{
new Vector2(458, 741),
new Vector2(400, 499),
new Vector2(458, 458),
new Vector2(600, 400),
new Vector2(741, 458),
new Vector2(800, 600),
new Vector2(741, 741),
new Vector2(599, 800),
};

Assert.Equal(expected, shape.GetEdgePoints(8));

Assert.True(shape.IsInside(new Vector2(600, 600)));
Assert.True(shape.IsInside(new Vector2(600, 700)));
Assert.True(shape.IsInside(new Vector2(600, 500)));
Assert.True(shape.IsInside(new Vector2(700, 600)));
Assert.True(shape.IsInside(new Vector2(500, 600)));
Assert.True(shape.IsInside(new Vector2(550, 550)));
Assert.True(shape.IsInside(new Vector2(650, 650)));
Assert.False(shape.IsInside(new Vector2(450, 750)));
Assert.False(shape.IsInside(new Vector2(750, 450)));
}
}
}
46 changes: 46 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/CircleF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,51 @@ public void GetEdgePoints()
Assert.Equal(expected[i].Y, edgePoints[i].Y, 2);
}
}

[Fact]
public void UpdatePosition()
{
var shape = new CircleF(new Vector2F(500, 500), 200);
shape.UpdatePosition(new Vector2F(600, 600));

var expected = new Vector2F[]
{
new Vector2F(400, 600),
new Vector2F(600, 400),
new Vector2F(800, 600),
new Vector2F(600, 800),
};

Assert.Equal(expected, shape.GetEdgePoints(4));

expected = new Vector2F[]
{
new Vector2F(458.58f, 741.42f),
new Vector2F(400.00f, 600.00f),
new Vector2F(458.58f, 458.58f),
new Vector2F(600.00f, 400.00f),
new Vector2F(741.42f, 458.58f),
new Vector2F(800.00f, 600.00f),
new Vector2F(741.42f, 741.42f),
new Vector2F(600.00f, 800.00f),
};

var edgePoints = shape.GetEdgePoints(8);
for (var i = 0; i < 8; ++i)
{
Assert.Equal(expected[i].X, edgePoints[i].X, 2);
Assert.Equal(expected[i].Y, edgePoints[i].Y, 2);
}

Assert.True(shape.IsInside(new Vector2F(600, 600)));
Assert.True(shape.IsInside(new Vector2F(600, 700)));
Assert.True(shape.IsInside(new Vector2F(600, 500)));
Assert.True(shape.IsInside(new Vector2F(700, 600)));
Assert.True(shape.IsInside(new Vector2F(500, 600)));
Assert.True(shape.IsInside(new Vector2F(550, 550)));
Assert.True(shape.IsInside(new Vector2F(650, 650)));
Assert.False(shape.IsInside(new Vector2F(450, 750)));
Assert.False(shape.IsInside(new Vector2F(750, 450)));
}
}
}
21 changes: 21 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/Cone.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,26 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
var shape = new Cone(new Vector2(350, 350), 0, 200, 60);
shape.UpdatePosition(new Vector2(250, 250));

var expected = new Vector2[]
{
new Vector2(250, 250),
new Vector2(423, 350),
new Vector2(450, 250),
new Vector2(423, 150),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2(300, 250)));
Assert.False(shape.IsInside(new Vector2(400, 350)));
Assert.False(shape.IsInside(new Vector2(300, -250)));
}
}
}
21 changes: 21 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/ConeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,26 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
var shape = new ConeF(new Vector2F(350, 350), 0, 200, 60);
shape.UpdatePosition(new Vector2F(250, 250));

var expected = new Vector2F[]
{
new Vector2F(250, 250),
new Vector2F(423, 350),
new Vector2F(450, 250),
new Vector2F(423, 150),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2F(300, 250)));
Assert.False(shape.IsInside(new Vector2F(400, 350)));
Assert.False(shape.IsInside(new Vector2F(300, -250)));
}
}
}
24 changes: 24 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,29 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
// Triangle
var shape = new Polygon(new Vector2(500, 500), new Vector2(400, 300), new Vector2(600, 300));
shape.UpdatePosition(new Vector2(-100, -100));

var expected = new Vector2[]
{
new Vector2(-100, -100),
new Vector2(-200, -300),
new Vector2(0, -300),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2(-100, -200)));
Assert.True(shape.IsInside(new Vector2(-210, -301)));
Assert.True(shape.IsInside(new Vector2(-190, -301)));
Assert.True(shape.IsInside(new Vector2(-210, -301)));
Assert.False(shape.IsInside(new Vector2(-190, -290)));
Assert.False(shape.IsInside(new Vector2(-210, -290)));
}
}
}
24 changes: 24 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/PolygonF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,29 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
// Triangle
var shape = new PolygonF(new Vector2F(500, 500), new Vector2F(400, 300), new Vector2F(600, 300));
shape.UpdatePosition(new Vector2F(-100, -100));

var expected = new Vector2F[]
{
new Vector2F(-100, -100),
new Vector2F(-200, -300),
new Vector2F(0, -300),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2F(-100, -200)));
Assert.True(shape.IsInside(new Vector2F(-210, -301)));
Assert.True(shape.IsInside(new Vector2F(-190, -301)));
Assert.True(shape.IsInside(new Vector2F(-210, -301)));
Assert.False(shape.IsInside(new Vector2F(-190, -290)));
Assert.False(shape.IsInside(new Vector2F(-210, -290)));
}
}
}
27 changes: 27 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/Rectangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,32 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
var shape = Rectangle.Centered(new Vector2(500, 500), new Vector2(200, 200));
shape.UpdatePosition(new Vector2(1000, 1000));

var expected = new Vector2[4]
{
new Vector2(900, 900),
new Vector2(1100, 900),
new Vector2(1100, 1100),
new Vector2(900, 1100),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2(1000, 1000)));
Assert.True(shape.IsInside(new Vector2(950, 950)));
Assert.True(shape.IsInside(new Vector2(900, 900)));
Assert.True(shape.IsInside(new Vector2(1050, 1050)));
Assert.True(shape.IsInside(new Vector2(1000, 1000)));
Assert.False(shape.IsInside(new Vector2(899, 899)));
Assert.False(shape.IsInside(new Vector2(510, 510)));
Assert.False(shape.IsInside(new Vector2(1101, 1101)));
Assert.False(shape.IsInside(new Vector2(1500, 1200)));
}
}
}
27 changes: 27 additions & 0 deletions Yggdrasil.Test/Geometry/Shapes/RectangleF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,32 @@ public void GetEdgePoints()

Assert.Equal(expected, shape.GetEdgePoints());
}

[Fact]
public void UpdatePosition()
{
var shape = RectangleF.Centered(new Vector2F(500, 500), new Vector2F(200, 200));
shape.UpdatePosition(new Vector2F(1000, 1000));

var expected = new Vector2F[4]
{
new Vector2F(900, 900),
new Vector2F(1100, 900),
new Vector2F(1100, 1100),
new Vector2F(900, 1100),
};

Assert.Equal(expected, shape.GetEdgePoints());

Assert.True(shape.IsInside(new Vector2F(1000, 1000)));
Assert.True(shape.IsInside(new Vector2F(950, 950)));
Assert.True(shape.IsInside(new Vector2F(900, 900)));
Assert.True(shape.IsInside(new Vector2F(1050, 1050)));
Assert.True(shape.IsInside(new Vector2F(1000, 1000)));
Assert.False(shape.IsInside(new Vector2F(899, 899)));
Assert.False(shape.IsInside(new Vector2F(510, 510)));
Assert.False(shape.IsInside(new Vector2F(1101, 1101)));
Assert.False(shape.IsInside(new Vector2F(1500, 1200)));
}
}
}
6 changes: 6 additions & 0 deletions Yggdrasil/Geometry/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ public interface IShape
/// <param name="rnd"></param>
/// <returns></returns>
Vector2 GetRandomPoint(Random rnd);

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
void UpdatePosition(Vector2 position);
}
}
6 changes: 6 additions & 0 deletions Yggdrasil/Geometry/ShapeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,11 @@ public interface IShapeF
/// <param name="rnd"></param>
/// <returns></returns>
Vector2F GetRandomPoint(Random rnd);

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
void UpdatePosition(Vector2F position);
}
}
13 changes: 12 additions & 1 deletion Yggdrasil/Geometry/Shapes/Circle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Circle : IShape
/// <summary>
/// Returns the circle's center position.
/// </summary>
public Vector2 Center { get; }
public Vector2 Center { get; private set; }

/// <summary>
/// Returns the circle's radius.
Expand Down Expand Up @@ -136,5 +136,16 @@ public Vector2 GetRandomPoint(Random rnd)

return new Vector2((int)x, (int)y);
}

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
public void UpdatePosition(Vector2 position)
{
this.Center = position;
_edgePoints = null;
_outlines = null;
}
}
}
11 changes: 11 additions & 0 deletions Yggdrasil/Geometry/Shapes/CircleF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,16 @@ public void RotateAround(Vector2F pivot, float degreeAngle)

this.Center = newCenterPoint;
}

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
public void UpdatePosition(Vector2F position)
{
this.Center = position;
_edgePoints = null;
_outlines = null;
}
}
}
16 changes: 15 additions & 1 deletion Yggdrasil/Geometry/Shapes/CompositeShape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class CompositeShape : IShape
/// <summary>
/// Returns the average of the combined shapes.
/// </summary>
public Vector2 Center { get; }
public Vector2 Center { get; private set; }

/// <summary>
/// Creates new instance.
Expand Down Expand Up @@ -107,5 +107,19 @@ public Vector2 GetRandomPoint(Random rnd)
var rndShape = this.Shapes.Random();
return rndShape.GetRandomPoint(rnd);
}

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
public void UpdatePosition(Vector2 position)
{
this.Center = position;
_edgePoints = null;
_outlines = null;

foreach (var shape in this.Shapes)
shape.UpdatePosition(position);
}
}
}
14 changes: 14 additions & 0 deletions Yggdrasil/Geometry/Shapes/CompositeShapeF.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,19 @@ public void RotateAround(Vector2F pivot, float degreeAngle)
_edgePoints = null;
_outlines = null;
}

/// <summary>
/// Moves shape to the given position and recalculates its properties.
/// </summary>
/// <param name="position"></param>
public void UpdatePosition(Vector2F position)
{
this.Center = position;
_edgePoints = null;
_outlines = null;

foreach (var shape in this.Shapes)
shape.UpdatePosition(position);
}
}
}
Loading

0 comments on commit 20dd8cf

Please sign in to comment.