views:

80

answers:

2

So basically, we create four lines. One is created by giving a point (x,y) and the slope, the second is created by giving two points (x1, y1) and (x2,y2), the third is created as an equation in slope intercept form y=mx+b, and the fourth is given as an equation x=a with the line being vertical. To do that, we create four constructors.

Then, we're supposed to implement methods to see if the lines intersect, equal each other, or are parallel. My problem is, I have no clue how to code up "Take slope of line one and compare it to line three" "Take slope of line 2 and compare it to line 1" in the methods. If anyone could provide some insight I'd greatly appreciate it. My code right now is terribly convoluted, I know.

Links to Pastebin:

pastebin.com/zGrZL0D7

pastebin.com/A1K18gAH

A: 

Convert each line into a canonical form (e.g., the "two distinct points" form) and write a comparison function that operates on two lines presented in this form.

Philip Starhill
A: 

Your Line class does not need a set of variables for each line - just one set of doubles. Each Line object should only know its own variables.

Internally, your Line should be represented in general form (Ax + By + C = 0), and you should calculate A, B and C from the given representation. You may also want to store the slope as a fourth variable to make comparison easier. (your Line class should have private doubles - A, B, C and possibly m)

Your LineTester class is already set up to properly compare Line objects, if you finish writing the Line class. Inside the comparison methods you will use statements such as

if (m == other.m)

to implement your comparison logic.

RD
Thanks for the help!
Matt
I followed what you said, instead of Ax+By+C=0 I used slope intercept form. All of my tests now work except when I compare line 2 to line 4 (the vertical one). I know the problem lies in line 4, since other comparisons with line two work fine. Any idea what the problem may be? I think it's because the slope of a vertical line would be undefined and I'm not sure how to represent that.
Matt
Here's my updated code: pastebin.com/0cjyhZY1
Matt
A vertical line is what would be considered a special case. You need to test special cases before doing your normal tests. For instance, when testing for parallel lines, you would first test if either line is vertical. If neither line is vertical, proceed to your standard test. But if one is vertical, they can only be parallel if they are BOTH vertical.
RD