2

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.

Paincakes
  • 137
  • 1
  • 2
  • 11

2 Answers2

3
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.

Vitthal Kavitake
  • 879
  • 5
  • 18
  • Also, float/double numbers in Java should (almost) never be compared using `==`. One should test that the absolute value of the difference is less than some small threshold. (See [this thread](http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java) for instance.) – Ted Hopp Feb 17 '16 at 06:06
  • @Vitthal Yeah, I was thinking the same thing too. I don't know how to access the "other" segment point1 and point2. However, cybersam cleared things up for me. – Paincakes Feb 17 '16 at 06:34
2

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.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • Since `isParallelTo` is a member of `Segment`, the points could be `private`. Also, one should never compare floating point numbers using `==`. (See [this thead](http://stackoverflow.com/questions/1088216/whats-wrong-with-using-to-compare-floats-in-java) for why.) – Ted Hopp Feb 17 '16 at 06:07
  • I understand about floating point, but I wonder if the professor is really wanting that level of detail. Also, I do not have access to the `Segment` class, so I just wanted to get the main point across. – cybersam Feb 17 '16 at 06:10
  • @TedHopp I have my data as protected, and thank you to cybersam for your help. I did not know that I could access the point1 and point2 of the other segment just with a simple other.point1 or other.point2. – Paincakes Feb 17 '16 at 06:32