Register | Login | |||||
Main
| Memberlist
| Active users
| Calendar
| Chat
| Online users Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album |
| |
0 users currently in Programming. |
Acmlm's Board - I3 Archive - Programming - Line intersection? | New poll | | |
Add to favorites | Next newer thread | Next older thread |
User | Post | ||
Smallhacker Super Koopa I AM A Group Of Officially Frustrated Younglings, G.O.O.F.Y. MEMBER |
| ||
Well... Technically, this is a math problem, but there's no math forum. Also, I need this for a program, so I guess that it sort of fits here.
I need a formula for calculating the location at which two infinite lines intersects. The lines are described with a coordinate and an angle, instead of a coordinate and a slope (due to 90 degrees having an infinite slope and such stuff). I've created a formula myself, but it doesn't work with all angles. Does anyone have a formula that calculates the intersection of two lines that works in all cases (except when the lines are parallel)? |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
Could you post the formula? Maybe someone can spot a flaw in it.
Also, I have a formula that determines whether a line intersects a triangle in 3D space if that would help. I know that's probably basic stuff to anyone who does a lot of 3D work, but it's a pain to someone who isn't experienced in the field and didn't pay attention in Trigonometry class. |
|||
MathOnNapkins 1100 In SPC700 HELL Since: 11-18-05 Last post: 6432 days Last view: 6432 days |
| ||
If you know the equations for the two lines the solution is a simple matrix calculation. In general, if you have:
ax + by = c (first line's equation) dx + ey = f (second line's equation) Then the point where they would hypothetically intersect would be the solution of the matrix equation: [[a b]][x] = [c] [[c d]][y] [f] i.e. you multiply by the inverse of the matrix A, where A is: [[a b]] [[c d]] and the vector x̄ = [x,y]t (t indicates transpose) let [c,f]t be represented as ȳ as well. thus producing A-1Ax̄= x̄= A-1ȳ which would yield your answer. If the matrix is singular (noninvertible) then the lines are parallel, and possibly collinear (the same exact line). Most programming languages have libraries that allow you to deal with Matrix manipulation, even inversion, though the formula for inverting a 2x2 matrix is simple anyways. You may want to use the library just to make the code readable. The case for 3 dimensional lines and above is more complicated of course, but still pretty easy. (btw, I didn't fully read your post at first, but slopes are not a problem when using standard form anyways, so I'd suggest you use this format. You can derive the standard form ax + by = c type equation from just an angle and a coordinate, but I won't say how b/c that would spoil the fun of figuring it out ) (edited by MathOnNapkins on 11-25-06 01:45 PM) (edited by MathOnNapkins on 11-26-06 10:51 AM) |
|||
blackhole89 Moronic Thread Bodycount: 17 (since 2006-08-21 09:50 EST) F5 F5 F5 F5 F5 Since: 12-31-69 From: Dresden/SN/DE Last post: 6434 days Last view: 6432 days |
| ||
For a clearer solution (which I schemed out on paper yesterday but were unable to post due to various reasons until now D
Let x1, y1 be the coordinates of the given point of line 1 and a be its slope angle. Let x2, y2 be the coordinates of the given point of line 2 and b be its slope angle. (They'd be alpha and beta, but I am unsure as to what encoding to use here for greek letters...) Then... you first retrieve a factor m: m=(y1 cos a + x2 sin a - x1 sin a - y2 cos a)/(sin b cos a - cos b sin a). Then you calculate the point of intersection, which may be defined as (u, v), as follows: x2 + m cos b = u y2 + m sin b = v For a=b, the lines are obviously parallel, leading to (sin b cos a - cos b sin a) = 0 and thus no results. Hope that helps... (I didn't deep-verify it against minor calculation errors yet, but an elementary test with (0,0),45° and (1,0),90° I did yielded correct results) |
|||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
This is probably the biggest problem I have when people post equations.
Originally posted by blackhole89 Now is that sin (b * cos a) or sin b * cos a or sin ((b * cos a) - cos b) or what? Is there a defined order for these operations I don't know about? |
|||
blackhole89 Moronic Thread Bodycount: 17 (since 2006-08-21 09:50 EST) F5 F5 F5 F5 F5 Since: 12-31-69 From: Dresden/SN/DE Last post: 6434 days Last view: 6432 days |
| ||
(sin b) (cos a) - (cos b) (sin a). | |||
HyperHacker Star Mario Finally being paid to code in VB! If only I still enjoyed that. <_< Wii #7182 6487 4198 1828 Since: 11-18-05 From: Canada, w00t! My computer's specs, if anyone gives a damn. STOP TRUNCATING THIS >8^( Last post: 6432 days Last view: 6432 days |
| ||
Right, but how do you tell in general? Is it always that way? | |||
Smallhacker Super Koopa I AM A Group Of Officially Frustrated Younglings, G.O.O.F.Y. MEMBER |
| ||
Thanks a lot. | |||
MathOnNapkins 1100 In SPC700 HELL Since: 11-18-05 Last post: 6432 days Last view: 6432 days |
| ||
Originally posted by HyperHacker Unless you have some reason to assume that it is sine or cosine of several varied arguments, I'd not worry too much about it. if it was sin wx or cos 3x + 5 then I'd be a little more worried. Common sense says that most equations for practical usage are not going to involve sines and cosines within one another, but yeah I do understand your frustration. And boo to Blackhole for ruining the fun. |
Add to favorites | Next newer thread | Next older thread |
Acmlm's Board - I3 Archive - Programming - Line intersection? | | |