Line2D.java

  1. package org.drip.zen.geometry;

  2. /*
  3.  * 1) Slope Intercept Form
  4.  * 2) Construction from slope and intercept
  5.  * 3) Construction from 2 Points
  6.  * 4) Construction From Slope and Point
  7.  * 5) Accessors
  8.  * 6) Y from X
  9.  * 7) X From Y
  10.  * 8) Parallel Line
  11.  * 9) Perpendicular Line
  12.  * 10) Intersection Between Lines
  13.  */

  14. public class Line2D {
  15.     private double _m = 0.;
  16.     private double _b = 0.;

  17.     public Line2D (double m, double b)
  18.     {
  19.         _m = m;
  20.         _b = b;
  21.     }

  22.     public Line2D (Point2D p1, Point2D p2)
  23.     {
  24.         double x1 = p1.getX();
  25.         double x2 = p2.getX();
  26.         double y1 = p1.getY();
  27.         double y2 = p2.getY();

  28.         _m = (y2 - y1) / (x2 - x1);

  29.         _b = y2 - _m * x2;
  30.     }

  31.     public Line2D (double m, Point2D p2)
  32.     {
  33.         double x2 = p2.getX();
  34.         double y2 = p2.getY();

  35.         _m = m;
  36.         _b = y2 - _m * x2;
  37.     }

  38.     public double slope()
  39.     {
  40.         return _m;
  41.     }

  42.     public double yIntercept()
  43.     {
  44.         return _b;
  45.     }

  46.     public void show()
  47.     {
  48.         System.out.println ("Slope: " + _m + "; Intercept: " + _b);
  49.     }

  50.     public double YFromX (double x)
  51.     {
  52.         double y = _m * x + _b;
  53.         return y;
  54.     }

  55.     public double XFromY (double y)
  56.     {
  57.         double x = (y - _b) / _m;
  58.         return x;
  59.     }

  60.     public Line2D parallelLineAtPoint (Point2D p)
  61.     {
  62.         Line2D parallelLine = new Line2D (_m, p);

  63.         return parallelLine;
  64.     }

  65.     public Line2D perpendicularLineAtPoint (Point2D p)
  66.     {
  67.         Line2D perpendicularLine = new Line2D (-1 / _m, p);

  68.         return perpendicularLine;
  69.     }

  70.     public Point2D intersection (Line2D lineOther)
  71.     {
  72.         double b1 = yIntercept();
  73.         double b2 = lineOther.yIntercept();
  74.         double m1 = slope();
  75.         double m2 = lineOther.slope();

  76.         double xIntersection = -1 * (b2 - b1) / (m2 - m1);
  77.         double yIntersection = m1 * xIntersection + b1;

  78.         Point2D pointIntersection = new Point2D (xIntersection, yIntersection);

  79.         return pointIntersection;
  80.     }

  81.     public static void main (String[] args)
  82.     {
  83.         double mySlope = 1;
  84.         double myIntercept = 2;

  85.         Line2D myFirstLine = new Line2D (mySlope, myIntercept);

  86.         myFirstLine.show();

  87.         Point2D point1 = new Point2D (1, 3);

  88.         Point2D point2 = new Point2D (4, 6);

  89.         Line2D mySecondLine = new Line2D (point1, point2);

  90.         mySecondLine.show();

  91.         Line2D myThirdLine = new Line2D (mySlope, point2);

  92.         myThirdLine.show();

  93.         double x1 = 1;

  94.         double y1 = myThirdLine.YFromX (x1);

  95.         System.out.println ("Y From  = " + x1 + " is " + y1);

  96.         double y2 = 1;

  97.         double x2 = myThirdLine.XFromY (y2);

  98.         System.out.println ("X From Y = " + y2 + " is " + x2);

  99.         Point2D linePoint = new Point2D (6, 12);

  100.         Line2D parallelLine = myThirdLine.parallelLineAtPoint (linePoint);

  101.         parallelLine.show();

  102.         Line2D perpendicularLine = myThirdLine.parallelLineAtPoint (linePoint);

  103.         perpendicularLine.show();

  104.         Line2D firstLine = new Line2D (new Point2D (0, 0), new Point2D (1, 1));

  105.         Line2D secondLine = new Line2D (new Point2D (1, 1), new Point2D (2, 0));

  106.         Point2D pIntersect = firstLine.intersection (secondLine);

  107.         pIntersect.printCoordinate();
  108.     }
  109. }