home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 23 / IOPROG_23.ISO / SOFT / RAYCAST.ZIP / GETANGLE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-12  |  1.3 KB  |  58 lines

  1. #include "ray.h"
  2. #include "globals.h"
  3. #include "getangle.h"
  4. #include "fixed.h"
  5. #include "abs.h"
  6. #include <math.h>
  7.  
  8. #define RADIAN_COUNT 6.28
  9.  
  10. angle_type * atan_table;
  11.  
  12. void Init_Get_Angle() {
  13.    atan_table=(angle_type *)NewPtr(ONE * sizeof(angle_type));
  14.    float real_ratio;
  15.    MYFIXED cur_ratio;
  16.    for (cur_ratio=0; cur_ratio<ONE; cur_ratio++) {
  17.       real_ratio=((float)cur_ratio)/ONE;
  18.       atan_table[cur_ratio]=(angle_type)(atan(real_ratio)*ANGLE_360/RADIAN_COUNT);
  19.    }
  20. }
  21.  
  22. void Close_Get_Angle() {
  23.    delete atan_table;
  24. }
  25.  
  26. angle_type Get_Angle_Towards(long dx, long dy) {
  27.    angle_type base_angle;
  28.  
  29.    if (ABS(dx)>ABS(dy)) {
  30.       MYFIXED ratio=ABS(fixeddiv(dy, dx));
  31.       if (ratio>=ONE)
  32.          base_angle=ANGLE_45;
  33.       else base_angle=atan_table[ratio];
  34.    } else {
  35.       if (dy==0) {
  36.          return 0;
  37.       }
  38.       MYFIXED ratio=ABS(fixeddiv(dx, dy));
  39.       if  (ratio>=ONE)
  40.          base_angle=ANGLE_45;
  41.       else base_angle=ANGLE_90-atan_table[ratio];
  42.    }
  43.  
  44.    if (dy<0) {
  45.       if (dx>0) {
  46.          return (ANGLE_360-base_angle);
  47.       } else {
  48.          return (base_angle+ANGLE_180);
  49.       } /* endif */
  50.    } else {
  51.       if (dx<0) {
  52.          return (ANGLE_180-base_angle);
  53.       } else {
  54.          return (base_angle);
  55.       } /* endif */
  56.    } /* endif */
  57. }
  58.