From 3f5ed58eaf07241754341e3d3c991a67378ab3e1 Mon Sep 17 00:00:00 2001 From: Victor Chelaru Date: Sat, 25 Nov 2023 11:16:25 -0700 Subject: [PATCH] Added Polygon.KeepThisInsideOf Added ShapeCollection.KeepThisInsideOf --- .../FlatRedBall/Math/Geometry/Polygon.cs | 35 ++++++++++++++++++- .../Math/Geometry/ShapeCollection.cs | 18 ++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Polygon.cs b/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Polygon.cs index 2f2097f6f..aad9d3a50 100644 --- a/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Polygon.cs +++ b/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/Polygon.cs @@ -2121,7 +2121,6 @@ public bool IsPointInside(ref Point point, ref Vector3 pointOffset, ref Matrix r #endregion - public void InvertPointOrder() { Point temporaryPoint = new Point(); @@ -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() { diff --git a/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/ShapeCollection.cs b/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/ShapeCollection.cs index 1c6026fac..61c393c86 100644 --- a/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/ShapeCollection.cs +++ b/Engines/FlatRedBallXNA/FlatRedBall/Math/Geometry/ShapeCollection.cs @@ -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); + } + } } }