convenience methods for operating on various types

This commit is contained in:
Michael Becker 2019-11-29 18:46:02 -05:00
parent 48d5ebb25e
commit 3e0b638cff
No known key found for this signature in database
GPG Key ID: 389DFF5D73781A12
4 changed files with 105 additions and 1 deletions

View File

@ -0,0 +1,51 @@
//
// ArrayExtensions.cs
//
// Author:
// Mike Becker <alcexhim@gmail.com>
//
// 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 <http://www.gnu.org/licenses/>.
using System;
namespace MBS.Framework
{
public static class ArrayExtensions
{
public static void Bisect<T>(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<T>(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<T>(ref array, old.Length - length);
Array.Copy(old, 0, array, 0, start);
Array.Copy(old, start + length, array, start, array.Length - start - length);
}
}
}

View File

@ -34,6 +34,13 @@ namespace MBS.Framework.Drawing
Width = size.Width; Width = size.Width;
Height = size.Height; 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) public Rectangle(double x, double y, double width, double height)
{ {
X = x; X = x;
@ -71,6 +78,33 @@ namespace MBS.Framework.Drawing
return rect; return rect;
} }
/// <summary>
/// Normalize this instance.
/// </summary>
/// <returns>The normalize.</returns>
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) public bool Contains(double x, double y)
{ {
@ -80,6 +114,11 @@ namespace MBS.Framework.Drawing
{ {
return Contains(point.X, point.Y); 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) public int CompareTo(Rectangle other)
{ {
@ -129,5 +168,13 @@ namespace MBS.Framework.Drawing
{ {
return base.Equals(obj); 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);
}
} }
} }

View File

@ -23,5 +23,10 @@ namespace MBS.Framework.Drawing
mvarX = x; mvarX = x;
mvarY = y; mvarY = y;
} }
public override string ToString()
{
return String.Format("({0}, {1})", X, Y);
}
} }
} }

View File

@ -46,6 +46,7 @@
<Compile Include="Drawing\Color.cs" /> <Compile Include="Drawing\Color.cs" />
<Compile Include="Drawing\Dimension.cs" /> <Compile Include="Drawing\Dimension.cs" />
<Compile Include="Drawing\Vector.cs" /> <Compile Include="Drawing\Vector.cs" />
<Compile Include="ArrayExtensions.cs" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project> </Project>