I need two Geometry formula's and I'm hoping someone out there can help...
The first is to calculate the distance between two points, such as x1,y1 and
x2,y2.
The other is to calculate the angle between two lines with a common
endpoint, such as Line1=(x1,y1),(x2,y2),Line2=(x2,y2),(x3,y3).
No, this is not a homework problem. :) I need to perform these calculations
in some software I am working on and I don't have any of my Geometry books
laying around from school. Any help would be greatly appreciated!
Thanks,
Ray wrote:
> The first is to calculate the distance between two points, such as x1,y1 and
> x2,y2.
>
distance = sqrt ( (x1-x2)^2 + (y1-y2)^2 ) (derived from Pythagorean Theorem)
> The other is to calculate the angle between two lines with a common
> endpoint, such as Line1=(x1,y1),(x2,y2),Line2=(x2,y2),(x3,y3).
The thing here is to realize that you have 2 lines, each of which has a slope.
Each slope is the tangent of some angle. Subtract the larger angles from the
smaller, and voila, except that you may sometimes want the *other* angle, i.e.
the supplement of your answer.
Thus: the slope of Line1 is (y1 - y2) / (x1 - x2). call this m1.
Using a calculator or your preferred computer language, ask for the arctangent
of this angle. You calculator or computer may give this in radians, which will
go from - pi/2 to + pi/2, or in degrees, from -90 to +90 degrees.
I imagine you probably want degrees. To convert from radians, just remember that
pi radians means 180 degrees, so you can divide the # of radians by pi and
multiply the result by 180.
Now you have angle1, which is the angle that line1 makes with the x-axis.
The slope of Line2 is going to be (y2 -y3) / (x2 - x3), call this m2.
Again, find the angle whose tangent is m2 (i.e., the arctangent of m2).
Subtract these 2 angles (bigger minus smaller), and adjust in case you want to
have the obtuse angle instead of the acute angle.
I suggest you try a few cases where you KNOW what the answer will be, first.
Watch out: if one of your lines is vertical, the slope will be infinite (because
you are dividing by zero), and this will cause problems. But they are easily
solved with a simple IF-THEN statement.
Hope this helps.
D = sqr( (x2-x1)^2 + (y2-y1)^2)
The angle you can get a couple of ways. First,
let the common point be P1(x1,y1)
a second point on one of the lines be P2(x2,y2)
a third point, on the other line, be P3(x3,y3)
now let vector a = P1P2 = (x2-x1)i + (y2-y1)j and vector b = P1P3 =
(x3-x1)i + (y3-y1)j
cos alpha = a dot b / |a| |b|
where alpha is the angle between the two lines. This seems like a lot of
work, but vectors can also provide some shortcuts later on in your
programming.
Another, maybe easier, route to this angle is:
Line 1 : y = m1*x+b1
Line 2 : y = m2*x+b2
(If you don't remember how to get a line into standard form, e-mail me
and I'll help)
Now
alpha1 = arctan( m1) and
alpha2 = arctan( m2) and
alpha12 = alpha2-alpha1 for the angle from line1 to line2
Bill Weaver