diff --git a/MBS.Framework/ArrayExtensions.cs b/MBS.Framework/ArrayExtensions.cs new file mode 100644 index 0000000..881a3f3 --- /dev/null +++ b/MBS.Framework/ArrayExtensions.cs @@ -0,0 +1,51 @@ +// +// ArrayExtensions.cs +// +// Author: +// Mike Becker +// +// Copyright (c) 2019 Mike Becker +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +using System; +namespace MBS.Framework +{ + public static class ArrayExtensions + { + public static void Bisect(this T[] array, int index, out T[] left, out T[] right) + { + left = new T[index]; + right = new T[array.Length - index]; + + Array.Copy(array, 0, left, 0, left.Length); + Array.Copy(array, index, right, 0, right.Length); + } + public static void Array_RemoveAt(ref T[] array, int index, int count = 1) + { + T[] old = (T[])array.Clone(); + + int start = index; + int length = count; + if (count < 0) + { + start = index + count; + length = Math.Abs(count); + } + Array.Resize(ref array, old.Length - length); + + Array.Copy(old, 0, array, 0, start); + Array.Copy(old, start + length, array, start, array.Length - start - length); + } + } +} diff --git a/MBS.Framework/Drawing/Rectangle.cs b/MBS.Framework/Drawing/Rectangle.cs index d9045f0..9f2babd 100644 --- a/MBS.Framework/Drawing/Rectangle.cs +++ b/MBS.Framework/Drawing/Rectangle.cs @@ -34,6 +34,13 @@ namespace MBS.Framework.Drawing Width = size.Width; Height = size.Height; } + public Rectangle(Vector2D topLeft, Vector2D bottomRight) + { + X = topLeft.X; + Y = topLeft.Y; + Width = (bottomRight.X - topLeft.X); + Height = (bottomRight.Y - topLeft.Y); + } public Rectangle(double x, double y, double width, double height) { X = x; @@ -71,6 +78,33 @@ namespace MBS.Framework.Drawing return rect; } + /// + /// Normalize this instance. + /// + /// The normalize. + public Rectangle Normalize() + { + double x = this.X; + double y = this.Y; + double r = this.Right; + double b = this.Bottom; + + if (x > r) + { + double t = x; + x = r; + r = t; + } + if (y > b) + { + double t = y; + y = b; + b = t; + } + + return new Rectangle(x, y, r - x, b - y); + } + public bool Contains(double x, double y) { @@ -80,6 +114,11 @@ namespace MBS.Framework.Drawing { return Contains(point.X, point.Y); } + public bool Contains(Rectangle rect) + { + return rect.X >= X && rect.Y >= Y && rect.Right <= Right && rect.Bottom <= Bottom; + } + public int CompareTo(Rectangle other) { @@ -129,5 +168,13 @@ namespace MBS.Framework.Drawing { return base.Equals(obj); } + + public bool IntersectsWith(Rectangle rect) + { + return (rect.X < this.Right) && + (this.X < (rect.Right)) && + (rect.Y < this.Bottom) && + (this.Y < rect.Bottom); + } } } diff --git a/MBS.Framework/Drawing/Vector.cs b/MBS.Framework/Drawing/Vector.cs index 9f69160..84d25e1 100644 --- a/MBS.Framework/Drawing/Vector.cs +++ b/MBS.Framework/Drawing/Vector.cs @@ -23,5 +23,10 @@ namespace MBS.Framework.Drawing mvarX = x; mvarY = y; } + + public override string ToString() + { + return String.Format("({0}, {1})", X, Y); + } } } diff --git a/MBS.Framework/MBS.Framework.csproj b/MBS.Framework/MBS.Framework.csproj index d6643ac..0e22737 100644 --- a/MBS.Framework/MBS.Framework.csproj +++ b/MBS.Framework/MBS.Framework.csproj @@ -46,6 +46,7 @@ + - \ No newline at end of file +