package xaero.rotatenjump.misc;

/**
 * Created by xaero on 09/07/2017.
 */

public class Transformations {

    /*
     * Rotates 'x' and 'y' coordinates by 'a' degrees around point (0,0)
     */
    public static float[] rotate(float x, float y, float a){//uses new RAM
        float[] dest = new float[] {0, 0};
        rotate(x, y, a, dest);
        return dest;
    }
    public static void rotate(float x, float y, float a, float[] dest){//doesn't use new RAM
        dest[0] = (float) (x * Math.cos(Math.toRadians(a)) - y * Math.sin(Math.toRadians(a)));
        dest[1] = (float) (y * Math.cos(Math.toRadians(a)) + x * Math.sin(Math.toRadians(a)));
    }

    public static void reflect(float x, float y, float nx, float ny, float[] dest){//doesn't use new RAM
        normalize(x, y, dest);
        x = dest[0];
        y = dest[1];
        dest[0] = x * (2 * nx * nx - 1) + 2 * nx * ny * y;//from finding coordinates on normal and normal_right axises using a system
        dest[1] = y * (2 * ny * ny - 1) + 2 * nx * ny * x;
    }

    /*
     * Gets the angle at which the coordinates are to vector (1,0)
     */
    public static double getAngle(float x, float y){
        if(x == 0)
            x = 0.00000001f;
        double at = Math.atan(y / x);
        if(x < 0)
            at += Math.PI;
        return at;
    }

    public static float wrap(float a, float b){
        if(a > 0)
            return a % b;
        else
            return a % b + b;
    }

    public static float getSinAnimation(double stage, double max){
        return (float)((1.0 + Math.sin(Math.PI * (stage / max - 0.5))) / 2.0);
    }

    public static void normalize(float x, float y, float[] dest){
        float len = (float)Math.sqrt(x * x + y * y);
        dest[0] = x / len;
        dest[1] = y / len;
    }

    private static StringBuilder[] stringBuilders = {new StringBuilder(), new StringBuilder()};//for update and render threads
    public static final int UPDATE_THREAD = 0;
    public static final int RENDER_THREAD = 1;

    public static String intToString(int num, int thread){
        return intToString(null, num, thread);
    }

    public static String intToString(String prefix, int num, int thread){
        StringBuilder builder = stringBuilders[thread];
        builder.setLength(0);
        if(prefix != null)
            builder.append(prefix);
        builder.append(num);
        return builder.toString();
    }

}
