delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/02/23/18:05:46

From: Bertrand COCONNIER <bcoconni AT club-internet DOT fr>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: math problem
Date: Tue, 23 Feb 1999 23:59:59 +0100
Organization: Club-Internet (France)
Lines: 33
Message-ID: <36D332EF.8841B673@club-internet.fr>
References: <36d2d0aa DOT 0 AT news DOT sbbs DOT se>
NNTP-Posting-Host: perigueux-2-55.club-internet.fr
Mime-Version: 1.0
X-Trace: front4.grolier.fr 919810541 29844 195.36.138.55 (23 Feb 1999 22:55:41 GMT)
NNTP-Posting-Date: 23 Feb 1999 22:55:41 GMT
X-Mailer: Mozilla 4.5 [fr] (Win98; I)
X-Accept-Language: fr
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Erik Johansson a écrit :

> if i have a line like this
> y
> |     /
> |    /
> |   /
> |  /
> | /
> |/_____________ x
>
> How can I calculate the angle of that line in degrees?
>
> Thanx in advance

You should first compute the slope of the line : slope= (y2-y1)/(x2-x1).
(x1,y1) and (x2,y2) are any plots on your line (they have to be
distinct). On your draw, the line seems to be drawn from the origin to a
point (x,y) so 'slope=y/x'. Then you compute atan(slope). Be careful :
1. The result is in radians
2. If the line is vertical the slope computation will fail but (of
course) the result is 90° or -90° depending on the sign of 'y'.
3. The result of atan() is in between -pi/2 and pi/2. If your angle
should have different values you must care about it. A simple test can
help you to determinate whether the angle is in or out the [-pi/2;pi/2]
interval.
 if (x>0.)
  angle=atan(slope); /* the angle is in between -pi/2 and pi/2*/
else
  angle=atan(slope)+pi; /* the angle is in between pi/2 and 3pi/2*/

    Bertrand.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019