Advertisement
RovkirHexus

Location

Jun 8th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.15 KB | None | 0 0
  1. package feAttempt;
  2.  
  3. /*
  4.  * AP(r) Computer Science GridWorld Case Study:
  5.  * Copyright(c) 2002-2006 College Entrance Examination Board
  6.  * (http://www.collegeboard.com).
  7.  *
  8.  * This code is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation.
  11.  *
  12.  * This code is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * @author Alyce Brady
  18.  * @author Chris Nevison
  19.  * @author APCS Development Committee
  20.  * @author Cay Horstmann
  21.  */
  22.  
  23. /**
  24.  * A <code>Location</code> object represents the row and column of a location
  25.  * in a two-dimensional grid. <br />
  26.  * The API of this class is testable on the AP CS A and AB exams.
  27.  */
  28. public class Location implements Comparable
  29. {
  30.   private int row; // row location in grid
  31.   private int col; // column location in grid
  32.  
  33.   /**
  34.    * The turn angle for turning 90 degrees to the left.
  35.    */
  36.   public static final int LEFT = -90;
  37.   /**
  38.    * The turn angle for turning 90 degrees to the right.
  39.    */
  40.   public static final int RIGHT = 90;
  41.   /**
  42.    * The turn angle for turning 45 degrees to the left.
  43.    */
  44.   public static final int HALF_LEFT = -45;
  45.   /**
  46.    * The turn angle for turning 45 degrees to the right.
  47.    */
  48.   public static final int HALF_RIGHT = 45;
  49.   /**
  50.    * The turn angle for turning a full circle.
  51.    */
  52.   public static final int FULL_CIRCLE = 360;
  53.   /**
  54.    * The turn angle for turning a half circle.
  55.    */
  56.   public static final int HALF_CIRCLE = 180;
  57.   /**
  58.    * The turn angle for making no turn.
  59.    */
  60.   public static final int AHEAD = 0;
  61.  
  62.   /**
  63.    * The compass direction for north.
  64.    */
  65.   public static final int NORTH = 0;
  66.   /**
  67.    * The compass direction for northeast.
  68.    */
  69.   public static final int NORTHEAST = 45;
  70.   /**
  71.    * The compass direction for east.
  72.    */
  73.   public static final int EAST = 90;
  74.   /**
  75.    * The compass direction for southeast.
  76.    */
  77.   public static final int SOUTHEAST = 135;
  78.   /**
  79.    * The compass direction for south.
  80.    */
  81.   public static final int SOUTH = 180;
  82.   /**
  83.    * The compass direction for southwest.
  84.    */
  85.   public static final int SOUTHWEST = 225;
  86.   /**
  87.    * The compass direction for west.
  88.    */
  89.   public static final int WEST = 270;
  90.   /**
  91.    * The compass direction for northwest.
  92.    */
  93.   public static final int NORTHWEST = 315;
  94.  
  95.   /**
  96.    * Constructs a location with given row and column coordinates.
  97.    * @param r the row
  98.    * @param c the column
  99.    */
  100.   public Location(int r, int c)
  101.   {
  102.     row = r;
  103.     col = c;
  104.   }
  105.  
  106.   /**
  107.    * Gets the row coordinate.
  108.    * @return the row of this location
  109.    */
  110.   public int getRow()
  111.   {
  112.     return row;
  113.   }
  114.  
  115.   /**
  116.    * Gets the column coordinate.
  117.    * @return the column of this location
  118.    */
  119.   public int getCol()
  120.   {
  121.     return col;
  122.   }
  123.  
  124.   /**
  125.    * Gets the adjacent location in any one of the eight compass directions.
  126.    * @param direction the direction in which to find a neighbor location
  127.    * @return the adjacent location in the direction that is closest to
  128.    * <tt>direction</tt>
  129.    */
  130.   public Location getAdjacentLocation(int direction)
  131.   {
  132.     // reduce mod 360 and round to closest multiple of 45
  133.     int adjustedDirection = (direction + HALF_RIGHT / 2) % FULL_CIRCLE;
  134.     if (adjustedDirection < 0)
  135.       adjustedDirection += FULL_CIRCLE;
  136.    
  137.     adjustedDirection = (adjustedDirection / HALF_RIGHT) * HALF_RIGHT;
  138.     int dc = 0;
  139.     int dr = 0;
  140.     if (adjustedDirection == EAST)
  141.       dc = 1;
  142.     else if (adjustedDirection == SOUTHEAST)
  143.     {
  144.       dc = 1;
  145.       dr = 1;
  146.     }
  147.     else if (adjustedDirection == SOUTH)
  148.       dr = 1;
  149.     else if (adjustedDirection == SOUTHWEST)
  150.     {
  151.       dc = -1;
  152.       dr = 1;
  153.     }
  154.     else if (adjustedDirection == WEST)
  155.       dc = -1;
  156.     else if (adjustedDirection == NORTHWEST)
  157.     {
  158.       dc = -1;
  159.       dr = -1;
  160.     }
  161.     else if (adjustedDirection == NORTH)
  162.       dr = -1;
  163.     else if (adjustedDirection == NORTHEAST)
  164.     {
  165.       dc = 1;
  166.       dr = -1;
  167.     }
  168.     return new Location(getRow() + dr, getCol() + dc);
  169.   }
  170.  
  171.   /**
  172.    * Returns the direction from this location toward another location. The
  173.    * direction is rounded to the nearest compass direction.
  174.    * @param target a location that is different from this location
  175.    * @return the closest compass direction from this location toward
  176.    * <code>target</code>
  177.    */
  178.   public int getDirectionToward(Location target)
  179.   {
  180.     int dx = target.getCol() - getCol();
  181.     int dy = target.getRow() - getRow();
  182.     // y axis points opposite to mathematical orientation
  183.     int angle = (int) Math.toDegrees(Math.atan2(-dy, dx));
  184.    
  185.     // mathematical angle is counterclockwise from x-axis,
  186.     // compass angle is clockwise from y-axis
  187.     int compassAngle = RIGHT - angle;
  188.     // prepare for truncating division by 45 degrees
  189.     compassAngle += HALF_RIGHT / 2;
  190.     // wrap negative angles
  191.     if (compassAngle < 0)
  192.       compassAngle += FULL_CIRCLE;
  193.     // round to nearest multiple of 45
  194.     return (compassAngle / HALF_RIGHT) * HALF_RIGHT;
  195.   }
  196.  
  197.   /**
  198.    * Indicates whether some other <code>Location</code> object is "equal to"
  199.    * this one.
  200.    * @param other the other location to test
  201.    * @return <code>true</code> if <code>other</code> is a
  202.    * <code>Location</code> with the same row and column as this location;
  203.    * <code>false</code> otherwise
  204.    */
  205.   public boolean equals(Object other)
  206.   {
  207.     if (!(other instanceof Location))
  208.       return false;
  209.    
  210.     Location otherLoc = (Location) other;
  211.     return getRow() == otherLoc.getRow() && getCol() == otherLoc.getCol();
  212.   }
  213.  
  214.   /**
  215.    * Generates a hash code.
  216.    * @return a hash code for this location
  217.    */
  218.   public int hashCode()
  219.   {
  220.     return getRow() * 3737 + getCol();
  221.   }
  222.  
  223.   /**
  224.    * Compares this location to <code>other</code> for ordering. Returns a
  225.    * negative integer, zero, or a positive integer as this location is less
  226.    * than, equal to, or greater than <code>other</code>. Locations are
  227.    * ordered in row-major order. <br />
  228.    * (Precondition: <code>other</code> is a <code>Location</code> object.)
  229.    * @param other the other location to test
  230.    * @return a negative integer if this location is less than
  231.    * <code>other</code>, zero if the two locations are equal, or a positive
  232.    * integer if this location is greater than <code>other</code>
  233.    */
  234.   public int compareTo(Object other)
  235.   {
  236.     Location otherLoc = (Location) other;
  237.     if (getRow() < otherLoc.getRow())
  238.       return -1;
  239.     if (getRow() > otherLoc.getRow())
  240.       return 1;
  241.     if (getCol() < otherLoc.getCol())
  242.       return -1;
  243.     if (getCol() > otherLoc.getCol())
  244.       return 1;
  245.     return 0;
  246.   }
  247.  
  248.   /**
  249.    * Creates a string that describes this location.
  250.    * @return a string with the row and column of this location, in the format
  251.    * (row, col)
  252.    */
  253.   public String toString()
  254.   {
  255.     return "(" + getRow() + ", " + getCol() + ")";
  256.   }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement