MoonTools.Bonk/Bonk/Shapes/Line.cs

42 lines
1.3 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 Line : IShape2D, IEquatable<IShape2D>
{
private Position2D[] vertices;
public Line(Position2D start, Position2D end)
{
vertices = new Position2D[2] { start, end };
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
2019-09-06 20:00:35 +00:00
var TransformedStart = Vector2.Transform(vertices[0], transform.TransformMatrix);
var TransformedEnd = Vector2.Transform(vertices[1], transform.TransformMatrix);
return Vector2.Dot(TransformedStart, direction) > Vector2.Dot(TransformedEnd, direction) ?
TransformedStart :
TransformedEnd;
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 Line)
{
var otherLine = (Line)other;
return vertices[0].ToVector2() == otherLine.vertices[0].ToVector2() && vertices[1].ToVector2() == otherLine.vertices[1].ToVector2();
}
return false;
}
}
}