MoonTools.Bonk/Bonk/Shapes/Line.cs

76 lines
1.9 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-10-31 23:19:30 +00:00
using System.Numerics;
2019-09-06 08:11:58 +00:00
using MoonTools.Core.Structs;
namespace MoonTools.Core.Bonk
{
2019-10-25 21:01:36 +00:00
/// <summary>
/// A line is a shape defined by exactly two points in space.
/// </summary>
public struct Line : IShape2D, IEquatable<Line>
2019-09-06 08:11:58 +00:00
{
2019-10-25 10:46:47 +00:00
private Position2D v0;
private Position2D v1;
2019-12-02 06:25:05 +00:00
public IEnumerable<Position2D> Vertices
2019-10-25 10:46:47 +00:00
{
get
{
yield return v0;
yield return v1;
2019-10-25 10:46:47 +00:00
}
}
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-12-02 05:51:52 +00:00
return Bonk.AABB.FromTransformedVertices(Vertices, Transform2D);
2019-09-06 08:11:58 +00:00
}
2019-10-25 23:20:43 +00:00
public override bool Equals(object obj)
{
return obj is IShape2D other && Equals(other);
2019-10-25 23:20:43 +00:00
}
2019-09-06 08:11:58 +00:00
public bool Equals(IShape2D other)
{
return other is Line otherLine && Equals(otherLine);
}
2019-09-06 08:11:58 +00:00
public bool Equals(Line other)
{
return (v0 == other.v0 && v1 == other.v1) || (v1 == other.v0 && v0 == other.v1);
2019-09-06 08:11:58 +00:00
}
2019-10-25 23:20:43 +00:00
public override int GetHashCode()
{
return HashCode.Combine(v0, v1);
2019-10-25 23:20:43 +00:00
}
public static bool operator ==(Line a, Line b)
{
return a.Equals(b);
}
public static bool operator !=(Line a, Line b)
{
return !(a == b);
}
2019-09-06 08:11:58 +00:00
}
}