Reputation: 147
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.
Upvotes: 2
Views: 124
Reputation: 66975
isParallelTo
should be something like this:
public boolean isParallelTo (Segment other) {
double otherSlope = other.point1.slopeTo(other.point2);
double thisSlope = point1.slopeTo(point2);
return otherSlope == thisSlope;
}
I assume here that point1
and point2
are not private
.
Upvotes: 2
Reputation: 879
if (s1 .equals(pointSlope))
return true;
else
return false;
You are comparing Segment with double, and that will never return true?
Another point is, Slope should be for the segment and not for the combined 2 segments. You can compare slope of segment1 and segment2 and if they are equal then you can say that the segments are equal. You need to change both slope and parallelTo methods.
Upvotes: 3