Skip to content

Commit

Permalink
Added Polygon.KeepThisInsideOf
Browse files Browse the repository at this point in the history
Added ShapeCollection.KeepThisInsideOf
  • Loading branch information
vchelaru committed Nov 25, 2023
1 parent ab3da82 commit 3f5ed58
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Polygon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2121,7 +2121,6 @@ public bool IsPointInside(ref Point point, ref Vector3 pointOffset, ref Matrix r

#endregion


public void InvertPointOrder()
{
Point temporaryPoint = new Point();
Expand All @@ -2137,6 +2136,40 @@ public void InvertPointOrder()
}
}

public void KeepThisInsideOf(AxisAlignedRectangle rectangle)
{
Vector2 repositionVector;
repositionVector.X = 0;
repositionVector.Y = 0;

this.ForceUpdateDependencies();

for(int i = 0; i < mVertices.Length; i++)
{
if (mVertices[i].Position.X < rectangle.Left)
{
repositionVector.X = System.Math.Max(repositionVector.X, rectangle.Left - mVertices[i].Position.X);
}
else if (mVertices[i].Position.X > rectangle.Right)
{
repositionVector.X = System.Math.Min(repositionVector.X, rectangle.Right - mVertices[i].Position.X);
}

if (mVertices[i].Position.Y > rectangle.Top)
{
repositionVector.Y = System.Math.Min(repositionVector.Y, rectangle.Top - mVertices[i].Position.Y);
}
else if (mVertices[i].Position.Y < rectangle.Bottom)
{
repositionVector.Y = System.Math.Max(repositionVector.Y, rectangle.Bottom - mVertices[i].Position.Y);
}
}

PositionedObject topParent = this.TopParent;

topParent.Position.X += repositionVector.X;
topParent.Position.Y += repositionVector.Y;
}

public void OptimizeRadius()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2198,5 +2198,23 @@ public bool IsMouseOver(Gui.Cursor cursor, Layer layer)
{
return cursor.IsOn3D(this, layer);
}

public void KeepThisInsideOf(AxisAlignedRectangle rectangle)
{
for(int i = 0; i < mCircles.Count; i++)
{
mCircles[i].KeepThisInsideOf(rectangle);
}

for(int i = 0; i < mAxisAlignedRectangles.Count; i++)
{
mAxisAlignedRectangles[i].KeepThisInsideOf(rectangle);
}

for(int i = 0; i < mPolygons.Count; i++)
{
mPolygons[i].KeepThisInsideOf(rectangle);
}
}
}
}

0 comments on commit 3f5ed58

Please sign in to comment.