/**
 * The LineVector class represents a vector as the front and back of a line segment.
 * 
 * @author Fred Kral
 * @version 1.0
 *
 */

public class LineVector {
   /** x of front of line */
   private int x = 0;
   /** y of front of line */
   private int y = 0;
   /** x2 of back of line */
   private int x2 = 0;
   /** y2 of back of line */
   private int y2 = 0;
   
   /** 
   * The constructor makes a line with front (x, y) and back (x2, y2) 
   * @param x, y, x2, y2
   */
   LineVector(int x, int y, int x2, int y2){
      this.x = x;
      this.y = y;
      this.x2 = x2;
      this.y2 = y2;
   }
   
   /** Two LineVector objects are compared and set equal if head positions coincide
   @param c another object
   @return true for equal, false for not equal
   */
   public boolean equalsFront(LineVector c){
      return c.x() == x && c.y() == y;
   }
   /** Is the line on the grid (board or panel) defined from origin to maxima (-1) */
   public boolean onGrid(int maxX, int maxY){
      return x >= 0 && y >=0 && x < maxX && y < maxY;
   }
   /** 
   * Works like {@link #move(int, int, int, int)}, but does not need x and y.
   */
   public void move(int dx, int dy){
      move(this.x, this.y, dx, dy);
   }
   /**
   * Move the line by changing x and y (and x2 and y2), by increments (speed)
   * @param x unused
   * @param y unused 
   * @param dx increment in x (speed in x)
   * @param dy increment in y (speed in y)
   */
   public void move(int x, int y, int dx, int dy){
      x2 = this.x;
      y2 = this.y;
      this.x += dx;
      this.y += dy;  
   }
   /** Turn right or left
   * @param rightTurn true is turn right, false is turn left
   * @param speedX is how far to move the front of the line in x (East/West)
   * @param speedY ditto for y (North/South)
   */
   public void turn(boolean rightTurn, int speedX, int speedY){
      int dx = 0;
      int dy = 0;
      
      // East-West
      if (x > x2) {// facing East (right)
         if (rightTurn) dy = 1;
         else dy = -1;
      }
      else if (x < x2) {// facing West
         if (rightTurn) dy = -1;
         else dy = 1;
      }
      else {
         // do nothing, pointing up or down
      }
      
      
      // North-South
      if (y > y2) {// facing South (down)
         if (rightTurn) dx = -1;
         else dx = 1;
      }
      else if (y < y2) {// facing North
         if (rightTurn) dx = 1;
         else dx = -1;
      }
      else {
         // do nothing, pointing left or right
      }
      
      
      move(dx*speedX, dy*speedY);// perform the move by one block
      
   }// END method turn
   
   /** get x, of front */
   public int x(){
      return x;}
   /** get y, of front */
   public int y(){
      return y;}   
   /** get x2, of back */
   public int x2(){
      return x2;}
   /** get y2, of back */
   public int y2(){
      return y2;}
      
   public String toString(){
      String s = "LineVector " + x() + " " + y() + " " + x2() + " " + y2();
      
      return s;
   }
   
}// END class