Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Line
- {
- private static double EPSILON=1E-14;
- private double cx1;
- private double cy1;
- private double cx2;
- private double cy2;
- private double m;
- private double cb;
- private double ca;
- //When (x,y) and m are given
- public Line(double x1, double y1, double slope)
- {
- //Sets the variables
- cx1=x1;
- cy1=y1;
- m=slope;
- //Finds b for y=mx+b
- cb=(cx1*m)-y1;
- }
- //When (x1, y1) and (x2, y2) are given
- public Line(double x1, double y1, double x2, double y2)
- {
- //Sets the variables
- cx1=x1;
- cy1=y1;
- cx2=x2;
- cy2=y2;
- //Finds the slope
- m=(cy2-cy1)/(cx2-cx1);
- //Finds b for y=mx+b
- cb=(cx1*m)-y1;
- }
- //When y=mx+b is given
- public Line(double slope, double b)
- {
- //Sets the variables
- m=slope;
- cb=b;
- }
- //When x=a is given for vertical lines
- public Line(double a)
- {
- //Sets the variables
- ca=a;
- //Slope and y intercept are zero, since it's vertical
- m=0;
- cb=0;
- }
- public boolean intersects(Line other)
- {
- if(Math.abs(m-other.m) > EPSILON)
- return true;
- else
- return false;
- }
- public boolean equals(Line other)
- {
- if((Math.abs(m-other.m) < EPSILON) && (Math.abs(cb-other.cb) < EPSILON))
- return true;
- else
- return false;
- }
- public boolean isParallel(Line other)
- {
- if(Math.abs(m-other.m) < EPSILON)
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement