Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Line
- {
- private double m;
- private double b;
- /**
- Constructor
- @param m is the slope of the line
- @param b is the y-intercept of the line
- */
- public Line(double m, double b)
- {
- this.m = m;
- this.b = b;
- }
- /**
- Constructor
- @param m is the slope of the line
- @param x1 is abcissa of a given point on the line
- @param y1 is ordinate of a given point on the line
- */
- public Line(double m, double x1, double y1)
- {
- //this.m = m;
- //this.b = -m*x1+y1;
- this(m, -m*x1+y1);
- }
- /**
- Constructor
- @param x1 is abcissa of a given point on the line
- @param y1 is ordinate of a given point on the line
- @param x2 is abcissa of another given point on the line
- @param y2 is ordinate of another given point on the line
- */
- public Line(double x1, double y1, double x2, double y2)
- {
- //this.m = (y2-y1)/(x2-x1);
- //this.b = -m*x1+y1;
- this((y2-y1)/(x2-x1),x1,y1);
- }
- /**
- Accessor
- @param other is another line to compare to this line
- @return true iff m1=m2
- */
- public boolean isParallel(Line other)
- {
- return m==other.getM();
- }
- /**
- Accessor
- @param other is another line to compare to this line
- @return true iff m1!=m2
- */
- public boolean intersects(Line other)
- {
- return !isParallel(other);
- }
- /**
- Accessor
- @param other is another line to compare to this line
- @return true iff m1=m2 and b1=b2
- */
- public boolean equals(Object other)
- {
- Line temp = (Line)other;
- return isParallel(temp) && b==temp.getB();
- }
- /**
- Accessor
- @return the slope of this line
- */
- private double getM()
- {
- return m;
- }
- /**
- Accessor
- @return the y-intercept of this line
- */
- private double getB()
- {
- return b;
- }
- /**
- Accessor
- @return the state of this line
- */
- public String toString()
- {
- return "y = " + m + "x + " + b;
- }
- /**
- Accessor
- @param other is another line to compare to this line
- @return the abcissa=(b2-b1)/(m1-m2) of the POI for this and other
- */
- public double getX(Line other)
- {
- return (other.getB()-b)/(m-other.getM());
- }
- /**
- Accessor
- @param other is another line to compare to this line
- @return the ordinate=m(b2-b1)/(m1-m2)+b of the POI for this and other
- */
- public double getY(Line other)
- {
- return m*getX(other)+b;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement