Point2D.java

  1. package org.drip.zen.geometry;

  2. /*
  3.  * 1) Motivation behind Class - Structure and Function
  4.  * 2) x/y fields and printing them
  5.  * 3) Invocation of the Class - Creation of the Class Instance
  6.  * 4) Adding a method - set to Origin
  7.  * 5) Add the Constructor with fields
  8.  * 6) Member Fields with an underscore
  9.  * 7) Distance from Origin
  10.  * 8) Angle with X axis at Origin
  11.  * 9) Degrees to Radians Conversion
  12.  * 10) Quadrant
  13.  * 11) Degrees to Radian with the Qudrant Assignment
  14.  * 12) Distance From Another Point
  15.  * 13) Translate the Points - 20 March 2016 #2
  16.  * 14) Polar Coordinate Representation - 20 March 2016 #3
  17.  * 15) Sketch the Problem Solution - 20 March 2016 #4
  18.  * 16) Point from Polar Coordinates - 20 March 2016 #5
  19.  * 17) Introducing the static Constructor - 20 March 2016 #6
  20.  */

  21. public class Point2D {
  22.     private double _x = -1.;
  23.     private double _y = -1.;

  24.     /*
  25.      * Added 20 March 2016
  26.      */

  27.     public static Point2D MakeFromPolar (double r, double angle)
  28.     {
  29.         double x = r * Math.cos (angle);

  30.         double y = r * Math.sin (angle);

  31.         Point2D p = new Point2D (x, y);

  32.         return p;
  33.     }

  34.     /*
  35.      * Done Adding 20 March 2016
  36.      */

  37.     Point2D (double x, double y)
  38.     {
  39.         _x = x;
  40.         _y = y;
  41.     }

  42.     public double getX()
  43.     {
  44.         return _x;
  45.     }

  46.     public double getY()
  47.     {
  48.         return _y;
  49.     }

  50.     void printCoordinate()
  51.     {
  52.         System.out.println ("\t[" + _x + ", " + _y + "]");
  53.     }

  54.     void resetToOrigin()
  55.     {
  56.         _x = 0.;
  57.         _y = 0.;
  58.     }

  59.     double distanceFromOrigin()
  60.     {
  61.         double distance = Math.sqrt (_x * _x + _y * _y);

  62.         return distance;
  63.     }

  64.     double distanceBetweenPoints (Point2D anotherPoint)
  65.     {
  66.         double x1 = _x;
  67.         double y1 = _y;
  68.         double x2 = anotherPoint._x;
  69.         double y2 = anotherPoint._y;

  70.         double distance = Math.sqrt ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));

  71.         return distance;
  72.     }

  73.     public double angleAtOrigin()
  74.     {
  75.         double adjustedAngleAtOrigin = 0.;
  76.         double angleAtOriginTan = _y / _x;

  77.         double unadjustedAngleAtOrigin = Math.atan (angleAtOriginTan);

  78.         if (_x > 0 && _y > 0)
  79.             adjustedAngleAtOrigin = unadjustedAngleAtOrigin;
  80.         else if (_x < 0 && _y > 0)
  81.             adjustedAngleAtOrigin = unadjustedAngleAtOrigin + Math.PI;
  82.         else if (_x < 0 && _y < 0)
  83.             adjustedAngleAtOrigin = unadjustedAngleAtOrigin + Math.PI;
  84.         else if (_x > 0 && _y < 0)
  85.             adjustedAngleAtOrigin = unadjustedAngleAtOrigin + 2. * Math.PI;
  86.         else if (_x > 0 && _y < 0)
  87.             adjustedAngleAtOrigin = 0.;

  88.         return adjustedAngleAtOrigin * 180. / Math.PI;
  89.     }

  90.     String quadrant()
  91.     {
  92.         String region;

  93.         if (_x > 0 && _y > 0)
  94.             region = "UPPER RIGHT";
  95.         else if (_x < 0 && _y > 0)
  96.             region = "UPPER LEFT";
  97.         else if (_x < 0 && _y < 0)
  98.             region = "LOWER LEFT";
  99.         else if (_x > 0 && _y < 0)
  100.             region = "LOWER RIGHT";
  101.         else
  102.             region = "ORIGIN";

  103.         return region;
  104.     }

  105.     /*
  106.      * Added 20 March 2016
  107.      */

  108.     public boolean translate (double xShift, double yShift)
  109.     {
  110.         _x = _x + xShift;
  111.         _y = _y + yShift;
  112.         return true;
  113.     }

  114.     void printPolarCoordinate()
  115.     {
  116.         System.out.println ("\t[" + distanceFromOrigin() + ", " + angleAtOrigin() + "]");
  117.     }

  118.     /*
  119.      * Done Adding On 20 March 2016
  120.      */

  121.     public static void main (String[] args)
  122.     {
  123.         Point2D myPoint = new Point2D (2., 3.);

  124.         myPoint.printCoordinate();

  125.         myPoint._x = 1;
  126.         myPoint._y = 1;

  127.         myPoint.printCoordinate();

  128.         myPoint.resetToOrigin();

  129.         myPoint.printCoordinate();

  130.         System.out.println ("\tDistance From Origin: " + myPoint.distanceFromOrigin());

  131.         Point2D secondPoint = new Point2D (3., 4.);

  132.         System.out.println ("\tDistance Between Points: " + myPoint.distanceBetweenPoints (secondPoint));

  133.         myPoint._x = 2;
  134.         myPoint._y = -2;

  135.         System.out.println ("\tAngle at Origin: " + myPoint.angleAtOrigin());

  136.         System.out.println ("\tQuadrant: " + myPoint.quadrant());
  137.     }
  138. }