Advertisement
calcpage

C5X15_Line.java

Dec 1st, 2011
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  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.     }
  28.  
  29.     /**
  30.         Constructor
  31.         @param x1 is abcissa of a given point on the line
  32.         @param y1 is ordinate of a given point on the line
  33.         @param x2 is abcissa of another given point on the line
  34.         @param y2 is ordinate of another given point on the line
  35.     */
  36.     public Line(double x1, double y1, double x2, double y2)
  37.     {
  38.         this.m = (y2-y1)/(x2-x1);
  39.         this.b = -m*x1+y1;
  40.     }
  41.  
  42.     /**
  43.         Accessor
  44.         @param other is another line to compare to this line
  45.         @return true iff m1=m2
  46.     */
  47.     public boolean isParallel(Line other)
  48.     {
  49.         return m==other.getM();
  50.     }
  51.  
  52.     /**
  53.         Accessor
  54.         @param other is another line to compare to this line
  55.         @return true iff m1!=m2
  56.     */
  57.     public boolean intersects(Line other)
  58.     {
  59.         return !isParallel(other);
  60.     }
  61.  
  62.     /**
  63.         Accessor
  64.         @param other is another line to compare to this line
  65.         @return true iff m1=m2 and b1=b2
  66.     */
  67.     public boolean equals(Object other)
  68.     {
  69.         Line temp = (Line)other;
  70.         return isParallel(temp) && b==temp.getB();
  71.     }
  72.  
  73.     /**
  74.         Accessor
  75.         @return the slope of this line
  76.     */
  77.     private double getM()
  78.     {
  79.         return m;
  80.     }
  81.  
  82.     /**
  83.         Accessor
  84.         @return the y-intercept of this line
  85.     */
  86.     private double getB()
  87.     {
  88.         return b;
  89.     }
  90.  
  91.     /**
  92.         Accessor
  93.         @return the state of this line
  94.     */
  95.     public String toString()
  96.     {
  97.         return "y = " + m + "x + " + b;
  98.     }
  99.  
  100.     /**
  101.         Accessor
  102.         @param other is another line to compare to this line
  103.         @return the abcissa=(b2-b1)/(m1-m2) of the POI for this and other
  104.     */
  105.     public double getX(Line other)
  106.     {
  107.         return (other.getB()-b)/(m-other.getM());
  108.     }
  109.  
  110.     /**
  111.         Accessor
  112.         @param other is another line to compare to this line
  113.         @return the ordinate=m(b2-b1)/(m1-m2)+b of the POI for this and other
  114.     */
  115.     public double getY(Line other)
  116.     {
  117.         return m*getX(other)+b;
  118.     }
  119. }
  120.  
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement