Math wizardry

Richard K. Herz herz at ucsd.edu
Wed Mar 2 12:51:31 EST 2005

> I've got two line graphics drawn on the screen. I need to find out if
> they intersect and, if so, what the angle is that is formed by their
> intersection. Any simple way to do this?

See, e.g.,
for each line defined by endpoints x1,y1 and x2,y2
st line equation y = m*x + b (m = slope, b = y intercept at x = 0)
m = (y2 - y1)/(x2 - x1) = slope of line
b = y1 - m*x1 = intersection of line with y axis at x = 0
for two lines, get m1 and m2, b1 and b2
Any two nonparallel lines (m1 not m2) intersect
find x such that y's are equal
m1*x + b1 = m2*x + b2
x = (b2 - b1)/(m1 - m2)  = x at intersection
y at intersection = m1*x + b1 = m2*x + b2
slope m1 = tangent of angle A1 between line 1 and x-axis
angle A1 = inverse tangent(m1) = atan(m1) in Rev
angle A2 = inverse tangent(m2)
see http://www.mathwords.com/t/tangent_inverse.htm
A2 - A1 = one pair of the two pairs of angles formed by the intersection of
two straight lines, with the other pair being pi radians (180 degrees) minus
this angle
Note that Rev and most other languages refer to angles in radians rather
than degrees in their trig functions.
Of course, work out a few examples and graph them to double-check this and
to understand the angle values returned.

Rich Herz
herz at ucsd.edu