home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / tech / pcbsrcs2 / dist2.c < prev    next >
C/C++ Source or Header  |  1991-02-07  |  5KB  |  134 lines

  1. #include <stdio.h>
  2. #include "cell.h"
  3.  
  4. int GetApxDist( int, int, int, int );
  5. int CalcDist( int, int, int, int );
  6.  
  7. extern int NoDiag;
  8. extern int DoubleSided;
  9.  
  10. int GetApxDist ( r1, c1, r2, c2 ) /* calculate approximate distance */
  11.     int r1, c1, r2, c2;
  12.     {
  13.     register int d1, d2; /* row and column deltas */
  14.     int d0; /* temporary variable for swapping d1 and d2 */
  15.  
  16.     if ((d1 = r1-r2) < 0) /* get absolute row delta */
  17.         d1 = -d1;
  18.     if ((d2 = c1-c2) < 0) /* get absolute column delta */
  19.         d2 = -d2;
  20.     if (!d1) /* in same row? */
  21.         return( (d2*50) ); /* 50 mils per cell */
  22.     if (!d2) /* in same column? */
  23.         return( (d1*50) ); /* 50 mils per cell */
  24.     if (d1 > d2) { /* get smaller into d1 */
  25.         d0 = d1;
  26.         d1 = d2;
  27.         d2 = d0;
  28.         }
  29.     d2 -= d1; /* get non-diagonal part of approximate "route" */
  30.     return( (d1*71)+(d2*50) ); /* 71 mils diagonally per cell */
  31.     }
  32.  
  33. /* distance to go thru a cell */
  34. static int dist[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  35.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  36. /* N  */ { 50, 60, 35, 60, 99, 60, 35, 60,   12, 12 },
  37. /* NE */ { 60, 71, 60, 71, 60, 99, 60, 71,   23, 23 },
  38. /* E  */ { 35, 60, 50, 60, 35, 60, 99, 60,   12, 12 },
  39. /* SE */ { 60, 71, 60, 71, 60, 71, 60, 99,   23, 23 },
  40. /* S  */ { 99, 60, 35, 60, 50, 60, 35, 60,   12, 12 },
  41. /* SW */ { 60, 99, 60, 71, 60, 71, 60, 71,   23, 23 },
  42. /* W  */ { 35, 60, 99, 60, 35, 60, 50, 60,   12, 12 },
  43. /* NW */ { 60, 71, 60, 99, 60, 71, 60, 71,   23, 23 },
  44.  
  45. /* OT */ { 12, 23, 12, 23, 12, 23, 12, 23,   99, 99 },
  46. /* OR */ { 99, 99, 99, 99, 99, 99, 99, 99,   99, 99 }
  47.     };
  48.  
  49. /* penalty for extraneous holes and corners, scaled by sharpness of turn */
  50. static int penalty[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  51.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  52. /* N  */ {  0,  5, 10, 15, 20, 15, 10,  5,   50, 0 },
  53. /* NE */ {  5,  0,  5, 10, 15, 20, 15, 10,   50, 0 },
  54. /* E  */ { 10,  5,  0,  5, 10, 15, 20, 15,   50, 0 },
  55. /* SE */ { 15, 10,  5,  0,  5, 10, 15, 20,   50, 0 },
  56. /* S  */ { 20, 15, 10,  5,  0,  5, 10, 15,   50, 0 },
  57. /* SW */ { 15, 20, 15, 10,  5,  0,  5, 10,   50, 0 },
  58. /* W  */ { 10, 15, 20, 15, 10,  5,  0,  5,   50, 0 },
  59. /* NW */ {  5, 10, 15, 20, 15, 10,  5,  0,   50, 0 },
  60.  
  61. /* OT */ { 50, 50, 50, 50, 50, 50, 50, 50,  100, 0 },
  62. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,    0, 0 }
  63.     };
  64.  
  65. /* penalty for Diaganal Routes */
  66. static int penalty2[3][10][10] =  /* OT=Otherside, OR=Origin (source) cell */
  67. {
  68. {        /* N, NE,  E, SE,  S, SW,  W, NW, OT, OR */
  69. /* N  */ {  0, 30, 30, 30, 30, 30, 30, 30,  0, 0 },
  70. /* NE */ { 30,  0, 30, 30, 30, 30, 30, 30,  0, 0 },
  71. /* E  */ { 30, 30,  0, 30, 30, 30, 30, 30,  0, 0 },
  72. /* SE */ { 30, 30, 30,  0, 30, 30, 30, 30,  0, 0 },
  73. /* S  */ { 30, 30, 30, 30,  0, 30, 30, 30,  0, 0 },
  74. /* SW */ { 30, 30, 30, 30, 30,  0, 30, 30,  0, 0 },
  75. /* W  */ { 30, 30, 30, 30, 30, 30,  0, 30,  0, 0 },
  76. /* NW */ { 30, 30, 30, 30, 30, 30, 30,  0,  0, 0 },
  77. /* OT */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 },
  78. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 }
  79. },
  80. {        /* N, NE,  E, SE,  S, SW,  W, NW, OT, OR */
  81. /* N  */ { 30, 30, 30, 30, 30, 30, 30, 30,  0, 0 },
  82. /* NE */ { 30,  0, 30, 30, 30, 30, 30, 30,  0, 0 },
  83. /* E  */ { 30, 30,  0, 30, 30, 30, 30, 30,  0, 0 },
  84. /* SE */ { 30, 30, 30,  0, 30, 30, 30, 30,  0, 0 },
  85. /* S  */ { 30, 30, 30, 30, 30, 30, 30, 30,  0, 0 },
  86. /* SW */ { 30, 30, 30, 30, 30,  0, 30, 30,  0, 0 },
  87. /* W  */ { 30, 30, 30, 30, 30, 30,  0, 30,  0, 0 },
  88. /* NW */ { 30, 30, 30, 30, 30, 30, 30,  0,  0, 0 },
  89. /* OT */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 },
  90. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 }
  91. },
  92. {        /* N, NE,  E, SE,  S, SW,  W, NW, OT, OR */
  93. /* N  */ {  0,  0,  0,  0, 30, 30, 30, 30,  0, 0 },
  94. /* NE */ { 30,  0, 30, 30, 30, 30, 30, 30,  0, 0 },
  95. /* E  */ { 30, 30, 30, 30, 30, 30, 30, 30,  0, 0 },
  96. /* SE */ { 30, 30, 30,  0, 30, 30, 30, 30,  0, 0 },
  97. /* S  */ { 30, 30, 30, 30,  0, 30, 30, 30,  0, 0 },
  98. /* SW */ { 30, 30, 30, 30, 30,  0, 30, 30,  0, 0 },
  99. /* W  */ { 30, 30, 30, 30, 30, 30, 30, 30,  0, 0 },
  100. /* NW */ { 30, 30, 30, 30, 30, 30, 30,  0,  0, 0 },
  101. /* OT */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 },
  102. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,  0, 0 }
  103. } };
  104.  
  105.  
  106. /*
  107. ** x is the direction to enter the cell of interest.
  108. ** y is the direction to exit the cell of interest.
  109. ** z is the direction to really exit the cell, if y=FROM_OTHERSIDE.
  110. **
  111. ** return the distance of the trace through the cell of interest.
  112. ** the calculation is driven by the tables above.
  113. */
  114.  
  115. int CalcDist ( x, y, z, layer ) /* calculate distance of a trace through a cell */
  116.     int x, y, z, layer;
  117.     {
  118.     int adjust;
  119.  
  120.     adjust = 0; /* set if hole is encountered */
  121.     if (x == EMPTY)
  122.         x = 10;
  123.     if (y == EMPTY)
  124.         y = 10;
  125.     else if (y == FROM_OTHERSIDE) {
  126.         if (z == EMPTY)
  127.             z = 10;
  128.         adjust = penalty[x-1][z-1];
  129.         }
  130.     if ( NoDiag )
  131.       adjust += penalty2[DoubleSided*(layer%2)+DoubleSided][x-1][y-1];
  132.     return( dist[x-1][y-1] + penalty[x-1][y-1] + adjust );
  133.     }
  134.