Circle2D.java

  1. package org.drip.zen.geometry;

  2. import org.drip.zen.equation.Quadratic;

  3. /*
  4.  * Circle2D
  5.  *
  6.  * 1) Instance Fields - center and radius
  7.  * 2) Get the fields
  8.  * 3) Calculate Area
  9.  * 4) Calculate Perimeter
  10.  * 5) Closest Distance to a Point
  11.  * 6) Do they Overlap
  12.  * 7) Do they Touch
  13.  * 8) Distance between a Line and the Circle
  14.  * 9) Does Line intersect the Circle
  15.  * 10) Point inside Circle
  16.  * 11) Intersection Between Circle and Line
  17.  * 12) Concept of Intersection - satisfy equations of both line AND circle
  18.  * 13) Derive the Quadratic Equations
  19.  * 14) Formulate them and encode them
  20.  * 15) Solve for them
  21.  * 16) Translate Circles - 20 Match 2016 #1
  22.  * 17) Angle Extended at Origin - 20 March 2016 #7
  23.  * 17) Tangents to Circle - Formulation + Conception - 20 March 2016 #8
  24.  * 18) Tangential Points - Implementation - 20 March 2016 #9
  25.  * 19) Bring it together - 20 March 2016 #10
  26.  */

  27. public class Circle2D {
  28.     private Point2D _center;
  29.     private double _radius;

  30.     public Circle2D (Point2D center, double radius)
  31.     {
  32.         _center = center;
  33.         _radius = radius;
  34.     }

  35.     public Point2D getCenter()
  36.     {
  37.         return _center;
  38.     }

  39.     public double getRadius()
  40.     {
  41.         return _radius;
  42.     }

  43.     public double area()
  44.     {
  45.         double circleArea = Math.PI * _radius * _radius;
  46.         return circleArea;
  47.     }

  48.     public double perimeter()
  49.     {
  50.         double circumference = 2. * Math.PI * _radius;
  51.         return circumference;
  52.     }

  53.     public boolean overlap (Circle2D anotherCircle)
  54.     {
  55.         double centerX1 = _center.getX();
  56.         double centerY1 = _center.getY();
  57.         double centerX2 = anotherCircle.getCenter().getX();
  58.         double centerY2 = anotherCircle.getCenter().getY();
  59.         double radius1 = getRadius();
  60.         double radius2 = anotherCircle.getRadius();

  61.         double distanceBetweenCenters = Math.sqrt ((centerX1 - centerX2) * (centerX1 - centerX2) + (centerY1 - centerY2) * (centerY1 - centerY2));

  62.         boolean circlesIntersect = false;

  63.         if (distanceBetweenCenters < radius1 + radius2)
  64.             circlesIntersect = true;
  65.         else
  66.             circlesIntersect = false;

  67.         return circlesIntersect;
  68.     }

  69.     public boolean touch (Circle2D anotherCircle)
  70.     {
  71.         double centerX1 = _center.getX();
  72.         double centerY1 = _center.getY();
  73.         double centerX2 = anotherCircle.getCenter().getX();
  74.         double centerY2 = anotherCircle.getCenter().getY();
  75.         double radius1 = getRadius();
  76.         double radius2 = anotherCircle.getRadius();

  77.         double distanceBetweenCenters = Math.sqrt ((centerX1 - centerX2) * (centerX1 - centerX2) + (centerY1 - centerY2) * (centerY1 - centerY2));

  78.         boolean circlesTouch = false;

  79.         if (distanceBetweenCenters == radius1 + radius2)
  80.             circlesTouch = true;
  81.         else
  82.             circlesTouch = false;

  83.         return circlesTouch;
  84.     }

  85.     public double distanceToLine (Line2D line)
  86.     {
  87.         Line2D perpendicularLine = line.perpendicularLineAtPoint (getCenter());

  88.         Point2D intersectingPoint = line.intersection (perpendicularLine);

  89.         double distance = intersectingPoint.distanceBetweenPoints (getCenter());

  90.         return distance;
  91.     }

  92.     public boolean linePassesThrough (Line2D line)
  93.     {
  94.         double distanceBetweenLineAndCenter = distanceToLine (line);

  95.         boolean linePassesThroughTheCircle = false;

  96.         if (distanceBetweenLineAndCenter < getRadius())
  97.             linePassesThroughTheCircle = true;
  98.         else
  99.             linePassesThroughTheCircle = false;

  100.         return linePassesThroughTheCircle;
  101.     }

  102.     public boolean inside (Point2D p)
  103.     {
  104.         boolean inside = false;

  105.         double distanceFromCenter = p.distanceBetweenPoints (getCenter());

  106.         if (distanceFromCenter < getRadius())
  107.             inside = true;
  108.         else
  109.             inside = false;

  110.         return inside;
  111.     }

  112.     public Point2D[] intersections (Line2D l)
  113.     {
  114.         double x1 = _center.getX();
  115.         double y1 = _center.getY();
  116.         double r = _radius;

  117.         double m = l.slope();
  118.         double b = l.yIntercept();

  119.         double quadraticA = 1 + m * m;
  120.         double quadraticB = -2 * x1 + 2 * m * b - 2 * m * y1;
  121.         double quadraticC = x1 * x1 + b * b - 2 * b * y1 + y1 * y1 - r * r;

  122.         Quadratic quad = new Quadratic (quadraticA, quadraticB, quadraticC);

  123.         double[] intersectionX = quad.findRoots();

  124.         if (null == intersectionX) return null;

  125.         Point2D[] intersectingPoints = new Point2D[2];
  126.         intersectingPoints[0] = new Point2D (intersectionX[0], l.YFromX (intersectionX[0]));
  127.         intersectingPoints[1] = new Point2D (intersectionX[1], l.YFromX (intersectionX[1]));
  128.         return intersectingPoints;
  129.     }

  130.     /*
  131.      * Added 20 March 2016
  132.      */

  133.     public boolean translate (double xAmount, double yAmount)
  134.     {
  135.         return _center.translate (xAmount, yAmount);
  136.     }

  137.     public double angleAtOrigin()
  138.     {
  139.         double distanceFromCenterToOrigin = _center.distanceFromOrigin();

  140.         double originAngle = Math.asin (_radius / distanceFromCenterToOrigin);

  141.         return originAngle;
  142.     }

  143.     public Point2D[] tangentsFromOrigin()
  144.     {
  145.         double centerAngleAtOrigin = _center.angleAtOrigin() * Math.PI / 180.;

  146.         double circleAngleAtOrigin = angleAtOrigin();

  147.         double tangentDistanceFromOrigin = _radius / Math.sin (centerAngleAtOrigin);

  148.         double angleAbove = centerAngleAtOrigin + circleAngleAtOrigin;
  149.         double angleBelow = centerAngleAtOrigin - circleAngleAtOrigin;

  150.         Point2D[] tangentPoints = new Point2D[2];

  151.         tangentPoints[0] = Point2D.MakeFromPolar (tangentDistanceFromOrigin, angleAbove);

  152.         tangentPoints[1] = Point2D.MakeFromPolar (tangentDistanceFromOrigin, angleBelow);

  153.         return tangentPoints;
  154.     }

  155.     /*
  156.      * Done Adding 20 March 2016
  157.      */

  158.     public static void main (String[] args)
  159.     {
  160.         Point2D myCenter = new Point2D (0, 0);
  161.         double myRadius = 1;
  162.         Circle2D myCircle = new Circle2D (myCenter, myRadius);

  163.         Line2D myLine = new Line2D (1, 0);

  164.         Point2D[] circleLineIntersections = myCircle.intersections (myLine);

  165.         circleLineIntersections[0].printCoordinate();

  166.         circleLineIntersections[1].printCoordinate();

  167.         /*
  168.          * Added 20 March 2016
  169.          */

  170.         Point2D anotherCenter = new Point2D (5, 5);
  171.         double anotherRadius = 1;
  172.         Circle2D anotherCircle = new Circle2D (anotherCenter, anotherRadius);

  173.         Point2D[] tangentialPoints = anotherCircle.tangentsFromOrigin();

  174.         System.out.println();

  175.         System.out.println ("\tCenter Angle At Origin: " + anotherCircle._center.angleAtOrigin());

  176.         System.out.println ("\tCircle Angle At Origin: " + anotherCircle.angleAtOrigin());

  177.         System.out.println();

  178.         tangentialPoints[0].printCoordinate();

  179.         tangentialPoints[1].printCoordinate();

  180.         System.out.println();

  181.         tangentialPoints[0].printPolarCoordinate();

  182.         tangentialPoints[1].printPolarCoordinate();

  183.         /*
  184.          * Done Adding 20 March 2016
  185.          */
  186.     }
  187. }