Mail Archives: djgpp/1999/02/23/19:00:39
From: | Martin Ambuhl <mambuhl AT earthlink DOT net>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Re: math problem
|
Date: | Tue, 23 Feb 1999 18:16:03 -0500
|
References: | <36d2d0aa DOT 0 AT news DOT sbbs DOT se>
|
X-Posted-Path-Was: | not-for-mail
|
X-Accept-Language: | en
|
X-ELN-Date: | 23 Feb 1999 23:15:53 GMT
|
X-ELN-Insert-Date: | Tue Feb 23 15:25:03 1999
|
Organization: | Nocturnal Aviation
|
Lines: | 40
|
Mime-Version: | 1.0
|
NNTP-Posting-Host: | 1cust140.tnt11.nyc3.da.uu.net
|
Message-ID: | <36D336B3.5C3797D@earthlink.net>
|
X-Mailer: | Mozilla 4.5 [en] (Win95; I)
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Erik Johansson wrote:
>
> if i have a line like this
> y
> | /
> | /
> | /
> | /
> | /
> |/_____________ x
>
> How can I calculate the angle of that line in degrees?
>
> Thanx in advance
This is really not a djgpp question, but what-the-heck.
If the ray runs through the origin to (x,y) then tangent is
defined as
tan(theta) = y/x
the angle (in the range of -pi to +pi) is given by
theta = atan2(y,x); /* actual code */
If you are not compiling with -ansi, then you have the
constant M_PI from <math.h>.
The angle returned from atan2 will be in radian measure (not 'radians'
since it is unitless). A circle is 2*M_PI in radian measure,
corresponding to 360 degrees. You no doubt want your numbers in the
range [0,360), so we must make theta positive:
if (theta < 0) theta += 2*M_PI;
Now theta is in the range [0,2*M_PI).
To convert this to degrees is trivial
degrees = 180. * theta / M_PI;
--
Martin Ambuhl (mambuhl AT earthlink DOT net)
Note: mambuhl AT tiac DOT net will soon be inactive
- Raw text -