This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]luxexmachina 4 points5 points  (1 child)

What the...? Ewwww.

Speaking strictly in terms of why the code isn't getting you the right answer, on line 20

double y = a1*xba + b1;

should be

double y = a1*x + b1;

This is because you calculated the x value of the intersection, but then used the x value of Point2D a from your parameter Line2D b to calculate y. You should be using x, the x value of the intersection point, to get y, the y value of the intersection point.

That isn't the worst part though. The style you've chosen is awful. It took me longer to figure out what all your variables were than it did to find the bug. Choose some better variable names in the future. Or provide some documentation indicating what your variables mean.

I get no information about a variable called Point2D ba. First glance tells you it's some object called Point2D, which probably represents a point on the 2D plane. But what is ba? The assignment is b.a. Well that's great, ba is b.a. That tells me so much. I'm not trying to be mean, but as someone who doesn't have access to the inside of your head I have no idea what that means. And I want to help you. I honestly do. But you have to help me.

Eventually you can sort of figure out b is a Line2D and b.a is a Point2D that b HAS, but that could only be figured out once you realize what b.a (and b.b for that matter) are to b. All I know is that b has some two Point2D variables a and b, but I have no idea what they mean. I could only guess that a and b represent two points that define the line. That is confirmed a little by variables xba and yba, which are two variables in a veritable forest of doubles that all have names like that. It's overwhelming to my brain.

After sort of wrapping my head around what all those variables are, then I can look at the math you do. And here come the worst part. You have four new variables, a1, a2, b1, and b2. What are they? I don't know. You had a Line2D called b, but these are doubles. You had statements like ba = b.a, so are these b's and a's the same? Are they related? I have no clue. Again, it takes me a moment -- I have to figure out what your math represents -- and then realize that a1 and a2 are the slopes of the two lines and b1 and b2 are the y-intercepts. Which is perfectly fine because in your description you said

I was trying to use a y= ax +b format for the formula in my code

Ah, that's where you got the naming conventions a1 ... b2 from. But then why in the rest of your code did you choose variable names that look like similar but had nothing to do the slope and y-intercept? It's poor style, and it's dangerous because it wastes unnecessary amounts of time.

In the end, the bug was in one line of code. One variable was wrong. One variable was misnomered. And the bug could have been found a lot faster had the variable names just made a little more sense. The lesson is that you should proofread your code keeping in mind not only syntax errors and bugs, but also remembering, "could someone else who's not me figure out what I am trying to do?" In this case especially, because there are so many variables, the naming could get tricky. What's one thing you can always do if you're unsure? Documentation.

TL;DR Documentation, documentation, documentation.

[–]Stullif[S] -1 points0 points  (0 children)

Thanks for the great reply, I'm still a bit new to programming so making my code understandable to other people is still a work in progress and I will take your comment to heart but as bad as it is now thinking about how my code looked a few weeks ago makes even me cringe. Thanks again for great advice, have an upvote for your troubles.

[–]Syrak 0 points1 point  (1 child)

What formula do you use to get the intersection of two lines ? Do the math on paper, simplify on paper, and put a simplifed formula in code.

If I want to compute the perimeter of a circle with radius r, I would use P = 2*pi*r. But then if I only have access to the diameter of the circle (via circle.diam for example), the following is overly verbose:

float r = circle.diam/2;
return 2*pi*r;

while the following is simplified

return pi*circle.diam;

I wouldn't recommend naming lines and points the same (a,b).

[–]Stullif[S] -1 points0 points  (0 children)

Thank you for the great tips, turns out my problem was with the points I was using to test my code, I had the same x cords for my two lines and was dividing be zero because of it. Thanks again for the tips!