// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Runtime.CompilerServices;
namespace SixLabors.PolygonClipper {
///
/// Represents a line segment on a plane.
///
internal readonly struct Segment : IEquatable
{
///
/// Initializes a new instance of the struct.
///
/// The segment source.
/// The segment target.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Segment(in Vertex source, in Vertex target)
{
this.Source = source;
this.Target = target;
this.Min = Vertex.Min(source, target);
this.Max = Vertex.Max(source, target);
}
///
/// Gets the segment source vector.
///
public Vertex Source { get; }
///
/// Gets the segment target vector.
///
public Vertex Target { get; }
///
/// Gets the point of the segment with lexicographically smallest coordinate.
///
public Vertex Min { get; }
///
/// Gets the point of the segment with lexicographically largest coordinate.
///
public Vertex Max { get; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(in Segment left, in Segment right)
=> left.Equals(right);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(in Segment left, in Segment right)
=> !(left == right);
///
/// Gets a value indicating whether the segment is degenerate.
///
///
/// if the segment is degenerate; otherwise .
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsDegenerate() => this.Source.Equals(this.Target);
///
/// Gets a value indicating whether the segment is vertical.
///
///
/// if the segment is vertical; otherwise .
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsVertical() => this.Source.X == this.Target.X;
///
/// Changes the segment orientation.
///
/// The .
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Segment Reverse()
=> new(this.Target, this.Source);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Equals(object? obj)
=> obj is Segment segment && this.Equals(segment);
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Segment other)
=> this.Source.Equals(other.Source) && this.Target.Equals(other.Target);
///
public override int GetHashCode()
=> HashCode.Combine(this.Source, this.Target);
}
}