Don't like ads? PRO users don't see any ads ;-)

C5X15a_Line.java

By: calcpage on Dec 12th, 2011  |  syntax: Java  |  size: 2.29 KB  |  hits: 89  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class Line
  2. {
  3.         private double m;
  4.         private double b;
  5.  
  6.         /**
  7.                 Constructor
  8.                 @param m is the slope of the line
  9.                 @param b is the y-intercept of the line
  10.         */
  11.         public Line(double m, double b)
  12.         {
  13.                 this.m = m;
  14.                 this.b = b;
  15.         }
  16.  
  17.         /**
  18.                 Constructor
  19.                 @param m is the slope of the line
  20.                 @param x1 is abcissa of a given point on the line
  21.                 @param y1 is ordinate of a given point on the line
  22.         */
  23.         public Line(double m, double x1, double y1)
  24.         {
  25.                 //this.m = m;
  26.                 //this.b = -m*x1+y1;
  27.                 this(m, -m*x1+y1);
  28.         }
  29.  
  30.         /**
  31.                 Constructor
  32.                 @param x1 is abcissa of a given point on the line
  33.                 @param y1 is ordinate of a given point on the line
  34.                 @param x2 is abcissa of another given point on the line
  35.                 @param y2 is ordinate of another given point on the line
  36.         */
  37.         public Line(double x1, double y1, double x2, double y2)
  38.         {
  39.                 //this.m = (y2-y1)/(x2-x1);
  40.                 //this.b = -m*x1+y1;
  41.                 this((y2-y1)/(x2-x1),x1,y1);
  42.         }
  43.  
  44.         /**
  45.                 Accessor
  46.                 @param other is another line to compare to this line
  47.                 @return true iff m1=m2
  48.         */
  49.         public boolean isParallel(Line other)
  50.         {
  51.                 return m==other.getM();
  52.         }
  53.  
  54.         /**
  55.                 Accessor
  56.                 @param other is another line to compare to this line
  57.                 @return true iff m1!=m2
  58.         */
  59.         public boolean intersects(Line other)
  60.         {
  61.                 return !isParallel(other);
  62.         }
  63.  
  64.         /**
  65.                 Accessor
  66.                 @param other is another line to compare to this line
  67.                 @return true iff m1=m2 and b1=b2
  68.         */
  69.         public boolean equals(Object other)
  70.         {
  71.                 Line temp = (Line)other;
  72.                 return isParallel(temp) && b==temp.getB();
  73.         }
  74.  
  75.         /**
  76.                 Accessor
  77.                 @return the slope of this line
  78.         */
  79.         private double getM()
  80.         {
  81.                 return m;
  82.         }
  83.  
  84.         /**
  85.                 Accessor
  86.                 @return the y-intercept of this line
  87.         */
  88.         private double getB()
  89.         {
  90.                 return b;
  91.         }
  92.  
  93.         /**
  94.                 Accessor
  95.                 @return the state of this line
  96.         */
  97.         public String toString()
  98.         {
  99.                 return "y = " + m + "x + " + b;
  100.         }
  101.  
  102.         /**
  103.                 Accessor
  104.                 @param other is another line to compare to this line
  105.                 @return the abcissa=(b2-b1)/(m1-m2) of the POI for this and other
  106.         */
  107.         public double getX(Line other)
  108.         {
  109.                 return (other.getB()-b)/(m-other.getM());
  110.         }
  111.  
  112.         /**
  113.                 Accessor
  114.                 @param other is another line to compare to this line
  115.                 @return the ordinate=m(b2-b1)/(m1-m2)+b of the POI for this and other
  116.         */
  117.         public double getY(Line other)
  118.         {
  119.                 return m*getX(other)+b;
  120.         }
  121. }
  122.  
  123.