MoonTools.Bonk/Bonk/Rectangle.cs

70 lines
1.9 KiB
C#
Raw Normal View History

2019-09-06 08:11:58 +00:00
using System;
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
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; }
private Position2D[] vertices;
public Rectangle(int minX, int minY, int maxX, int maxY)
{
MinX = minX;
MinY = minY;
MaxX = maxX;
MaxY = maxY;
vertices = new Position2D[4]
{
new Position2D(minX, minY),
new Position2D(minX, maxY),
new Position2D(maxX, minY),
new Position2D(maxX, maxY)
};
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
var furthestDistance = float.NegativeInfinity;
var furthestVertex = Vector2.Transform(vertices[0], transform.TransformMatrix);
foreach (var v in vertices)
{
2019-09-06 20:00:35 +00:00
var TransformedVertex = Vector2.Transform(v, transform.TransformMatrix);
var distance = Vector2.Dot(TransformedVertex, direction);
2019-09-06 08:11:58 +00:00
if (distance > furthestDistance)
{
furthestDistance = distance;
2019-09-06 20:00:35 +00:00
furthestVertex = TransformedVertex;
2019-09-06 08:11:58 +00:00
}
}
return furthestVertex;
}
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;
}
}
}