Guest User

Untitled

a guest
Dec 7th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 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.  
  9. import pyglet
  10.  
  11. class TheEnd(pyglet.window.Window):
  12.     def __init__(self, i_max = 512, x_min = 0, y_min = 0, x_max = 64, y_max = 64):
  13.         config = Config(double_buffer=True)
  14.         super().__init__(1920, 1080, resizable=True, config=config)
  15.  
  16.         self.sensitivity = 0.2
  17.         self.x = 0
  18.         self.y = 0
  19.         self.z = -100
  20.         self.rx = 0
  21.         self.ry = 0
  22.         self.rz = 0
  23.  
  24.         glClearColor(0, 0, 0, 0)
  25.         glColor3f(1, 0, 0)
  26.  
  27.         print("Creating the End...")
  28.  
  29.         theend = {}
  30.  
  31.         for i in range(i_max):
  32.              for j in range(i):
  33.                   a = i - j
  34.                   b = i + j
  35.                   c = a * b
  36.                   d = int(sqrt(c))
  37.                   e = int(c - (d * d))
  38.                   f = int(e - ((2 * d) + 1))
  39.                   n = int(i - d)
  40.                   x = int(d - a)
  41.  
  42.                   if e not in theend:
  43.                       theend[e] = {}
  44.                   if n not in theend[e]:
  45.                       theend[e][n] = []
  46.                   if f not in theend:
  47.                       theend[f] = {}
  48.                   if n - 1 not in theend[f]:
  49.                       theend[f][n - 1] = [];
  50.  
  51.                   theend[e][n].append([e, n, d, x, a, b, c])
  52.                   theend[f][n - 1].append([f, n - 1, d + 1, x + 1, a, b, c])
  53.  
  54.         print("Generating points...")
  55.  
  56.         points = []
  57.         colors = []
  58.         for e in theend:
  59.             for n in theend[e]:
  60.                 for cell in theend[e][n]:
  61.                     _, _, d, x, a, b, c = cell
  62.                     if not c % 2 == 1:
  63.                         continue
  64.                     if is_prime(a):
  65.                         colors.extend([255, 0, 0])
  66.                     else:
  67.                         colors.extend([255, 255, 255])
  68.                     points.extend([e, n, a])
  69.  
  70.         self.batch = pyglet.graphics.Batch()
  71.         self.batch.add(len(points)//3, GL_POINTS, None, ("v3i", points), ("c3B", (colors)))
  72.  
  73.     def on_resize(self, width, height):
  74.         glViewport(0, 0, width, height)
  75.         glMatrixMode(GL_PROJECTION)
  76.         glLoadIdentity()
  77.         gluPerspective(60., width / float(height), .1, 1000.)
  78.         glMatrixMode(GL_MODELVIEW)
  79.         return pyglet.event.EVENT_HANDLED
  80.  
  81.     def on_draw(self):
  82.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
  83.         glLoadIdentity()
  84.         glTranslatef(0, 0, self.z)
  85.         glTranslatef(self.x, self.y, 0)
  86.         glRotatef(self.rz, 0, 0, 1)
  87.         glRotatef(self.ry, 0, 1, 0)
  88.         glRotatef(self.rx, 1, 0, 0)
  89.         self.draw()
  90.         return pyglet.event.EVENT_HANDLED
  91.  
  92.     def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
  93.         if buttons == pyglet.window.mouse.LEFT:
  94.             self.x += dx * self.sensitivity
  95.             self.y += dy * self.sensitivity
  96.         elif buttons == pyglet.window.mouse.RIGHT:
  97.             self.rx += dy * self.sensitivity
  98.             self.ry += dx * self.sensitivity
  99.         return pyglet.event.EVENT_HANDLED
  100.  
  101.     def on_mouse_scroll(self, x, y, scroll_x, scroll_y):
  102.         self.z += scroll_y * 10
  103.         return pyglet.event.EVENT_HANDLED
  104.  
  105.     def on_key_press(self, symbol, modifier):
  106.         if symbol == pyglet.window.key.SPACE:
  107.             buf = (GLubyte * (self.width * self.height * 3))(0)
  108.             glReadPixels(0, 0, self.width, self.height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf)
  109.             img = Image.frombytes(mode="RGB", size=(self.width, self.height), data=buf)
  110.             img = img.transpose(Image.FLIP_TOP_BOTTOM)
  111.             epoch = int(time())
  112.             name = "end_{}.png".format(epoch)
  113.             img.save(name)
  114.  
  115.             print("Saved {}".format(name))
  116.         return pyglet.event.EVENT_HANDLED
  117.  
  118.     def draw(self):
  119.         self.batch.draw()
  120.  
  121.  
  122.  
  123. end = TheEnd()
  124. pyglet.app.run()
Advertisement
Add Comment
Please, Sign In to add comment