#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 {
/// A Ray from some origin to infinity
#if !NO_SERIALIZATION
[Serializable]
#endif
public class Ray2 : ILine2 {
/// Initializes a new ray
[System.Diagnostics.DebuggerStepThrough]
public Ray2() {
Origin = Vector2.Zero;
Direction = Vector2.UnitX;
}
/// Constructs a new line as copy of an existing instance
/// Existing instance to copy
[System.Diagnostics.DebuggerStepThrough]
public Ray2(Ray2 other) {
Origin = other.Origin;
Direction = other.Direction;
}
/// Initializes a new ray
/// Location from which the ray originates
/// Direction into which the ray goes
[System.Diagnostics.DebuggerStepThrough]
public Ray2(Vector2 origin, Vector2 direction) {
Origin = origin;
Direction = direction;
// Make sure the direction is normalized
Direction = Vector2.Normalize(Direction);
}
/// Determines the closest point on the ray to the specified location
/// Random loation to which the closest point is determined
/// The closest point within the ray
public Vector2 ClosestPointTo(Vector2 location) {
// Calculate the position of an orthogonal vector on the ray pointing
// towards the location the caller specified
float position = Vector2.Dot(location - Origin, Direction);
// Clip the position in the negative direction so it can't go before the ray's origin
return Origin + Direction * Math.Max(position, 0.0f);
}
/// Checks two ray 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 !=(Ray2 first, Ray2 second) {
return !(first == second);
}
/// Checks two ray 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 ==(Ray2 first, Ray2 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 Ray2);
}
/// 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(Ray2 other) {
if(other == null)
return false;
else
return (this.Origin == other.Origin) && (this.Direction == other.Direction);
}
/// Obtains a hash code of this instance
/// The hash code of the instance
public override int GetHashCode() {
unchecked { return Origin.GetHashCode() + Direction.GetHashCode(); }
}
/// Origin of the ray
public Vector2 Origin;
/// Normalized direction into which the ray goes
public Vector2 Direction;
}
} // namespace Nuclex.Geometry.Ranges