MoonTools.Bonk/Bonk/Shapes/Line.cs

54 lines
1.4 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;
2019-09-06 08:11:58 +00:00
using Microsoft.Xna.Framework;
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
public struct Line : IShape2D, IEquatable<IShape2D>
{
2019-10-25 10:46:47 +00:00
private Position2D v0;
private Position2D v1;
private IEnumerable<Position2D> vertices
{
get
{
yield return v0;
yield return v0;
}
}
2019-09-06 08:11:58 +00:00
public Line(Position2D start, Position2D end)
{
2019-10-25 10:46:47 +00:00
v0 = start;
v1 = end;
2019-09-06 08:11:58 +00:00
}
public Vector2 Support(Vector2 direction, Transform2D transform)
{
2019-10-25 10:46:47 +00:00
var TransformedStart = Vector2.Transform(v0, transform.TransformMatrix);
var TransformedEnd = Vector2.Transform(v1, transform.TransformMatrix);
2019-09-06 20:00:35 +00:00
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;
2019-10-25 10:46:47 +00:00
return v0.ToVector2() == otherLine.v0.ToVector2() && v1.ToVector2() == otherLine.v1.ToVector2();
2019-09-06 08:11:58 +00:00
}
return false;
}
}
}