Advertisement
advictoriam

Untitled

Jan 8th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. public class Line
  2. {
  3.    private double slope;
  4.    private double yIntercept;
  5.  
  6.    /**
  7.       Construct a line with equation y = mx + b
  8.       @param aSlope the slope of this line
  9.       @param aYIntercept the y intercept of this line
  10.    */
  11.    public Line(double aSlope, double aYIntercept)
  12.    {
  13.       slope = aSlope;
  14.       yIntercept = aYIntercept;
  15.    }
  16.  
  17.    /**
  18.       Checks whether a point is contained on the line
  19.       @param x the x-coordinate of the point
  20.       @param y the y-coordinate of the point
  21.       @return true if (x,y) is contained in the line
  22.    */
  23.    public boolean contains(double x, double y)
  24.    {      
  25.       return Math.abs(x * slope + yIntercept - y) < EPSILON;
  26.    }
  27.  
  28.    /**
  29.       Gets the slope of this line
  30.       @return the slope
  31.    */
  32.    public double getSlope()
  33.    {
  34.       return slope;
  35.    }
  36.  
  37.    /**
  38.       Gets the y intercept of this line
  39.       @return the y intercept
  40.    */
  41.    public double getYIntercept()
  42.    {
  43.       return yIntercept;
  44.    }
  45.  
  46.    /**
  47.       Checks whether this line intersects with another.
  48.       @param other another line
  49.       @return true if this line and other intersect
  50.    */
  51.    public boolean intersects(Line other)
  52.    {
  53.       return (other.getSlope() == this.getSlope() && other.getYIntercept() != this.getYIntercept()) ? false : true;
  54.    }
  55.  
  56.  
  57.    private static final double EPSILON = 1E-12;
  58.  
  59.    // this method is used to check your work
  60.  
  61.    public static boolean check(double slope1, double yIntercept1,
  62.       double slope2, double yIntercept2)
  63.    {
  64.       Line first = new Line(slope1, yIntercept1);
  65.       Line second = new Line(slope2, yIntercept2);
  66.       return first.intersects(second);
  67.    }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement