I am trying to figure out how to determine if two lines are parallel to each other. I have a Point class and a Segment class.
So far my slope method is this in my Point class.
public double slopeTo (Point anotherPoint)
{
double totalSlope;
double slope1;
double slope2;
slope1 = (anotherPoint.y - this.y);
slope2 = (anotherPoint.x - this.x);
if(slope2 == 0)
{
slope2 = Double.POSITIVE_INFINITY;
return slope2;
}
else if (slope1 == 0)
{
slope1 = 0;
return slope1;
}
else
{
totalSlope = (slope1 / slope2);
return totalSlope;
}
}
And my parallel method is this in my Segment class.
public boolean isParallelTo (Segment s1)
{
double pointSlope;
pointSlope = (point1.slopeTo (point2));
if (s1 .equals(pointSlope))
return true;
else
return false;
}
There is a tester that my professor provided for us and in the tester, he creates four new points two of which is for one segment and the other two is for the second segment.
s1 = new Segment(3,6,4,1); //<---(x1,y1,x2,y2)
s2 = new Segment(4,7,5,2);
originals1ToString = s1.toString();
originals2ToString = s2.toString();
System.out.println("\nTest6.1: Testing isParallelTo with " + s1 + " and " + s2);
System.out.print("expected: true \ngot: ");
boolean boolAns = s1.isParallelTo(s2);
System.out.println(boolAns);
When I run the tester I am getting a false, but it should be true. So my parallel method is wrong. I know that it cannot be my slope method because I have tested that over and over and everything is correct.
Please help me. I would greatly appreciate it.