Miscellaneous > Programming & Networking
A programming challenge all up in your face.
mobrien_12:
--- Quote from: TheQuirk ---Wow, this thread has taken off.
Re: vertical lines, since we have this:
[(y2 - y1)/(x2 - x1)]x + k = y
We could multiply both sides by (x2 - x1) and see what that gets us (there won't be division by 0).
--- End quote ---
Been thinking about this. You could do this but for a vertical line you are multiplying both sides by zero then, which gets you down to the fundamental problem: a vertical line segment is not a function of x. The entire coordinate-type algorithm is based around y being a function of x so it falls apart at this case.
The vector-analysis algorithm that I used doesn't need y to be a function of x.
This doesn't mean that the coordinate-type algorithm can't do it.
Just test to see if it is a vertical line. If so, then simply rotate the coordinate system by 90 degrees and you have a horizontal line. Then the algorithm will work.
worker201:
Would it be ghetto to write a whole other routine just for dealing with verticals? It's a much easier task. Say our vertical line was x=5. You could just plug that into the circle equation and solve for y. If you get 2 different real y values, then the circle intersects the line, and you can continue from there. It would probably be more elegant to write the routine in a way that didn't depend on the slope of the line (or distinct functions of variables), but I think for something as simple and specific as this, maybe a case solution might be better.
WWAD?
(what would Adobe do?)
:P
mobrien_12:
--- Quote from: worker201 ---Would it be ghetto to write a whole other routine just for dealing with verticals? It's a much easier task. Say our vertical line was x=5. You could just plug that into the circle equation and solve for y. If you get 2 different real y values, then the circle intersects the line, and you can continue from there. It would probably be more elegant to write the routine in a way that didn't depend on the slope of the line (or distinct functions of variables), but I think for something as simple and specific as this, maybe a case solution might be better.
WWAD?
(what would Adobe do?)
:P
--- End quote ---
Eh, simplest solution is the best I guess. I was just trying to think of how to make the existing code work for the one special case... I thought that would be the easiest. If a second routine to handle it would be easier, it's probably better to go that way...
TheQuirk:
--- Quote from: mobrien_12 ---Been thinking about this. You could do this but for a vertical line you are multiplying both sides by zero then, which gets you down to the fundamental problem: a vertical line segment is not a function of x. The entire coordinate-type algorithm is based around y being a function of x so it falls apart at this case.
The vector-analysis algorithm that I used doesn't need y to be a function of x.
This doesn't mean that the coordinate-type algorithm can't do it.
Just test to see if it is a vertical line. If so, then simply rotate the coordinate system by 90 degrees and you have a horizontal line. Then the algorithm will work.
--- End quote ---
Aye, the point is that you would get (y2-y1) + k(x2-x1) = y(x2-x1) => y2-y1 = 0 which is obviously false, thus pointing out that it's a vertical line. This is how we generally solved problems which involved coordinates.
(For what it's worth, however, I think your method is a lot nicer.)
mobrien_12:
But I really like the idea that we have two completely different solutions which give the same answers!!! And the code base is about the same length for each!
And you notice how that the challenge was put down a few months ago, but when we all worked together we knocked it out in a week or so? This has really been FUN!
I was able to fix the special cases in my approach that messed it up, maybe this weekend we can fix the vertical line case in the coordinates! :)
Navigation
[0] Message Index
[#] Next page
[*] Previous page
Go to full version