/*********************************************************\
         __      __           __  __
        / /     / / _____    / / / / ______   __     __
       / /     / / /___  \  / / / / / ___  | / /    / /
      / /_____/ / _____| / / / / / / /__/ / / /    / /
     / ______  / / ___  / / / / / / _____/ / /    / /
    / /     / / / /__/ / / / / / / |____  / |____/ /
   /_/     /_/ |______/ /_/ /_/  \_____/  \_____  /
                                         ______/ /
            High Level Game Framework   /_______/

  ------------------------------------------------------

  Copyright (c) 2009 - Rodrigo Braz Monteiro.
  This file is subject to the terms of license.txt.

\*********************************************************/

#include "base_transform.h"
#include "exception.h"
using namespace Halley;

BaseTransform::BaseTransform(Vector2f _u, Vector2f _v)
: u(_u), v(_v)
{
	if (u.Cross(v) == 0) throw Exception("Invalid base.");
}


Vector2f Halley::BaseTransform::toBase(Vector2f point) const
{
	return point.x * u + point.y * v;
}

Vector2f Halley::BaseTransform::fromBase(Vector2f point) const
{
	float a = u.x;
	float b = v.x;
	float c = u.y;
	float d = v.y;
	float px = point.x;
	float py = point.y;

	float div = a*d-b*c;	// Note: this can't be zero unless u and v are collinear, which is an error
	float x = (px*d - py*b)/div;
	float y = (py*a - px*c)/div;

	return Vector2f(x, y);
}
