AdventOfCode/AdventOfCode.Core/Shared/Grid/Line.cs
2023-12-09 09:18:10 +01:00

57 lines
2.0 KiB
C#

using static System.Collections.Specialized.BitVector32;
namespace AdventOfCode.Core.Shared.Grid
{
/// <summary>
/// A horizontal line based on the Start Y value.
/// </summary>
/// <param name="Start"></param>
/// <param name="End"></param>
public class Line(Point start, Point end)
{
public Point Start { get; set; } = start;
public Point End { get; set; } = end;
public string Value { get; set; } = string.Empty;
public Line(Point start, long length) : this(start, new Point (start.X + length - 1, start.Y)) { }
public Line(Point start, long length, string value) : this(start, length) => Value = value;
/// <summary>
/// This intersect assumes that the line horizontal.
/// Vertical may need to be added later
/// </summary>
/// <param name="point">The point search in this line.</param>
/// <returns></returns>
public bool Intersect(Point point)
{
if (point == null) return false;
if (point.Y != Start.Y) return false;
return point.X >= Start.X && point.X <= End.X;
}
/// <summary>
/// This intersect assumes that the line horizontal.
/// Vertical may need to be added later
/// </summary>
/// <param name="line">The line to seach in this line.</param>
/// <returns></returns>
public bool Intersect(Line line)
{
if (line == null) return false;
if (line.Start.Y != Start.Y) return false;
return Intersect(line.Start) // is the start point of the other line in this line
|| Intersect(line.End) // is the end point of the other line in this line
|| (line.Start.X <= Start.X && line.End.X >= End.X); // is this line fully within the other line
}
public override string ToString()
{
return $"[{Start.Y},{Start.X}] {Value}";
}
}
}