Mick Dugan Posted December 14, 2005 Posted December 14, 2005 Does anyone have a function that will take the coordinates of 2 lines (that are in the same plane) and compute the intersection point? TIA Quote
Leaders Iceplug Posted December 17, 2005 Leaders Posted December 17, 2005 Coordinates? Lines don't have coordinates. Points have coordinates, and lines have properties such as slope and Y-intercept. Assuming you have points that are supposed to represent lines, you can calculate the slope and y-intercept using the same formula from calculus. Slope = (Y2 - Y1) / (X2 - X1) If X2 = X1 then you'll just have to set Slope to a really big value. Y-intercept = Y1 - Slope * X1 Do this for both lines so that you have a slope and y-intercept for both lines. Next you'll want to compare the Slopes of these two lines and make sure that these two lines are not parallel. If Slope1 = Slope2 Then the lines are parallel. The remainder of the code would be run if Slope1 <> Slope2, or in the Else of the above slope equality check. If Slope1 <> Slope2 Then 'rest of stuff. To determine where (it's not If, since lines will cross unless they are parallel) the two lines cross... you know there must be a point where the same X-coordinates put into both line equations Y = MX + B will produce the same Y. So, therefore you find the X that will produce the same Y by setting them equal. M1 * Xc + B1 = M2 * Xc + B2 And solve for Xc = (M2 - M1) / (B1 - B2) Put Xc into a line equation: Yc = M1 * Xc + B1 to find the Yc coordinate. And this is the intersection point: Xc. Yc :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Leaders snarfblam Posted December 17, 2005 Leaders Posted December 17, 2005 There are many different ways to define lines. One way is with a pair of points, i.e. a line defined by (0, 5) and (3, 10). Another way is with the general equation of a line: Ax + By = c, such as 5x - 3y = -15 (the same as y=5/3x + 5). A third is slope-intercept form (the one you saw in algebra): y = mx + b, such as y=5/3x + 5 (the same as 5x - 3y = -15). And there are more. Depending on your representation of a line, the exact method differs. Iceplug's solution works for slope-intercept form, but the problem with slope-intercept form is that it can not compensate for vertical lines, which essentially have a slope of infinity. My best guess is that you are defining your lines by pairs of points. Also, I would say most likely you have line-segments, which means that you also have to check to make sure the point of intersection is within both segments. I guess what I'm trying to say is you can get a much better solution with more info. Quote [sIGPIC]e[/sIGPIC]
Mick Dugan Posted December 21, 2005 Author Posted December 21, 2005 Thanks for the responses guys. Sorry I didn�t supply enough information. Marble Eater, you were correct in assuming that the two �lines� are actually �line segments� and are derived from the coordinates of their end points. Sometimes they will actually cross, in which case I�d need to find the point of intersection. Usually though, they will not cross, in which case I need to find out the point they would intersect if they were to be extended. As someone who doesn�t know the difference between calculus and Confucius, I was hoping someone had a ready made routine that would fit the bill. Barring that, if I have to do it myself, I could really use some dumbed down Pseudo code to get me started. Quote
Leaders snarfblam Posted December 22, 2005 Leaders Posted December 22, 2005 I was bored so I looked up the equation and coded it, then made a demo app for it. All you really need is the Line struct, which has an intersection function. I wrote this in C#, so I hope you are using C#. If you are using VB and don't know C#, I can translate the Line class for you.Intersection.txt Quote [sIGPIC]e[/sIGPIC]
Mick Dugan Posted December 22, 2005 Author Posted December 22, 2005 Wow, thanks Marble Eater! I hate to push my luck, but I really only know VB. If you could make that translation for me I'd be your biggest fan! Quote
Leaders snarfblam Posted December 23, 2005 Leaders Posted December 23, 2005 I'm such a nice guy.Intersection.vb.txt Quote [sIGPIC]e[/sIGPIC]
Mick Dugan Posted December 23, 2005 Author Posted December 23, 2005 You saved me a HUGE amount of time, and I REALLY, REALLY appreciate it!!! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.