Advertisement
DEKTEN

MOGA_MATH

Jan 2nd, 2021
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1.  
  2. #include <assert.h> //:define NDEBUG to turn off assertions.
  3. #include  <stdio.h> //:printf(...)
  4.  
  5. //:SECTION:MOGA_MATH_FORMULA:================================://
  6. #define NO_NEED_TO_FLOOR /** No Code **/
  7. #define NO_INT_CAST      /** No Code **/
  8.  
  9.     int MogaMath( int x , int y )
  10.     {   return
  11.         (
  12.             (
  13.  
  14.                 /** Invert our index ordering from counting  **/
  15.                 /** up to counting down. @c_d : @count_down  **/
  16.                 ( 16 - 1 ) //:<-- Max Coordinate Value ? YES.
  17.  
  18.                 -
  19.                 (
  20.                     (  ((y + 2) % 2) * (3 - (x % 4))  )
  21.                     +
  22.                     (  ((y + 3) % 2) * (0 + (x % 4))  )
  23.                     +
  24.                     (                   4 * (y % 4)   )
  25.                 )
  26.             )
  27.  
  28.             +
  29.  
  30.             /** Quadrant Y axis multiplied by 32 **/
  31.             /** q_y * 32 **/
  32.             (NO_INT_CAST(NO_NEED_TO_FLOOR(y / 4)) * 32)
  33.  
  34.             +
  35.  
  36.             /** q_x * 16 **/
  37.             /** Quadrant X axis multiplied by 16 **/
  38.             (NO_INT_CAST(NO_NEED_TO_FLOOR(x / 4)) * 16)
  39.  
  40.         );;
  41.     }
  42.  
  43. #undef NO_NEED_TO_FLOOR /** No Code **/
  44. #undef NO_INT_CAST      /** No Code **/
  45. //:================================:SECTION:MOGA_MATH_FORMULA://
  46. //:SECTION:JOHN_MARK_MATH:===================================://
  47. #define QUAD_SPAN ( 2 ) /** 2x2 outer quadrants **/
  48. #define LED__SPAN ( 4 ) /** 4x4 pixels / LEDS per quadrant **/
  49. /** ********************************************************* **
  50.         Moga's table.
  51.         15 14 13 12 31 30 29 28
  52.         08 09 10 11 24 25 26 27
  53.         07 06 05 04 23 22 21 20
  54.         00 01 02 03 16 17 18 19
  55.         47 46 45 44 63 62 61 60
  56.         40 41 42 43 56 57 58 59
  57.         39 38 37 36 55 54 53 52
  58.         32 33 34 35 48 49 50 51
  59.  
  60.             |<--- QUAD_SPAN == 2 --->|
  61.             |                        |
  62.             |   q_x   |    |   q_x   |
  63.         ____|    0    |    |    1    |_______
  64.         |   15 14 13 12    31 30 29 28   |
  65.         0   08 09 10 11    24 25 26 27  LED__SPAN == 4
  66.         |   07 06 05 04    23 22 21 20   |
  67.         ____00 01 02 03    16 17 18 19_______
  68.              |              |
  69.             q_0            q_0
  70.  
  71.         ____
  72.         |   47 46 45 44    63 62 61 60
  73.         1   40 41 42 43    56 57 58 59
  74.         |   39 38 37 36    55 54 53 52
  75.         ____32 33 34 35    48 49 50 51
  76.              |              |
  77.             q_0            q_0
  78.  
  79.  
  80.         |<--- QUAD_SPAN == 2  --->|
  81.         +------------+------------+ ---
  82.         |            |            |  |
  83.         |  q_i == 0  |  q_i == 1  |  |
  84.         |  q_x == 0  |  q_x == 1  | LED__SPAN == 4
  85.         |  q_y == 0  |  q_y == 0  |  |
  86.         |            |            |  |
  87.         +------------+------------+ ---
  88.         |            |            |
  89.         |  q_i == 2  |  q_i == 3  |
  90.         |  q_x == 0  |  q_x == 1  |
  91.         |  q_y == 1  |  q_y == 1  |
  92.         |            |            |
  93.         +------------+------------+
  94.  
  95.  
  96.  
  97.  
  98. *** ******************************************************** **/
  99.  
  100.     int
  101.     JohnMath( int x , int y )
  102.     {
  103.         /** Input coords are over an 8x8 tilemap **/
  104.         assert( x >= 0 && x <= ( 8 - 1 ) );
  105.         assert( y >= 0 && y <= ( 8 - 1 ) );
  106.  
  107.         /** Use tilemap collsion math to figure out which **/
  108.         /** of the 4 sub cells we are in. Each sub cell   **/
  109.         /** measures 4x4 [ pixels / LED lights ]          **/
  110.         /** THEN run an xy-to-index formula to get the    **/
  111.         /** 1D representation of the quadrant location.   **/
  112.  
  113.         int q_x = ( x / 4 );
  114.         int q_y = ( y / 4 );
  115.         int q_i = q_x + ( QUAD_SPAN * q_y);
  116.  
  117.         assert( q_x >= 0 && q_x <= 1 );
  118.         assert( q_y >= 0 && q_y <= 1 );
  119.         assert( q_i >= 0 && q_i <= 3 );
  120.  
  121.         /** Get first index value of our snaking led pipe.  **/
  122.         int q_0 = ( q_i * 16 ); /** Base Value Of Our SNAKE **/
  123.  
  124.         /** Widdle down original global [x,y] to a  **/
  125.         /** cell-local [ l_x , l_y ] xy coordinate. **/
  126.         int l_x = ( x - ( q_x * 4 )  );
  127.         int l_y = ( y - ( q_y * 4 )  );
  128.         assert( l_x >= 0 && l_x <= 3 );
  129.         assert( l_y >= 0 && l_y <= 3 );
  130.  
  131.         /** convert [ l_x , l_y ] to it's 1D index form      **/
  132.         /** using traditional scanline ordering.             **/
  133.         /** left-to-right , then top-to-bottom.              **/
  134.         int l_i = l_x + ( LED__SPAN * l_y);
  135.  
  136.         int c_d = ( (16-1) - l_i ); /** @c_d : @count_down **/
  137.  
  138.         int f_d ; //:FLIPPED index, depending on odd/even row**/
  139.  
  140.         //:f_d = c_d;
  141.  
  142.         if(  0 == l_y % 2 ){
  143.        
  144.             /** Even row, don't flip this row. **/
  145.             f_d = c_d ;
  146.        
  147.         }else{
  148.             /** Odd row, FLIP THIS ROW. ............**/
  149.        
  150.             assert( c_d >= 0 && c_d <= 15 );
  151.            
  152.             /** Invert local Y axis ( 3 - l_y )     **/
  153.             /** Then multiply by stride (4)         **/
  154.             /** to get the first index on the LEFT. **/
  155.             /** Use this value to invert c_d        **/
  156.        
  157.             /** b_i : Base Index **/
  158.             int b_i = (  
  159.                 (3 - l_y) /** Invert Row Y     **/
  160.                 *
  161.                 4  /** time stride to get index **/
  162.             );;
  163.        
  164.             f_d = (  
  165.            
  166.                 (
  167.                     3 /** Flip Zeroed out X value **/
  168.                     -
  169.                     /** Zero out using base index **/
  170.                     ( c_d - b_i )
  171.                 )
  172.                 +
  173.                 b_i /** UNDO zeroing out when done. **/
  174.  
  175.             );;
  176.         };;
  177.  
  178.         /** Finally, f_d needs to have the     **/
  179.         /** global index offset applied to it. **/
  180.         int dex_out = f_d + q_0 ;
  181.  
  182.         return( dex_out );
  183.     }
  184.  
  185.  
  186. #undef QUAD_SPAN
  187. #undef LED__SPAN
  188. //:========================================:SECTION:JOHN_MARK://
  189.  
  190.  
  191. int main( void ){
  192.     printf("[BEG:main]\n");
  193.    
  194.     int      wid = (     8     ); /** 8 cells wide     **/
  195.     int      hig = (     8     ); /** 8 cells high     **/
  196.     int      num = ( wid * hig ); /** Number of pixels **/
  197.     int      p_i ; /** pixel index **/
  198.     int      p_x ; /** pixel X coord **/
  199.     int      p_y ; /** pixel Y coord **/
  200.     int  mog_dex ; /** MOGa's inDEX (1 dimensional? I think) **/
  201.    
  202.     printf("\n");
  203.     printf("[STANDARD_SCANLINE_ORDER_BELOW]:\n");
  204.  
  205.  
  206.     for( p_i = 0; p_i < num; p_i++ ){
  207.  
  208.         p_x =  p_i     % wid ;
  209.         if( 0 == p_x % 8){ printf("\n"); };
  210.         printf( "%02d" , p_i );
  211.         printf(  " "  );
  212.        
  213.     };;
  214.  
  215.     printf("\n\n\n");
  216.     printf("[MOGAS_REMAP]:\n");
  217.  
  218.    
  219.     for( p_i = 0; p_i < num; p_i++ ){
  220.  
  221.         /** I see you have 4 main quadrants each with  * * * **/
  222.         /** A layered spiral pattern? Maybe SPIRAL is  * * * **/
  223.         /** the wrong word... "Upward zig-zag" ?       * * * **/
  224.  
  225.         p_x =  p_i     % wid ;  
  226.         p_y = (p_i-p_x)/ wid ;  
  227.         mog_dex = MogaMath( p_x , p_y );
  228.         if( mog_dex ){ /** NOOP **/ };
  229.  
  230.         if( 0 == p_x % 8){ printf("\n"); };
  231.         printf( "%02d" , mog_dex );
  232.         printf(  " "  );
  233.        
  234.     };;
  235.    
  236.     printf("\n\n\n");
  237.     printf("[JOHNS_REMAP]:\n");
  238.  
  239.  
  240.     for( p_i = 0; p_i < num; p_i++ ){
  241.  
  242.         /** Copy this block but replace "MogaMath"        **/
  243.         /** with "JohnMath" and see if we get same thing. **/
  244.  
  245.         p_x =  p_i     % wid ;  
  246.         p_y = (p_i-p_x)/ wid ;  
  247.         mog_dex = JohnMath( p_x , p_y );
  248.         if( mog_dex ){ /** NOOP **/ };
  249.  
  250.         if( 0 == p_x % 8){ printf("\n"); };
  251.         printf( "%02d" , mog_dex );
  252.         printf(  " "  );
  253.        
  254.     };;
  255.  
  256.     printf("\n\n\n");
  257.     printf("[Asserting_Formulas_Have_Identical_Results...]\n");
  258.  
  259.  
  260.     /** Make absolutely sure our math matches **/
  261.     for( p_x = 0; p_x <= ( 8 -1 ); p_x ++ ){
  262.     for( p_y = 0; p_y <= ( 8 -1 ); p_y ++ ){
  263.  
  264.         assert( MogaMath( p_x , p_y ) == JohnMath( p_x , p_y ));
  265.  
  266.     };;};;
  267.    
  268.     printf("[END:main]\n");
  269. }
  270.  
  271.  
  272.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement