#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2009 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#endregion
using System;
using Microsoft.Xna.Framework;
namespace Nuclex.Geometry.Lines {
/// Line (extending to infinity on both directions)
#if !NO_SERIALIZATION
[Serializable]
#endif
public class Line2 : ILine2 {
/// Constructs a new line
[System.Diagnostics.DebuggerStepThrough]
public Line2() : this(Vector2.Zero, Vector2.Zero) { }
/// Constructs a new line as copy of an existing instance
/// Existing instance to copy
[System.Diagnostics.DebuggerStepThrough]
public Line2(Line2 other) : this(other.Offset, other.Direction) { }
/// Initializes a new line
/// Offset of the line from the coordinate system's center
/// Vector the defines the direction of the line
[System.Diagnostics.DebuggerStepThrough]
public Line2(Vector2 offset, Vector2 direction) {
this.Offset = offset;
this.Direction = direction;
}
/// Locates the nearest point on the line to some arbitrary location
/// Location to which the closest point is determined
/// The closest point on the line to the specified location
public Vector2 ClosestPointTo(Vector2 location) {
Vector2 distance = location - this.Offset;
float ratio = Vector2.Dot(this.Direction, distance) / this.Direction.LengthSquared();
return this.Offset + this.Direction * ratio;
}
/// Checks two line instances for inequality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if the instances differ or exactly one reference is set to null
public static bool operator !=(Line2 first, Line2 second) {
return !(first == second);
}
/// Checks two line instances for equality
/// First instance to be compared
/// Second instance fo tbe compared
/// True if both instances are equal or both references are null
public static bool operator ==(Line2 first, Line2 second) {
if(ReferenceEquals(first, null))
return ReferenceEquals(second, null);
return first.Equals(second);
}
/// Checks whether another instance is equal to this instance
/// Other instance to compare to this instance
/// True if the other instance is equal to this instance
public override bool Equals(object other) {
return Equals(other as Line2);
}
/// Checks whether another instance is equal to this instance
/// Other instance to compare to this instance
/// True if the other instance is equal to this instance
public virtual bool Equals(Line2 other) {
if(other == null)
return false;
else
return (this.Offset == other.Offset) && (this.Direction == other.Direction);
}
/// Obtains a hash code of this instance
/// The hash code of the instance
public override int GetHashCode() {
unchecked { return Offset.GetHashCode() + Direction.GetHashCode(); }
}
/// Offset of the line from the coordinate system's center
public Vector2 Offset;
/// Direction into which the line extends
public Vector2 Direction;
}
} // namespace Nuclex.Geometry.Ranges