convenience methods for operating on various types
This commit is contained in:
parent
48d5ebb25e
commit
3e0b638cff
51
MBS.Framework/ArrayExtensions.cs
Normal file
51
MBS.Framework/ArrayExtensions.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,5 +23,10 @@ namespace MBS.Framework.Drawing
|
||||
mvarX = x;
|
||||
mvarY = y;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return String.Format("({0}, {1})", X, Y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,6 +46,7 @@
|
||||
<Compile Include="Drawing\Color.cs" />
|
||||
<Compile Include="Drawing\Dimension.cs" />
|
||||
<Compile Include="Drawing\Vector.cs" />
|
||||
<Compile Include="ArrayExtensions.cs" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
Loading…
x
Reference in New Issue
Block a user