#region CPL License
/*
Nuclex Framework
Copyright (C) 2002-2010 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 System.Collections.Generic;
using Microsoft.Xna.Framework;
namespace Nuclex.UserInterface {
/// Stores a two-dimensional position or size
public struct UniVector {
/// A vector that has been initialized to zero
public static readonly UniVector Zero = new UniVector();
/// Initializes a new vector from the provided components
/// Absolute and relative X coordinate of the vector
/// Absolute and relative Y coordinate of the vector
public UniVector(UniScalar x, UniScalar y) {
this.X = x;
this.Y = y;
}
/// Converts the vector into pure offset coordinates
///
/// Dimensions of the container the relative part of the vector counts for
///
/// An XNA vector with the pure offset coordinates of the vector
public Vector2 ToOffset(Vector2 containerSize) {
return ToOffset(containerSize.X, containerSize.Y);
}
/// Converts the vector into pure offset coordinates
///
/// Width of the container the fractional part of the vector counts for
///
///
/// Height of the container the fractional part of the vector counts for
///
/// An XNA vector with the pure offset coordinates of the vector
public Vector2 ToOffset(float containerWidth, float containerHeight) {
return new Vector2(
this.X.Fraction * containerWidth + this.X.Offset,
this.Y.Fraction * containerHeight + this.Y.Offset
);
}
/// Adds one vector to another
/// Base vector to add to
/// Vector to add to the base
/// The result of the addition
public static UniVector operator +(UniVector vector, UniVector summand) {
return new UniVector(vector.X + summand.X, vector.Y + summand.Y);
}
/// Subtracts one vector from another
/// Base vector to subtract from
/// Vector to subtract from the base
/// The result of the subtraction
public static UniVector operator -(UniVector vector, UniVector subtrahend) {
return new UniVector(vector.X - subtrahend.X, vector.Y - subtrahend.Y);
}
/// Divides one vector by another
/// Base vector to be divided
/// Divisor to divide by
/// The result of the division
public static UniVector operator /(UniVector vector, UniVector divisor) {
return new UniVector(vector.X / divisor.X, vector.Y / divisor.Y);
}
/// Multiplies one vector with another
/// Base vector to be multiplied
/// Factor to multiply by
/// The result of the multiplication
public static UniVector operator *(UniVector vector, UniVector factor) {
return new UniVector(vector.X * factor.X, vector.Y * factor.Y);
}
/// Scales a vector by a scalar factor
/// Factor by which to scale the vector
/// Vector to be Scaled
/// The result of the multiplication
public static UniVector operator *(UniScalar factor, UniVector vector) {
return new UniVector(factor * vector.X, factor * vector.Y);
}
/// Scales a vector by a scalar factor
/// Vector to be Scaled
/// Factor by which to scale the vector
/// The result of the multiplication
public static UniVector operator *(UniVector vector, UniScalar factor) {
return new UniVector(vector.X * factor, vector.Y * factor);
}
/// Checks two vectors for inequality
/// First vector to be compared
/// Second vector to be compared
/// True if the instances differ or exactly one reference is set to null
public static bool operator !=(UniVector first, UniVector second) {
return !(first == second);
}
/// Checks two vectors for equality
/// First vector to be compared
/// Second vector to be compared
/// True if both instances are equal or both references are null
public static bool operator ==(UniVector first, UniVector second) {
// For a struct, neither 'first' nor 'second' can be 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) {
if (!(other is UniVector))
return false;
return Equals((UniVector)other);
}
/// 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 bool Equals(UniVector other) {
// For a struct, 'other' cannot be null
return (this.X == other.X) && (this.Y == other.Y);
}
/// Obtains a hash code of this instance
/// The hash code of the instance
public override int GetHashCode() {
return this.X.GetHashCode() ^ this.Y.GetHashCode();
}
///
/// Returns a human-readable string representation for the unified vector
///
/// The human-readable string representation of the unified vector
public override string ToString() {
return string.Format(
"{{X:{0}, Y:{1}}}",
this.X.ToString(),
this.Y.ToString()
);
}
/// The vector's X coordinate
public UniScalar X;
/// The vector's Y coordinate
public UniScalar Y;
}
} // namespace Nuclex.UserInterface