Article View: rec.games.programmer
Article #410Re: 3d geometry... help please.
From: Ian Bell
Date: Tue, 04 Aug 1992 21:26
Date: Tue, 04 Aug 1992 21:26
50 lines
2532 bytes
2532 bytes
In article <l7lhdbINNkcc@aludra.usc.edu> mduffy@aludra.usc.edu (Michael Duffy) writes:
>Ok, boys and girls, children of all ages... here's something for you to ponder.
>
>I'm trying to figure out the FASTEST way to figure out the angle between two
>rays in space, given any three points (say (a,b,c), (d,e,f), and (g,h,i) with
>(d,e,f) as the point where both rays originate). It has been too many years
>since I took Trig or Geometry or whichever math would help me out here.
>Pseudo code, equations, C, Pascal, or Assembly is appreciated. T
>Thanks in advance for any information,
>Mike Duffy (mduffy@aludra.usc.edu)
The easy quantity to calculate here is the cosine of the angle:
If the vector
u := (a,b,c) - (d,e,f) = (a-d,b-e,c-f)
and vector
v := (g,h,i) - (d,e,f) = (g-d,h-e,i-f)
then
u . v
cos(angle) = ------
|u||v|
(a-d)*(g-d) + (b-e)*(h-e) + (c-f)*(i-f)
= ----------------------------------------------------------------
sqrt(((a-d)^2 + (b-e)^2 + (c-f)^2)*((g-d)^2 + (h-e)^2 + (i-f)^2))
If you *really* want the angle, this is the accurate way to find it.
Evaluate this expression, and take the arccos of the result.
If subsequently you are going to take more trig functions (like sine
or tangent) of the angle, you can eliminate the need for trig function
evaluation, since there are nice formulas for sin(a) if cos(a)=x/y,
and so on (recall that sin(a)^2 + cos(a)^2 = 1).
You will probably be concerned with the expense of the sqrt and arccos
functions. The sqrt function is hard to avoid here, but one common
method for dealing with approximations of a trig function, call it
f(x), is to build a table of exact values for f at many x values,
use a searching technique or direct indexing to find f(x1) and f(x2),
where the desired argument x is between x1 and x2, and use linear
interpolation between the two f values for your result.
--
Ian Bell
(iebell@watcgl.waterloo.edu)
(iebell@watcgl.uwaterloo.ca)
Technical Details
Message-ID:
<BsHA8n.Jr5@watcgl.uwaterloo.ca>Path:
arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watmath!watcgl!iebellReferences:
<l7lhdbINNkcc@aludra.usc.edu> <1992Aug4.092815.27743@hellgate.utah.edu>Show Full Headers
Complete Article Headers
Newsgroups: rec.games.programmer Path: arc1-ca.pugleaf.net!sparky!uunet!utcsri!torn!watserv2.uwaterloo.ca!watmath!watcgl!iebell From: iebell@watcgl.uwaterloo.ca (Ian Bell) Subject: Re: 3d geometry... help please. Message-ID: <BsHA8n.Jr5@watcgl.uwaterloo.ca> Organization: Computer Graphics Laboratory, University of Waterloo, Ontario, Canada References: <l7lhdbINNkcc@aludra.usc.edu> <1992Aug4.092815.27743@hellgate.utah.edu> Date: Tue, 4 Aug 1992 21:26:46 GMT Lines: 50