/*-
 * Copyright (c) 1999,2000 Qube Software, Ltd.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of the source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 
 * 2. Any redistribution solely in binary form must conspicuously
 *    reproduce the following disclaimer in documentation provided with the
 *    binary redistribution.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'', WITHOUT ANY WARRANTIES, EXPRESS
 * OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  LICENSOR SHALL
 * NOT BE LIABLE FOR ANY LOSS OR DAMAGES RESULTING FROM THE USE OF THIS
 * SOFTWARE, EITHER ALONE OR IN COMBINATION WITH ANY OTHER SOFTWARE.
 * 
 * 	$Id: vec2.h,v 1.21 2000/04/20 16:21:58 n_hibma Exp $
 */

#ifndef __vec2_h__
#define __vec2_h__

MATHS_BEGIN

template<class T> struct Vec2;
template<class T> struct Vec3;
template<class T> struct Vec4;

template<class T>
struct EXPORT_QMATHS Vec2
{
    T			x;
    T			y;

    // Constructors
			Vec2() {}
			Vec2(const Vec2& v)
			    : x(v.x), y(v.y) {}
			Vec2(T tx, T ty)
			    : x(tx), y(ty) {}
			Vec2(T val)
			    : x(val), y(val) {}
			Vec2(const Vec3<T>& v)
			    : x(v.x), y(v.y) {}
			Vec2(const Vec4<T>& v)
			    : x(v.x / v.w), y(v.y / v.w) {}
    
    // Accessors
    T&			operator[](unsigned int i) { return (&x)[i]; }
    const T&		operator[](unsigned int i) const { return (&x)[i]; }

    // Testing
    bool		equals(const Vec2& v, float e = Epsilon) const
    {
	return Maths::equals(x, v.x, e) && Maths::equals(y, v.y, e);
    }

    // Modifying
    void                normalize();

    // Geometric operations
    T		  modulus();
};

// Modifying
template<class T> void
Vec2<T>::normalize()
{
    float inv_mod = invSqrtf((float)((x * x) + (y * y)));

    x = (T)(x * inv_mod);
    y = (T)(y * inv_mod);
}

// Binary operations
template<class T> inline Vec2<T>
operator+(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return Vec2<T>(v1.x + v2.x, v1.y + v2.y);
}

template<class T> inline Vec2<T>
operator-(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return Vec2<T>(v1.x - v2.x, v1.y - v2.y);
}

template<class T> inline Vec2<T>
operator*(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return Vec2<T>(v1.x * v2.x, v1.y * v2.y);
}

template<class T> inline Vec2<T>
operator*(const Vec2<T>& v, T s)
{
    return Vec2<T>(v.x * s, v.y * s);
}

template<class T> inline Vec2<T>
operator*(T s, const Vec2<T>& v)
{
    return Vec2<T>(v.x * s, v.y * s);
}

template<class T> inline Vec2<T>
operator/(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return Vec2<T>(v1.x / v2.x, v1.y / v2.y);
}

template<class T> inline Vec2<T>
operator/(const Vec2<T>& v, T s)
{
    return Vec2<T>(v.x / s, v.y / s);
}

template<class T> inline Vec2<T>
operator/(T s, const Vec2<T>& v)
{
    return Vec2<T>(s / v.x, s / v.y);
}

template<class T> inline Vec2<T>&
operator+=(Vec2<T>& a, const Vec2<T>& s)
{
    a.x += s.x;
    a.y += s.y;
    return a;
}

template<class T> inline Vec2<T>&
operator-=(Vec2<T>& a, const Vec2<T>& s)
{
    a.x -= s.x;
    a.y -= s.y;
    return a;
}

template<class T> inline Vec2<T>&
operator*=(Vec2<T>& a, const Vec2<T> &s)
{
    a.x *= s.x;
    a.y *= s.y;
    return a;
}

template<class T> inline Vec2<T>&
operator*=(Vec2<T>& a, T s)
{
    a.x *= s;
    a.y *= s;
    return a;
}

// Testing

template<class T> inline bool
operator==(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return v1.x == v2.x && v1.y == v2.y;
}

template<class T> inline bool
operator!=(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return !(v1 == v2);
}

// Geometric operations

template<class T> inline T
cross(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return v1.x * v2.y - v1.y * v2.x;
}

template<class T> inline T
dot(const Vec2<T>& v1, const Vec2<T>& v2)
{
    return v1.x * v2.x + v1.y * v2.y;
}

template<class T> inline Vec2<T>
interpolate(T mul, const Vec2<T>& v1, const Vec2<T>& v2)
{
    return v1 + ((v2 - v1) * mul);
}

template<class T> T
Vec2<T>::modulus()
{
    return sqrtf((x * x) + (y * y));
}

// Instantiate Vector types
typedef Vec2<float>		Vec2f;
typedef Vec2<double>		Vec2d;
typedef Vec2<short>		Vec2s;
typedef Vec2<int>		Vec2i;
typedef Vec2<unsigned char>	Vec2b;
typedef Vec2<unsigned int>	Vec2ui;

MATHS_END

#endif

// Local Variables:
// mode: c++
// End:
