#!/usr/bin/env python3
import numpy as np
import matplotlib.pyplot as plt
from alive_progress import alive_bar

img_res_x = 500
img_res_y = 500
total_pixels = img_res_x * img_res_y # so we don't gotta compute it every time

periods = .25
square_x = 1
square_y = 1

xmin = (-periods * np.pi) + (square_x * np.pi)
xmax = (periods  * np.pi) + (square_x * np.pi)
ymin = (-periods * np.pi) + (square_y * np.pi)
ymax = (periods * np.pi) + (square_y * np.pi)

escape = 10000
iterations = 255*3
c_x = 2 * np.pi
c_y = 2 * np.pi


image = np.empty([img_res_y, img_res_x])

with alive_bar(img_res_y, bar = 'filling', spinner = 'waves') as bar:
  for pix_y, y in enumerate(np.linspace(ymin, ymax, img_res_y)):
    for pix_x, x in enumerate(np.linspace(xmin, xmax, img_res_x)):
      on_x = x
      on_y = y
      for i in range(iterations):
        completed_ratio = ((pix_y * img_res_y) + pix_x) / total_pixels
        next_x = (completed_ratio * (on_x/np.sin(on_y))) + ((1 - completed_ratio) * on_x/np.tan(on_y))
        on_y = (completed_ratio * (on_y/np.sin(on_x))) + ((1 - completed_ratio) * on_y/np.tan(on_x))
        on_x = next_x
        if on_x**2 + on_y**2 > escape:
          break
      print(completed_ratio)
      image[pix_y][pix_x] = i
    bar()


      

plt.style.use('dark_background')
# fuck this shit
fig = plt.figure(frameon=False)
fig.set_size_inches(img_res_x/fig.dpi, img_res_y/fig.dpi)
#fig.set_size_inches(width/height, 1, forward=False)

ax = plt.Axes(fig, [0., 0., 1., 1.])
ax.set_axis_off()
fig.add_axes(ax)

cmap = plt.cm.viridis
cmap.set_bad((0,0,0))
cmap.set_over((0,0,0))
cmap.set_under((0,0,0))

ax.imshow(image, norm="log", aspect="auto", cmap=cmap)
fig.savefig("linear_transform_sin_tan_arnolds_tongue_hotspot.png")
plt.show()
