fix: Adds ISpanFormattable to Geometry structs (#1231)
* Adds ISpanFormattable to geometry structs and makes ToString() near-zero-allocation. * Adds IEquatable, and Parsable to Rectangle3D to get it in-line with the other structs. Closes #1067
This commit is contained in:
parent
c75bde55da
commit
d7d914df6c
12 changed files with 721 additions and 195 deletions
|
|
@ -20,37 +20,104 @@ namespace Server;
|
|||
[NoSort]
|
||||
[Parsable]
|
||||
[PropertyObject]
|
||||
public struct Rectangle2D
|
||||
public struct Rectangle2D : IEquatable<Rectangle2D>, ISpanFormattable
|
||||
{
|
||||
public bool Equals(Rectangle2D other) => m_Start == other.m_Start && m_End == other.m_End;
|
||||
|
||||
public static bool operator ==(Rectangle2D l, Rectangle2D r) => l.m_Start == r.m_Start && l.m_End == r.m_End;
|
||||
|
||||
public static bool operator !=(Rectangle2D l, Rectangle2D r) => l.m_Start != r.m_Start || l.m_End != r.m_End;
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(m_Start, m_End);
|
||||
|
||||
private Point2D m_Start;
|
||||
private Point2D m_End;
|
||||
private Point2D _start;
|
||||
private Point2D _end;
|
||||
|
||||
public Rectangle2D(Point2D start, Point2D end)
|
||||
{
|
||||
m_Start = start;
|
||||
m_End = end;
|
||||
_start = start;
|
||||
_end = end;
|
||||
}
|
||||
|
||||
public Rectangle2D(int x, int y, int width, int height)
|
||||
{
|
||||
m_Start = new Point2D(x, y);
|
||||
m_End = new Point2D(x + width, y + height);
|
||||
_start = new Point2D(x, y);
|
||||
_end = new Point2D(x + width, y + height);
|
||||
}
|
||||
|
||||
public void Set(int x, int y, int width, int height)
|
||||
{
|
||||
m_Start = new Point2D(x, y);
|
||||
m_End = new Point2D(x + width, y + height);
|
||||
_start = new Point2D(x, y);
|
||||
_end = new Point2D(x + width, y + height);
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Point2D Start
|
||||
{
|
||||
get => _start;
|
||||
set => _start = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Point2D End
|
||||
{
|
||||
get => _end;
|
||||
set => _end = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int X
|
||||
{
|
||||
get => _start.m_X;
|
||||
set => _start.m_X = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Y
|
||||
{
|
||||
get => _start.m_Y;
|
||||
set => _start.m_Y = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Width
|
||||
{
|
||||
get => _end.m_X - _start.m_X;
|
||||
set => _end.m_X = _start.m_X + value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Height
|
||||
{
|
||||
get => _end.m_Y - _start.m_Y;
|
||||
set => _end.m_Y = _start.m_Y + value;
|
||||
}
|
||||
|
||||
public void MakeHold(Rectangle2D r)
|
||||
{
|
||||
if (r._start.m_X < _start.m_X)
|
||||
{
|
||||
_start.m_X = r._start.m_X;
|
||||
}
|
||||
|
||||
if (r._start.m_Y < _start.m_Y)
|
||||
{
|
||||
_start.m_Y = r._start.m_Y;
|
||||
}
|
||||
|
||||
if (r._end.m_X > _end.m_X)
|
||||
{
|
||||
_end.m_X = r._end.m_X;
|
||||
}
|
||||
|
||||
if (r._end.m_Y > _end.m_Y)
|
||||
{
|
||||
_end.m_Y = r._end.m_Y;
|
||||
}
|
||||
}
|
||||
|
||||
public bool Equals(Rectangle2D other) => _start == other._start && _end == other._end;
|
||||
|
||||
public override bool Equals(object obj) => obj is Rectangle2D other && Equals(other);
|
||||
|
||||
public override int GetHashCode() => HashCode.Combine(_start, _end);
|
||||
|
||||
public static bool operator ==(Rectangle2D l, Rectangle2D r) => l._start == r._start && l._end == r._end;
|
||||
|
||||
public static bool operator !=(Rectangle2D l, Rectangle2D r) => l._start != r._start || l._end != r._end;
|
||||
|
||||
public static Rectangle2D Parse(string value)
|
||||
{
|
||||
var start = value.IndexOfOrdinal('(');
|
||||
|
|
@ -76,79 +143,33 @@ public struct Rectangle2D
|
|||
return new Rectangle2D(x, y, w, h);
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Point2D Start
|
||||
public bool Contains(Point3D p) =>
|
||||
_start.m_X <= p.m_X && _start.m_Y <= p.m_Y && _end.m_X > p.m_X && _end.m_Y > p.m_Y;
|
||||
|
||||
public bool Contains(Point2D p) =>
|
||||
_start.m_X <= p.m_X && _start.m_Y <= p.m_Y && _end.m_X > p.m_X && _end.m_Y > p.m_Y;
|
||||
|
||||
public bool Contains(int x, int y) =>
|
||||
_start.m_X <= x && _start.m_Y <= y && _end.m_X > x && _end.m_Y > y;
|
||||
|
||||
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider provider)
|
||||
=> destination.TryWrite(provider, $"({X}, {Y})+({Width}, {Height})", out charsWritten);
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
get => m_Start;
|
||||
set => m_Start = value;
|
||||
// Maximum number of characters that are needed to represent this:
|
||||
// 9 characters for (, )+(, )
|
||||
// Up to 11 characters to represent each integer
|
||||
const int maxLength = 9 + 11 * 4;
|
||||
Span<char> span = stackalloc char[maxLength];
|
||||
TryFormat(span, out var charsWritten, null, null);
|
||||
return span[..charsWritten].ToString();
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public Point2D End
|
||||
public string ToString(string format, IFormatProvider formatProvider)
|
||||
{
|
||||
get => m_End;
|
||||
set => m_End = value;
|
||||
// format and formatProvider are not doing anything right now, so use the
|
||||
// default ToString implementation.
|
||||
return ToString();
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int X
|
||||
{
|
||||
get => m_Start.m_X;
|
||||
set => m_Start.m_X = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Y
|
||||
{
|
||||
get => m_Start.m_Y;
|
||||
set => m_Start.m_Y = value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Width
|
||||
{
|
||||
get => m_End.m_X - m_Start.m_X;
|
||||
set => m_End.m_X = m_Start.m_X + value;
|
||||
}
|
||||
|
||||
[CommandProperty(AccessLevel.Counselor)]
|
||||
public int Height
|
||||
{
|
||||
get => m_End.m_Y - m_Start.m_Y;
|
||||
set => m_End.m_Y = m_Start.m_Y + value;
|
||||
}
|
||||
|
||||
public void MakeHold(Rectangle2D r)
|
||||
{
|
||||
if (r.m_Start.m_X < m_Start.m_X)
|
||||
{
|
||||
m_Start.m_X = r.m_Start.m_X;
|
||||
}
|
||||
|
||||
if (r.m_Start.m_Y < m_Start.m_Y)
|
||||
{
|
||||
m_Start.m_Y = r.m_Start.m_Y;
|
||||
}
|
||||
|
||||
if (r.m_End.m_X > m_End.m_X)
|
||||
{
|
||||
m_End.m_X = r.m_End.m_X;
|
||||
}
|
||||
|
||||
if (r.m_End.m_Y > m_End.m_Y)
|
||||
{
|
||||
m_End.m_Y = r.m_End.m_Y;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly bool Contains(Point3D p) =>
|
||||
m_Start.m_X <= p.m_X && m_Start.m_Y <= p.m_Y && m_End.m_X > p.m_X && m_End.m_Y > p.m_Y;
|
||||
|
||||
public readonly bool Contains(Point2D p) =>
|
||||
m_Start.m_X <= p.m_X && m_Start.m_Y <= p.m_Y && m_End.m_X > p.m_X && m_End.m_Y > p.m_Y;
|
||||
|
||||
public readonly bool Contains(int x, int y) =>
|
||||
m_Start.m_X <= x && m_Start.m_Y <= y && m_End.m_X > x && m_End.m_Y > y;
|
||||
|
||||
public override string ToString() => $"({X}, {Y})+({Width}, {Height})";
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue