Guest User

Untitled

a guest
Dec 19th, 2017
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.94 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from math import sqrt
  4. #from gmpy2 import is_prime
  5. from PIL import Image
  6. from pyglet.gl import *
  7. from time import time
  8. import pyglet
  9.  
  10. class TheEnd(pyglet.window.Window):
  11.     def __init__(self, x, y, i_max):
  12.         config = Config(double_buffer=True)
  13.         super().__init__(x, y, resizable=True, config=config, vsync=False)
  14.  
  15.         self.rotate = True
  16.         self.sensitivity = 0.2
  17.         self.show_fps = True
  18.  
  19.         self.x = 0
  20.         self.y = 0
  21.         self.z = -500
  22.  
  23.         self.rx = 0
  24.         self.ry = 0
  25.         self.rz = 0
  26.  
  27.         glClearColor(0, 0, 0, 0)
  28.  
  29.         print("Creating the end...")
  30.  
  31.         theend = {}
  32.  
  33.         for i in range(i_max):
  34.              for j in range(i):
  35.                   a = i - j
  36.                   b = i + j
  37.                   c = a * b
  38.                   d = int(sqrt(c))
  39.                   e = int(c - (d * d))
  40.                   f = int(e - ((2 * d) + 1))
  41.                   n = int(i - d)
  42.                   x = int(d - a)
  43.                   t = int((x + 1) / 2) if e % 1 else int((x + 2) / 2)
  44.  
  45.                   #if not is_prime(a) and not is_prime(b):
  46.                   #     continue
  47.  
  48.                   if e not in theend:
  49.                        theend[e] = {}
  50.                   if n not in theend[e]:
  51.                       theend[e][n] = {}
  52.                   if t not in theend[e][n]:
  53.                       theend[e][n][t] = []
  54.  
  55.                   if f not in theend:
  56.                       theend[f] = {}
  57.                   if n - 1 not in theend[f]:
  58.                       theend[f][n - 1] = {}
  59.                   if t not in theend[f][n - 1]:
  60.                       theend[f][n - 1][t] = []
  61.  
  62.                   theend[e][n][t] = [e, n, d, x, a, b, c]
  63.                   theend[f][n - 1][t] = [f, n - 1, d + 1, x + 1, a, b, c]
  64.  
  65.         print("Generating vertexes...")
  66.  
  67.         points = []
  68.         points_rgb = []
  69.         lines = []
  70.         lines_rgb = []
  71.  
  72.         for e in theend:
  73.             for n in theend[e]:
  74.                 for t in theend[e][n]:
  75.                     points.extend([e, n, d])
  76.                     points_rgb.extend([128, 128, 128])
  77.  
  78.                     _, _, _, _, _, _, C = theend[e][n][t]
  79.  
  80.                     _e = e
  81.                     _t = 1
  82.  
  83.                     if 1 not in theend[e] or _t not in theend[e][1]:
  84.                         break
  85.                     _, _, d, x, a, b, c = theend[e][1][_t]
  86.                     for i in range(100):
  87.                         _t += 1
  88.                         x += 2
  89.                         a = b
  90.                         b = a + x * 2 + n * 2
  91.                         d = a + x
  92.                         c = a * b
  93.  
  94.                         if C % a == 0:
  95.                             break
  96.  
  97.                     p = a
  98.                     i = 1
  99.                     _t = 1
  100.                     while True:
  101.                         _t += p
  102. #                        _t = (i * p + 1) - _t
  103.  
  104.                         _e, _n, _d, _x, _a, _b, _c = self.ent(e, 1, _t)
  105.  
  106.                         if _c == C:
  107.                             lines.extend([e, n, d])
  108.                             lines.extend([_e, _n, _d])
  109.                             lines_rgb.extend([255, 0, 0])
  110.                             lines_rgb.extend([255, 165, 0])
  111.                             break
  112.  
  113.                         if _c > C:
  114.                             break
  115.  
  116.                         i += 1
  117.  
  118.         print("Done")
  119.  
  120.         self.batch = pyglet.graphics.Batch()
  121.         self.batch.add(len(points)//3, GL_POINTS, None, ("v3i", points), ("c3B", points_rgb))
  122.         self.batch.add(len(lines)//3, GL_LINES, None, ("v3i", lines), ("c3B", lines_rgb))
  123.  
  124.         self.fps = pyglet.clock.ClockDisplay()
  125.         pyglet.clock.schedule(self.update)
  126.  
  127.     def ent(self, e, n, t):
  128.         if e == 0:
  129.             x1 = 2
  130.             d1 = 0
  131.             d2 = t+1
  132.             d3 = t
  133.         elif e % 2 == 0:
  134.             x1 = 0
  135.             d1 = int(e/2)
  136.             d2 = t
  137.             d3 = t-1
  138.         else:
  139.             x1 = 1
  140.             d1 = int((e+3)/2)
  141.             d2 = t+1
  142.             d3 = t-1
  143.         dt = 2 * d3 * d2 + d1
  144.         xt = 2 * (t - 1) + x1
  145.         ct = dt * dt + e;
  146.         at = dt - xt;
  147.         if at == 0:
  148.             return None
  149.         bt = int(ct / at);
  150.         return [e, n, dt, xt, at, bt, ct]
  151.  
  152.     def on_resize(self, width, height):
  153.         glViewport(0, 0, width, height)
  154.         return pyglet.event.EVENT_HANDLED
  155.  
  156.     def on_draw(self):
  157.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  158.  
  159.         glMatrixMode(GL_PROJECTION)
  160.         glLoadIdentity()
  161.         gluPerspective(60.0, float(self.width / self.height), 0.1, 10000.0)
  162.         glMatrixMode(GL_MODELVIEW)
  163.         glLoadIdentity()
  164.         glTranslatef(0, 0, self.z)
  165.         glTranslatef(self.x, self.y, 0)
  166.         glRotatef(self.rz, 0, 0, 1)
  167.         glRotatef(self.ry, 0, 1, 0)
  168.         glRotatef(self.rx, 1, 0, 0)
  169.         self.draw()
  170.  
  171.         if self.show_fps:
  172.             glMatrixMode(GL_PROJECTION)
  173.             glLoadIdentity()
  174.             gluOrtho2D(0, self.width, 0, self.height)
  175.             glMatrixMode(GL_MODELVIEW)
  176.             glLoadIdentity()
  177.             self.fps.draw()
  178.  
  179.         return pyglet.event.EVENT_HANDLED
  180.  
  181.     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
  182.         if buttons == pyglet.window.mouse.LEFT:
  183.             self.x += dx * self.sensitivity
  184.             self.y += dy * self.sensitivity
  185.         elif buttons == pyglet.window.mouse.RIGHT:
  186.             self.rx += dy * self.sensitivity
  187.             self.ry += dx * self.sensitivity
  188.         return pyglet.event.EVENT_HANDLED
  189.  
  190.     def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
  191.         self.z += scroll_y * 10
  192.         return pyglet.event.EVENT_HANDLED
  193.  
  194.     def on_key_press(self, symbol, modifier):
  195.         if symbol == pyglet.window.key.SPACE:
  196.             epoch = int(time())
  197.             name = "end_{}.png".format(epoch)
  198.             self.save(name)
  199.         if symbol == pyglet.window.key.F:
  200.             self.show_fps = not self.show_fps
  201.         if symbol == pyglet.window.key.R:
  202.             self.rotate = not self.rotate
  203.         return pyglet.event.EVENT_HANDLED
  204.  
  205.     def save(self, name):
  206.         buf = (GLubyte * (self.width * self.height * 3))(0)
  207.         glReadPixels(0, 0, self.width, self.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf)
  208.         img = Image.frombytes(mode="RGB", size=(self.width, self.height), data=buf)
  209.         img = img.transpose(Image.FLIP_TOP_BOTTOM)
  210.         img.save(name)
  211.  
  212.         print("Saved {}".format(name))
  213.  
  214.     def update(self, dt):
  215.         if not self.rotate:
  216.             return
  217.         self.rx += 0.3
  218.         if self.rx > 360:
  219.             self.rx = 0
  220.         self.ry += 0.6
  221.         if self.ry > 360:
  222.             self.ry = 0
  223.         self.rz += 0.9
  224.         if self.rz > 360:
  225.             self.rz = 0
  226.  
  227.     def draw(self):
  228.         self.batch.draw()
  229.  
  230.  
  231.  
  232. end = TheEnd(x=1280, y=720, i_max=512)
  233. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment