File: dir.hc

package info (click to toggle)
craft 3.5-10
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 18,000 kB
  • ctags: 1,602
  • sloc: cpp: 3,794; makefile: 2,319; ansic: 857; sh: 385
file content (90 lines) | stat: -rw-r--r-- 1,908 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

#include "stdio.h"

#include "dir.h"
#include "xmath.h"
#include "errorhandling.h"

int dir_left (int dir)
  {int d = dir - 1;   

   if (d < 0)
      d = 7;
   return d;
  } 

int dir_right (int dir)
  {int d = dir + 1;   

   if (d == 8)
      d = 0;
   return d;
  } 

void dir_dx_dy (int dir, int &dx, int &dy)
  {switch (dir)
     {case 0  : dx =  0; dy = -1; break;
      case 1  : dx = -1; dy = -1; break;
      case 2  : dx = -1; dy =  0; break;
      case 3  : dx = -1; dy =  1; break;
      case 4  : dx =  0; dy =  1; break;
      case 5  : dx =  1; dy =  1; break;
      case 6  : dx =  1; dy =  0; break;
      case 7  : dx =  1; dy = -1; break;
      default : errorstop (1, "DIR", "invalid direction"); break;
     };
  }
       
int direction (int dx, int dy)
  {if (dx == 0 && dy <  0) return 0;
   if (dx <  0 && dy <  0) return 1;
   if (dx <  0 && dy == 0) return 2;
   if (dx <  0 && dy >  0) return 3;
   if (dx == 0 && dy >  0) return 4;
   if (dx >  0 && dy >  0) return 5;
   if (dx >  0 && dy == 0) return 6;
   if (dx >  0 && dy <  0) return 7;
   return 0;
   errorstop (2, "DIR", "invalid direction");
  }

int fdir (int dx, int dy)
   {if      (dy != 0) perform_fdir
    else if (dx <  0) return 2;
    else              return 6;     

.  perform_fdir
     {double d = g_arc_tan2 (dx, dy);

      if ( -22 <= d && d <=   22) return 6;
      if (  22 <= d && d <=   66) return 5;
      if (  66 <= d && d <=  110) return 4;
      if ( 110 <= d && d <=  154) return 3;
      if ( -66 <= d && d <=   22) return 7;
      if (-110 <= d && d <=  -66) return 0;
      if (-154 <= d && d <= -110) return 1;
      return 2;
     }.

   }

int dir_back (int dir)
  {int d = dir + 4;
  
   if (d > 7)
      d -= 8;
   return d;
  }

int left_dist (int dir1, int dir2)
  {int c = 0;
   int d = dir1;

   while (d != dir2)
     {c++;
      d = dir_left (d);
     };
   return c;
  }