Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h> //:define NDEBUG to turn off assertions.
- #include <stdio.h> //:printf(...)
- //:SECTION:MOGA_MATH_FORMULA:================================://
- #define NO_NEED_TO_FLOOR /** No Code **/
- #define NO_INT_CAST /** No Code **/
- int MogaMath( int x , int y )
- { return
- (
- (
- /** Invert our index ordering from counting **/
- /** up to counting down. @c_d : @count_down **/
- ( 16 - 1 ) //:<-- Max Coordinate Value ? YES.
- -
- (
- ( ((y + 2) % 2) * (3 - (x % 4)) )
- +
- ( ((y + 3) % 2) * (0 + (x % 4)) )
- +
- ( 4 * (y % 4) )
- )
- )
- +
- /** Quadrant Y axis multiplied by 32 **/
- /** q_y * 32 **/
- (NO_INT_CAST(NO_NEED_TO_FLOOR(y / 4)) * 32)
- +
- /** q_x * 16 **/
- /** Quadrant X axis multiplied by 16 **/
- (NO_INT_CAST(NO_NEED_TO_FLOOR(x / 4)) * 16)
- );;
- }
- #undef NO_NEED_TO_FLOOR /** No Code **/
- #undef NO_INT_CAST /** No Code **/
- //:================================:SECTION:MOGA_MATH_FORMULA://
- //:SECTION:JOHN_MARK_MATH:===================================://
- #define QUAD_SPAN ( 2 ) /** 2x2 outer quadrants **/
- #define LED__SPAN ( 4 ) /** 4x4 pixels / LEDS per quadrant **/
- /** ********************************************************* **
- Moga's table.
- 15 14 13 12 31 30 29 28
- 08 09 10 11 24 25 26 27
- 07 06 05 04 23 22 21 20
- 00 01 02 03 16 17 18 19
- 47 46 45 44 63 62 61 60
- 40 41 42 43 56 57 58 59
- 39 38 37 36 55 54 53 52
- 32 33 34 35 48 49 50 51
- |<--- QUAD_SPAN == 2 --->|
- | |
- | q_x | | q_x |
- ____| 0 | | 1 |_______
- | 15 14 13 12 31 30 29 28 |
- 0 08 09 10 11 24 25 26 27 LED__SPAN == 4
- | 07 06 05 04 23 22 21 20 |
- ____00 01 02 03 16 17 18 19_______
- | |
- q_0 q_0
- ____
- | 47 46 45 44 63 62 61 60
- 1 40 41 42 43 56 57 58 59
- | 39 38 37 36 55 54 53 52
- ____32 33 34 35 48 49 50 51
- | |
- q_0 q_0
- |<--- QUAD_SPAN == 2 --->|
- +------------+------------+ ---
- | | | |
- | q_i == 0 | q_i == 1 | |
- | q_x == 0 | q_x == 1 | LED__SPAN == 4
- | q_y == 0 | q_y == 0 | |
- | | | |
- +------------+------------+ ---
- | | |
- | q_i == 2 | q_i == 3 |
- | q_x == 0 | q_x == 1 |
- | q_y == 1 | q_y == 1 |
- | | |
- +------------+------------+
- *** ******************************************************** **/
- int
- JohnMath( int x , int y )
- {
- /** Input coords are over an 8x8 tilemap **/
- assert( x >= 0 && x <= ( 8 - 1 ) );
- assert( y >= 0 && y <= ( 8 - 1 ) );
- /** Use tilemap collsion math to figure out which **/
- /** of the 4 sub cells we are in. Each sub cell **/
- /** measures 4x4 [ pixels / LED lights ] **/
- /** THEN run an xy-to-index formula to get the **/
- /** 1D representation of the quadrant location. **/
- int q_x = ( x / 4 );
- int q_y = ( y / 4 );
- int q_i = q_x + ( QUAD_SPAN * q_y);
- assert( q_x >= 0 && q_x <= 1 );
- assert( q_y >= 0 && q_y <= 1 );
- assert( q_i >= 0 && q_i <= 3 );
- /** Get first index value of our snaking led pipe. **/
- int q_0 = ( q_i * 16 ); /** Base Value Of Our SNAKE **/
- /** Widdle down original global [x,y] to a **/
- /** cell-local [ l_x , l_y ] xy coordinate. **/
- int l_x = ( x - ( q_x * 4 ) );
- int l_y = ( y - ( q_y * 4 ) );
- assert( l_x >= 0 && l_x <= 3 );
- assert( l_y >= 0 && l_y <= 3 );
- /** convert [ l_x , l_y ] to it's 1D index form **/
- /** using traditional scanline ordering. **/
- /** left-to-right , then top-to-bottom. **/
- int l_i = l_x + ( LED__SPAN * l_y);
- int c_d = ( (16-1) - l_i ); /** @c_d : @count_down **/
- int f_d ; //:FLIPPED index, depending on odd/even row**/
- //:f_d = c_d;
- if( 0 == l_y % 2 ){
- /** Even row, don't flip this row. **/
- f_d = c_d ;
- }else{
- /** Odd row, FLIP THIS ROW. ............**/
- assert( c_d >= 0 && c_d <= 15 );
- /** Invert local Y axis ( 3 - l_y ) **/
- /** Then multiply by stride (4) **/
- /** to get the first index on the LEFT. **/
- /** Use this value to invert c_d **/
- /** b_i : Base Index **/
- int b_i = (
- (3 - l_y) /** Invert Row Y **/
- *
- 4 /** time stride to get index **/
- );;
- f_d = (
- (
- 3 /** Flip Zeroed out X value **/
- -
- /** Zero out using base index **/
- ( c_d - b_i )
- )
- +
- b_i /** UNDO zeroing out when done. **/
- );;
- };;
- /** Finally, f_d needs to have the **/
- /** global index offset applied to it. **/
- int dex_out = f_d + q_0 ;
- return( dex_out );
- }
- #undef QUAD_SPAN
- #undef LED__SPAN
- //:========================================:SECTION:JOHN_MARK://
- int main( void ){
- printf("[BEG:main]\n");
- int wid = ( 8 ); /** 8 cells wide **/
- int hig = ( 8 ); /** 8 cells high **/
- int num = ( wid * hig ); /** Number of pixels **/
- int p_i ; /** pixel index **/
- int p_x ; /** pixel X coord **/
- int p_y ; /** pixel Y coord **/
- int mog_dex ; /** MOGa's inDEX (1 dimensional? I think) **/
- printf("\n");
- printf("[STANDARD_SCANLINE_ORDER_BELOW]:\n");
- for( p_i = 0; p_i < num; p_i++ ){
- p_x = p_i % wid ;
- if( 0 == p_x % 8){ printf("\n"); };
- printf( "%02d" , p_i );
- printf( " " );
- };;
- printf("\n\n\n");
- printf("[MOGAS_REMAP]:\n");
- for( p_i = 0; p_i < num; p_i++ ){
- /** I see you have 4 main quadrants each with * * * **/
- /** A layered spiral pattern? Maybe SPIRAL is * * * **/
- /** the wrong word... "Upward zig-zag" ? * * * **/
- p_x = p_i % wid ;
- p_y = (p_i-p_x)/ wid ;
- mog_dex = MogaMath( p_x , p_y );
- if( mog_dex ){ /** NOOP **/ };
- if( 0 == p_x % 8){ printf("\n"); };
- printf( "%02d" , mog_dex );
- printf( " " );
- };;
- printf("\n\n\n");
- printf("[JOHNS_REMAP]:\n");
- for( p_i = 0; p_i < num; p_i++ ){
- /** Copy this block but replace "MogaMath" **/
- /** with "JohnMath" and see if we get same thing. **/
- p_x = p_i % wid ;
- p_y = (p_i-p_x)/ wid ;
- mog_dex = JohnMath( p_x , p_y );
- if( mog_dex ){ /** NOOP **/ };
- if( 0 == p_x % 8){ printf("\n"); };
- printf( "%02d" , mog_dex );
- printf( " " );
- };;
- printf("\n\n\n");
- printf("[Asserting_Formulas_Have_Identical_Results...]\n");
- /** Make absolutely sure our math matches **/
- for( p_x = 0; p_x <= ( 8 -1 ); p_x ++ ){
- for( p_y = 0; p_y <= ( 8 -1 ); p_y ++ ){
- assert( MogaMath( p_x , p_y ) == JohnMath( p_x , p_y ));
- };;};;
- printf("[END:main]\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement