//#include <math.h>
#define PI 3.141592653589793115997963468544185161590576171875

double cosecant_single(double a, double b) { return a / cos(b); }
double secant_single(double a, double b) { return a / cos(b); }

/**
__kernel void render_frame_orbit_trap(__global double *frame_output, __global double *mask, 
    double x_step, double y_step,
    double x_start, double y_start,
    unsigned int iterations, unsigned int escape, double ratio) {  
  
  const vec2 point = (0, 0);
  
  unsigned int result;
  unsigned int iter;

  double min_distance = DBL_MAX; 
  
  for(iter = 0; iter < iterations; iter++) { 
    double next_x;
    double r = mask[img_index];
    next_x = (r * cosecant_single(x, y)) + ((1 - r) * secant_single(x, y));
    y = (r * cosecant_single(y, x)) + ((1 - r) * secant_single(y, x));
    x = next_x;
    if((pow(x, 2) + pow(y, 2)) >= escape) break;
  }
  
  frame_output[img_index] = total_curve / iter;

}
**/
 

__kernel void render_frame_curvature(__global double *frame_output, __global double *mask, 
    double x_step, double y_step,
    double x_start, double y_start,
    unsigned int iterations, unsigned int escape, double ratio) {  
  unsigned int result;
  unsigned int iter;
  
  double x_cart = (get_global_id(0) * x_step) + x_start;
  double y_cart = (get_global_id(1) * y_step) + y_start;
  double x = sqrt(pow(x_cart, 2) + pow(y_cart, 2));
  double y = atan(y_cart / x_cart);
 
  //double x = (get_global_id(0) * x_step) + x_start;
  //double y = (get_global_id(1) * y_step) + y_start;
  size_t img_index = (get_global_id(1) * get_global_size(1)) + get_global_id(0);

  double total_curve = 0;
  
  //just found out vectors are a thing
  double2 lp[3];
  
  for(iter = 0; iter < iterations; iter++) { 
    double next_x;
    double r = mask[img_index];
    next_x = (r * secant_single(x, y)) + ((1 - r) * cosecant_single(x, y));
    y = (r * secant_single(y, x)) + ((1 - r) * cosecant_single(y, x));
    x = next_x;

    if((pow(x, 2) + pow(y, 2)) >= escape) break;
    lp[2] = lp[1];
    lp[1] = lp[0];
    lp[0] = (double2)(x, y); //why do I have to cast this? hope no accuracy lost
      
    /**
    if(img_index == 254234) {
      printf("%u\n", ratio);
    }
    **/
    if(iter >= 2) {
      double curl = acos(dot((lp[0] - lp[1]), (lp[2] - lp[1])) / (distance(lp[0], lp[1]) * distance(lp[2], lp[1])));
      total_curve = (total_curve + curl);
    }
  }
  frame_output[img_index] = total_curve / iter;
}


__kernel void render_frame(__global double *frame_output, __global double *mask, 
    double x_step, double y_step,
    double x_start, double y_start,
    unsigned int iterations, unsigned int escape, double ratio) {  
  unsigned int result;
  double x_cart = (get_global_id(0) * x_step) + x_start;
  double y_cart = (get_global_id(1) * y_step) + y_start;
  size_t img_index = (get_global_id(1) * get_global_size(1)) + get_global_id(0);

  double x = sqrt(pow(x_cart, 2) + pow(y_cart, 2));
  double y = atan(y_cart / x_cart);
  
  //double x = (get_global_id(0) * x_step) + x_start;
  //double y = (get_global_id(1) * y_step) + y_start;
  

  unsigned int iter;

  //vec2 pos; 
  
  for(iter = 0; iter < iterations; iter++) { 
    double next_x;
    double r = ratio;
    next_x = (r * cosecant_single(x, y)) + ((1 - r) * secant_single(x, y));
    y = (r * cosecant_single(y, x)) + ((1 - r) * secant_single(y, x));
    x = next_x;
    //if(distance(pos) >= escape) break;
    if((pow(x, 2) + pow(y, 2)) >= escape) break;
  }
  

  //TODO turn back to int whenever 
  frame_output[img_index] = (double)iter;
  //frame_output[img_index] = mask[img_index] * 255;
}
