Advertisement
Guest User

Affine.java

a guest
Feb 6th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1. //#ifdef JAVA_ONLY
  2. package cross;
  3.  
  4. import engine.M;
  5.  
  6. public class Affine
  7. {
  8.     public static final int STACK_SIZE = 100;
  9.     public static float[] stack = new float[STACK_SIZE * 16];
  10.     public static float[] inverseStack = new float[16];
  11.     private static boolean mbRebuildInverse = true;
  12.     public static int stackPos = 0;
  13.     public static float outX;
  14.     public static float outY;
  15.     public static float[] identityStack = new float[] {
  16.             1.0f, 0.0f, 0.0f, 0.0f,
  17.             0.0f, 1.0f, 0.0f, 0.0f,
  18.             0.0f, 0.0f, 1.0f, 0.0f,
  19.             0.0f, 0.0f, 0.0f, 1.0f
  20.     };
  21.  
  22.     /*
  23.      * a b 0 u   0  1  2  3
  24.      * c d 0 v   4  5  6  7
  25.      * 0 0 1 0   8  9 10 11
  26.      * x y 0 1  12 13 14 15
  27.      */
  28.  
  29.     public static void push()
  30.     {
  31.         if (stackPos == (STACK_SIZE - 1) * 16)
  32.         {
  33.             System.out.println("Affine Stack Overflow!  Increase Affine.STACK_SIZE!");
  34.             return;
  35.         }
  36.         for (int i = 0; i < 16; i++)
  37.         {
  38.             stack[stackPos + i + 16] = stack[stackPos + i];
  39.         }
  40.         stackPos += 16;
  41.     }
  42.  
  43.     public static void pop()
  44.     {
  45.         if (stackPos == 0)
  46.         {
  47.             System.out.println("Affine Stack Underflow! Too many GFX.pop()'s?");
  48.             return;
  49.         }
  50.         stackPos -= 16;
  51.         mbRebuildInverse = true;
  52.     }
  53.  
  54.     public static void identity()
  55.     {
  56.         for (int i = 0; i < 16; i++)
  57.         {
  58.             stack[stackPos + i] = identityStack[i];
  59.         }
  60.         mbRebuildInverse = true;
  61.     }
  62.  
  63.     public static void rotate(float _angle)
  64.     {
  65.         float sin = M.sin(M.deg2rad(_angle));
  66.         float cos = M.cos(M.deg2rad(_angle));
  67.         float a = stack[stackPos + 0];
  68.         float b = stack[stackPos + 1];
  69.         float c = stack[stackPos + 4];
  70.         float d = stack[stackPos + 5];
  71.         stack[stackPos + 0] = cos * a + sin * c;
  72.         stack[stackPos + 1] = cos * b + sin * d;
  73.         stack[stackPos + 4] = -sin * a + cos * c;
  74.         stack[stackPos + 5] = -sin * b + cos * d;
  75.         mbRebuildInverse = true;
  76.     }
  77.  
  78.     public static void translate(float _x, float _y)
  79.     {
  80.         final float a = stack[stackPos + 0];
  81.         final float b = stack[stackPos + 1];
  82.         final float c = stack[stackPos + 4];
  83.         final float d = stack[stackPos + 5];
  84.         final float tx = stack[stackPos + 12];
  85.         final float ty = stack[stackPos + 13];
  86.         stack[stackPos + 12] = a * _x + c * _y + tx;
  87.         stack[stackPos + 13] = b * _x + d * _y + ty;
  88.         mbRebuildInverse = true;
  89.     }
  90.  
  91.     public static void scale(float _sx, float _sy)
  92.     {
  93.         stack[stackPos + 0] *= _sx;
  94.         stack[stackPos + 1] *= _sx;
  95.         stack[stackPos + 4] *= _sy;
  96.         stack[stackPos + 5] *= _sy;
  97.         mbRebuildInverse = true;
  98.     }
  99.  
  100.     public static void skew(float _sx, float _sy)
  101.     {
  102.         final float a = stack[stackPos + 0];
  103.         final float b = stack[stackPos + 1];
  104.         final float c = stack[stackPos + 4];
  105.         final float d = stack[stackPos + 5];
  106.         stack[stackPos + 0] = _sx * c;
  107.         stack[stackPos + 1] = _sx * d;
  108.         stack[stackPos + 4] = _sy * a;
  109.         stack[stackPos + 5] = _sy * b;
  110.         mbRebuildInverse = true;
  111.     }
  112.  
  113.     public static void localToGlobal(float _x, float _y)
  114.     {
  115.         final float a = stack[stackPos + 0];
  116.         final float b = stack[stackPos + 1];
  117.         final float c = stack[stackPos + 4];
  118.         final float d = stack[stackPos + 5];
  119.         final float tx = stack[stackPos + 12];
  120.         final float ty = stack[stackPos + 13];
  121.         outX = _x * a + _y * c + tx;
  122.         outY = _x * b + _y * d + ty;
  123.     }
  124.  
  125.     public static void globalToLocal(float _x, float _y)
  126.     {
  127.         updateInverse();
  128.         final float a = inverseStack[0];
  129.         final float b = inverseStack[1];
  130.         final float c = inverseStack[4];
  131.         final float d = inverseStack[5];
  132.         final float tx = inverseStack[12];
  133.         final float ty = inverseStack[13];
  134.         outX = _x * a + _y * c + tx;
  135.         outY = _x * b + _y * d + ty;
  136.     }
  137.  
  138.     public static void updateInverse()
  139.     {
  140.         if (mbRebuildInverse)
  141.         {
  142.             mbRebuildInverse = false;
  143.             final float a = stack[stackPos + 0];
  144.             final float b = stack[stackPos + 1];
  145.             final float c = stack[stackPos + 4];
  146.             final float d = stack[stackPos + 5];
  147.             final float tx = stack[stackPos + 12];
  148.             final float ty = stack[stackPos + 13];
  149.             final float adbc = (a * d - b * c);
  150.  
  151.             inverseStack[0] = d / adbc; // a
  152.             inverseStack[1] = -b / adbc; // b
  153.             inverseStack[4] = -c / adbc; // c
  154.             inverseStack[5] = a / adbc; // d
  155.             inverseStack[12] = (c * ty - d * tx) / adbc; // tx
  156.             inverseStack[13] = -(a * ty - b * tx) / adbc; // ty
  157.         }
  158.     }
  159.  
  160. }
  161. //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement