MoonTools.Bonk/Bonk/Shapes/Rectangle.cs

60 lines
1.6 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using System;
2019-10-25 10:46:47 +00:00
using System.Collections.Generic;
using System.Linq;
2019-09-06 08:11:58 +00:00
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
2019-10-25 10:46:47 +00:00
using MoreLinq;
2019-09-06 08:11:58 +00:00
namespace MoonTools.Core.Bonk
{
public struct Rectangle : IShape2D, IEquatable<IShape2D>
{
public int MinX { get; }
public int MinY { get; }
public int MaxX { get; }
public int MaxY { get; }
2019-10-25 10:46:47 +00:00
private IEnumerable<Position2D> vertices
{
get
{
yield return new Position2D(MinX, MinY);
yield return new Position2D(MinX, MaxY);
yield return new Position2D(MaxX, MinY);
yield return new Position2D(MaxX, MaxY);
}
}
2019-09-06 08:11:58 +00:00
public Rectangle(int minX, int minY, int maxX, int maxY)
{
MinX = minX;
MinY = minY;
MaxX = maxX;
MaxY = maxY;
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
2019-10-25 10:46:47 +00:00
return vertices.Select(vertex => Vector2.Transform(vertex, transform.TransformMatrix)).MaxBy(transformed => Vector2.Dot(transformed, direction)).First();
2019-09-06 08:11:58 +00:00
}
public AABB AABB(Transform2D Transform2D)
{
2019-09-06 20:00:35 +00:00
return Bonk.AABB.FromTransformedVertices(vertices, Transform2D);
2019-09-06 08:11:58 +00:00
}
public bool Equals(IShape2D other)
{
if (other is Rectangle rectangle)
{
return MinX == rectangle.MinX &&
MinY == rectangle.MinY &&
MaxX == rectangle.MaxX &&
MaxY == rectangle.MaxY;
}
return false;
}
}
}