using System;
using Ringtoets.Piping.Data.Properties;
namespace Ringtoets.Piping.Data
{
///
/// This class represents lines between two .
///
public class Segment2D
{
///
/// Creates a new instance of , with the set to
/// and the set to .
///
/// The first of the .
/// The second of the .
/// Thrown when either the or
/// point is null.
public Segment2D(Point2D first, Point2D second)
{
if (first == null || second == null)
{
throw new ArgumentException(Resources.Segment2D_Constructor_Segment_must_be_created_with_two_points);
}
FirstPoint = first;
SecondPoint = second;
}
///
/// The first of the .
///
public Point2D FirstPoint { get; private set; }
///
/// The second of the .
///
public Point2D SecondPoint { get; private set; }
///
/// This method determines whether is contained by the
/// and x coordinates.
///
/// The x for which to find out whether it is contained by the
/// and .
/// true if x is on or between the points' x coordinates. false otherwise.
public bool ContainsX(double x)
{
var distanceFirstPoint = FirstPoint.X - x;
var distanceSecondPoint = SecondPoint.X - x;
var onPoint = Math.Abs(FirstPoint.X - x) < 1e-8 || Math.Abs(SecondPoint.X - x) < 1e-8;
return onPoint || Math.Sign(distanceFirstPoint) != Math.Sign(distanceSecondPoint);
}
///
/// Determines whether the is vertical.
///
/// true if the is vertical. false otherwise.
public bool IsVertical()
{
return Math.Abs(FirstPoint.X - SecondPoint.X) < 1e-8;
}
///
/// Determines whether two segments are connected by each other's
/// and .
///
/// The segment which may be connected to the .
/// true if the segments are connected. false otherwise.
public bool IsConnected(Segment2D segment)
{
return
FirstPoint.Equals(segment.FirstPoint) ||
FirstPoint.Equals(segment.SecondPoint) ||
SecondPoint.Equals(segment.FirstPoint) ||
SecondPoint.Equals(segment.SecondPoint);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((Segment2D) obj);
}
public override int GetHashCode()
{
unchecked
{
return ((FirstPoint.X + SecondPoint.X).GetHashCode()*397) ^ (FirstPoint.Y + SecondPoint.Y).GetHashCode();
}
}
private bool Equals(Segment2D other)
{
return FirstPoint.Equals(other.FirstPoint) && SecondPoint.Equals(other.SecondPoint) ||
FirstPoint.Equals(other.SecondPoint) && SecondPoint.Equals(other.FirstPoint);
}
}
}