1 | /* |
2 | Copyright (C) 1998-2001 Andreas Zehender <az@azweb.de> |
3 | |
4 | it under the terms of the GNU General Public License as published by |
5 | the Free Software Foundation; either version 2 of the License, or |
6 | (at your option) any later version. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
16 | */ |
17 | |
18 | #include "mathroutines.h" |
19 | |
20 | #include "math.h" |
21 | |
22 | double rectToAngle(double x,double y) |
23 | { |
24 | double phi=0; |
25 | if(fabs(x)<1e-6) |
26 | { |
27 | if(y>0) |
28 | phi=M_PI_2; |
29 | else |
30 | phi=-M_PI_2; |
31 | } |
32 | else |
33 | { |
34 | phi=atan(y/x); |
35 | if(x<0) |
36 | phi+=M_PI; |
37 | } |
38 | if(phi>M_PI) |
39 | phi-=2*M_PI; |
40 | return phi; |
41 | } |
42 | |
43 | double average(double phi1,double phi2) |
44 | { |
45 | return phi2+difference(phi1,phi2)/2.0; |
46 | } |
47 | |
48 | double difference(double phi1,double phi2) |
49 | { |
50 | double dif; |
51 | |
52 | dif=phi1-phi2; |
53 | while(dif>M_PI) |
54 | dif-=2*M_PI; |
55 | while(dif<-M_PI) |
56 | dif+=2*M_PI; |
57 | |
58 | return dif; |
59 | } |
60 | |