/*
	Project: Balls2D
	Author: Simon Gayton
	Copyright 2007 (C) Simon Gayton
*/

/*
    This class represents a simple 2D vector.
*/
public class Vector2D {
  float x, y;

  /*
    Default constructor.
  */
  Vector2D() {
    x = 0.0f;
    y = 0.0f;
  }
  
  /*
    Copy constructor.
  */
  Vector2D(final Vector2D a) {
    x = a.x;
    y = a.y;
  }

  /*
    Construct from two reals.
  */
  Vector2D(float x, float y) {
    this.x = x;
    this.y = y;
  }

  /*
    Set to the zero vector.
  */
  void zero() {
    x = 0.0f;
    y = 0.0f;
  }

  /*
    Calculate the magnitude of the vector.
  */
  float length() {
    return (float)(Math.sqrt(x*x + y*y));
  }

  /*
    Turn the vector into a unit vector.
  */
  void normalize() {
    float mag = this.length();

    x /= mag;
    y /= mag;
  }

  /*
    Print out the vector data, for debuging.
  */
  void print() {
    System.out.println( "(" + x + ", " + y + ")" );
  }

  /*
    Add a vector to this one.
  */
  void add(Vector2D a) {
    x += a.x;
    y += a.y;
  }
  
  /*
    Set this vector to the difference of to vectors, position vectors.
  */
  void diff(Vector2D a, Vector2D b) {
    x = a.x - b.x;
    y = a.y - b.y;
  }

  /*
    Add a scaled copy of a vector to this one.
  */
  void addScaled(float scale, final Vector2D a) {
    x += scale * a.x;
    y += scale * a.y;
  }
  
  /*
    Calculate the dot product between two vectors.
  */
  static float dot(final Vector2D a, final Vector2D b) {
    return a.x * b.x + a.y * b.y;
  }
}
