comparison model/atan.c @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal shipping
9 charge). Anyone wanting to incorporate all or part of this software in a
10 commercial product must obtain a license from the Medical Research Council.
11
12 The MRC makes no representations about the suitability of this
13 software for any purpose. It is provided "as is" without express or implied
14 warranty.
15
16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 #include <stdio.h>
25 #include <math.h>
26
27 #include "stitch.h"
28
29 #define OUTPUT_SCALE 360
30
31 #ifdef TEST
32
33 #define TwoPi (3.1415926535*2)
34
35
36 main( argc, argv )
37 int argc ;
38 char *argv[] ;
39 {
40 double deg ;
41
42 printf( "hello\n" );
43
44 for( deg = 0 ; deg < TwoPi/2 ; deg += TwoPi/90001. )
45 if( (int) ( atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE + 0.5 ) != iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) )
46 printf( " %f %d\n", atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE, iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) ) ;
47
48 for( ; deg < TwoPi ; deg += TwoPi/90001. )
49 if( (int) ( atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE - 0.5 ) != iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) )
50 printf( " %f %d\n", atan2( sin( deg ), cos( deg ) )/TwoPi*OUTPUT_SCALE, iatan2( (int) ( sin(deg)*1000 ), (int) ( cos(deg)*1000 ) ) ) ;
51
52
53 }
54
55 #endif
56
57 iatan2( y, x )
58 int y, x ;
59 {
60 static int table_bits = 14 ;
61 static unsigned table_size ;
62 static short *atan_table = ( short * ) 0 ;
63 double two_phi ;
64 int i ;
65
66 if( atan_table == ( short * ) 0 ) {
67
68 table_size = 1 << table_bits ;
69
70 atan_table = ( short * ) stitch_malloc( sizeof ( *atan_table ) * ( table_size + 1 ), "Making atan lookup table" ) ;
71
72 two_phi = atan( 1. ) * 8. ;
73
74 for( i=0 ; i <= table_size ; i++ )
75 atan_table[ i ] = atan( ( double ) i / ( table_size ) ) / two_phi * OUTPUT_SCALE + 0.5 ;
76 }
77
78 if( x != 0 || y != 0 )
79 if( y > 0 )
80 if( x > 0 )
81 if( x > y )
82 return( atan_table[ ( ( y << table_bits + ( table_size >> 1 ) ) ) / x ] ) ;
83 else
84 return( ( OUTPUT_SCALE >> 2 ) - atan_table[ ( ( x << table_bits + ( table_size >> 1 ) ) ) / y ] ) ;
85 else
86 if( -x < y )
87 return( ( OUTPUT_SCALE >> 2 ) + atan_table[ ( ( -x << table_bits + ( table_size >> 1 ) ) ) / y ] ) ;
88 else
89 return( ( OUTPUT_SCALE >> 1 ) - atan_table[ ( ( y << table_bits + ( table_size >> 1 ) ) ) / -x ] ) ;
90 else
91 if( x < 0 )
92 if( -x > -y )
93 return( -( OUTPUT_SCALE >> 1 ) + atan_table[ ( ( -y << table_bits + ( table_size >> 1 ) ) ) / -x ] ) ;
94 else
95 return( -( OUTPUT_SCALE >> 2 ) - atan_table[ ( ( -x << table_bits + ( table_size >> 1 ) ) ) / -y ] ) ;
96 else
97 if( x < -y )
98 return( -( OUTPUT_SCALE >> 2 ) + atan_table[ ( ( x << table_bits + ( table_size >> 1 ) ) ) / -y ] ) ;
99 else
100 return( - atan_table[ ( ( -y << table_bits + ( table_size >> 1 ) ) ) / x ] ) ;
101 else
102 return( 99 ) ;
103 }