home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 373.lha / route_v1.0 / src / dist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-04-25  |  3.8 KB  |  106 lines

  1. #include <stdio.h>
  2. #include "cell.h"
  3.  
  4. int GetApxDist ( r1, c1, r2, c2 ) /* calculate approximate distance */
  5.     int r1, c1, r2, c2;
  6.     {
  7.     register int d1, d2; /* row and column deltas */
  8.     int d0; /* temporary variable for swapping d1 and d2 */
  9.  
  10.     /* NOTE: the -25 used below is because we are not going from the   */
  11.     /* center of (r1,c1) to the center of (r2,c2), we are going from   */
  12.     /* the edge of a hole at (r1,c1) to the edge of a hole at (r2,c2). */
  13.     /* holes are 25 mils in diameter (12.5 mils in radius), so we back */
  14.     /* off by 2 radii.                           */
  15.     if ((d1 = r1-r2) < 0) /* get absolute row delta */
  16.         d1 = -d1;
  17.     if ((d2 = c1-c2) < 0) /* get absolute column delta */
  18.         d2 = -d2;
  19.     if (!d1) /* in same row? */
  20.         return( (d2*50)-25 ); /* 50 mils per cell */
  21.     if (!d2) /* in same column? */
  22.         return( (d1*50)-25 ); /* 50 mils per cell */
  23.     if (d1 > d2) { /* get smaller into d1 */
  24.         d0 = d1;
  25.         d1 = d2;
  26.         d2 = d0;
  27.         }
  28.     d2 -= d1; /* get non-diagonal part of approximate "route" */
  29.     return( (d1*71)+(d2*50)-25 ); /* 71 mils diagonally per cell */
  30.     }
  31.  
  32. /* distance to go thru a cell */
  33. static int dist[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  34.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  35. /* N  */ { 50, 60, 35, 60, 99, 60, 35, 60,   12, 12 },
  36. /* NE */ { 60, 71, 60, 71, 60, 99, 60, 71,   23, 23 },
  37. /* E  */ { 35, 60, 50, 60, 35, 60, 99, 60,   12, 12 },
  38. /* SE */ { 60, 71, 60, 71, 60, 71, 60, 99,   23, 23 },
  39. /* S  */ { 99, 60, 35, 60, 50, 60, 35, 60,   12, 12 },
  40. /* SW */ { 60, 99, 60, 71, 60, 71, 60, 71,   23, 23 },
  41. /* W  */ { 35, 60, 99, 60, 35, 60, 50, 60,   12, 12 },
  42. /* NW */ { 60, 71, 60, 99, 60, 71, 60, 71,   23, 23 },
  43.  
  44. /* OT */ { 12, 23, 12, 23, 12, 23, 12, 23,   99, 99 },
  45. /* OR */ { 99, 99, 99, 99, 99, 99, 99, 99,   99, 99 }
  46.     };
  47.  
  48. /* distance around (circular) segment of hole */
  49. static int circ[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  50.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  51. /* N  */ { 39, 29, 20, 10,  0, 10, 20, 29,   99, 0 },
  52. /* NE */ { 29, 39, 29, 20, 10,  0, 10, 20,   99, 0 },
  53. /* E  */ { 20, 29, 39, 29, 20, 10,  0, 10,   99, 0 },
  54. /* SE */ { 10, 20, 29, 39, 29, 20, 10,  0,   99, 0 },
  55. /* S  */ {  0, 10, 20, 29, 39, 29, 20, 10,   99, 0 },
  56. /* SW */ { 10,  0, 10, 20, 29, 39, 29, 20,   99, 0 },
  57. /* W  */ { 20, 10,  0, 10, 20, 29, 39, 29,   99, 0 },
  58. /* NW */ { 29, 20, 10,  0, 10, 20, 29, 39,   99, 0 },
  59.  
  60. /* OT */ { 99, 99, 99, 99, 99, 99, 99, 99,   99, 0 },
  61. /* OR */ { 99, 99, 99, 99, 99, 99, 99, 99,   99, 0 }
  62.     };
  63.  
  64. /* penalty for extraneous holes and corners, scaled by sharpness of turn */
  65. static int penalty[10][10] = { /* OT=Otherside, OR=Origin (source) cell */
  66.      /* N, NE,  E, SE,  S, SW,  W, NW,   OT, OR */
  67. /* N  */ {  0,  5, 10, 15, 20, 15, 10,  5,   50, 0 },
  68. /* NE */ {  5,  0,  5, 10, 15, 20, 15, 10,   50, 0 },
  69. /* E  */ { 10,  5,  0,  5, 10, 15, 20, 15,   50, 0 },
  70. /* SE */ { 15, 10,  5,  0,  5, 10, 15, 20,   50, 0 },
  71. /* S  */ { 20, 15, 10,  5,  0,  5, 10, 15,   50, 0 },
  72. /* SW */ { 15, 20, 15, 10,  5,  0,  5, 10,   50, 0 },
  73. /* W  */ { 10, 15, 20, 15, 10,  5,  0,  5,   50, 0 },
  74. /* NW */ {  5, 10, 15, 20, 15, 10,  5,  0,   50, 0 },
  75.  
  76. /* OT */ { 50, 50, 50, 50, 50, 50, 50, 50,  100, 0 },
  77. /* OR */ {  0,  0,  0,  0,  0,  0,  0,  0,    0, 0 }
  78.     };
  79.  
  80. /*
  81. ** x is the direction to enter the cell of interest.
  82. ** y is the direction to exit the cell of interest.
  83. ** z is the direction to really exit the cell, if y=FROM_OTHERSIDE.
  84. **
  85. ** return the distance of the trace through the cell of interest.
  86. ** the calculation is driven by the tables above.
  87. */
  88.  
  89. int CalcDist ( x, y, z ) /* calculate distance of a trace through a cell */
  90.     int x, y, z;
  91.     {
  92.     int adjust;
  93.  
  94.     adjust = 0; /* set if hole is encountered */
  95.     if (x == EMPTY)
  96.         x = 10;
  97.     if (y == EMPTY)
  98.         y = 10;
  99.     else if (y == FROM_OTHERSIDE) {
  100.         if (z == EMPTY)
  101.             z = 10;
  102.         adjust = circ[x-1][z-1] + penalty[x-1][z-1];
  103.         }
  104.     return( dist[x-1][y-1] + penalty[x-1][y-1] + adjust );
  105.     }
  106.