Hmm, 2d line intersection, you don't need sin/cos for that ;)
Base point (ax, ay), direction vector (bx, by).
x = ax + n * bx
y = ay + n * by
For example, a line through (0, 2), with direction (2, 1) (meaning, if x
changes 2, y changes 1).
x = 0 + n * 2
y = 2 + n * 1
n is a varying real number giving how often you have to add the direction vector to the base point to reach another point at the line.
(n=0 gives the base point, other values give you the other points at the line.)
Every point at the line has a unique value for n for this line.
For example n at y = 0:
0 = 2 + n * 1
= 2 + n
n = -2
x at that point: 0 + (-2) * 2 = 0 - 4 = -4.
A trick to get a direction is to take two known points at the line, and
Say a line between (2, 5) and (8, 78)
Direction between both points is (8-2, 78-5)
x = 2 + p * 6
y = 5 + p * 73
A nice side-effect of this calculation is that p=0 gives you the first point
and p=1 gives you the second point.
For line intersection you need a 2nd line:
x = cx + m * dx
y = cy + m * dy
At the intersection point, x and y of both lines are the same:
ax + n * bx = cx + m * dx
ay + n * by = cy + m * dy
Two equations, two unknowns, piece of cake :p
(standard equation manipulation to pull the unknown n and m out. I am omitting all special cases where coordinates are 0, which means the direction is parallel to one of the axes)
From the top equation
n * bx = cx + m * dx - ax
n = (cx + m * dx - ax) / bx
And similarly for the bottom equation:
n * by = cy + m * dy - ay
n = (cy + m * dy - ay) / by
Obviously, n can only have one value, thus
(cx + m * dx - ax) / bx = (cy + m * dy - ay) / by
by * ((cx + m * dx - ax) / bx) = cy + m * dy - ay
by * (cx + m * dx - ax) = bx * (cy + m * dy - ay)
by * cx + m * by * dx - by * ax = bx * cy + m * bx * dy - bx * ay
m * by * dx - m * bx * dy = bx * cy - bx * ay - by * cx + by * ax
m * (by * dx - bx * dy) = bx * cy - bx * ay - by * cx + by * ax
m = (bx * cy - bx * ay - by * cx + by * ax) / (by * dx - bx * dy)
which looks scary but is a computable value if you know the original lines base points and directions.
(hopefully I did not make an error in it)
then use the original second line equations to compute x and y of the
My project: Messing about in FreeRCT
, dev blog
, and IRC #freerct at oftc.net