Advertisement
moomoomoo309

Python3_3Dgraphing

Sep 19th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. from random import random
  2. from turtle import Turtle
  3. from colorsys import hsv_to_rgb
  4.  
  5. from math import sin, ceil, cos, pi
  6.  
  7.  
  8. rectsPerPen = 100
  9. rectSize = 100
  10. equation = lambda x, y, time: (sin(y) + cos(x) + time) % 2 / 2
  11. scaleX,scaleY=.005,.005
  12. scaleX/=pi/rectSize
  13. scaleY/=pi/rectSize
  14. grayscale=False
  15.  
  16. pen=Turtle()
  17. pen.hideturtle()
  18. canvas=pen.getscreen()
  19. canvas.tracer(0, 0)
  20. oldPenPool, newPenPool = [], []
  21. width, height = canvas.window_width(),canvas.window_height()
  22.  
  23. for pool in [oldPenPool, newPenPool]:
  24.     for i in range(ceil(width * height / rectsPerPen)):
  25.         pen = Turtle()
  26.         pen.hideturtle()
  27.         pen.setundobuffer(0)
  28.         pool.append(pen)
  29.  
  30.  
  31. def rect(pen, x, y, width, height, color):
  32.     pen.penup()
  33.     pen.goto(x + width, y)
  34.     pen.pendown()
  35.     pen.pencolor(color)
  36.     pen.fillcolor(color)
  37.     if width == 1 and height == 1:
  38.         pen.dot()
  39.         return
  40.     elif width == 1:
  41.         pen.sety(y - height)
  42.         return
  43.     elif height == 1:
  44.         pen.setx(x + width)
  45.         return
  46.     pen.begin_fill()
  47.     for i in range(2):
  48.         pen.right(90)
  49.         pen.forward(width)
  50.         pen.right(90)
  51.         pen.forward(height)
  52.     pen.end_fill()
  53.  
  54.  
  55. def toWindowCoords(x, y):
  56.     return x - width / 2, -y + height / 2
  57.  
  58.  
  59. time = 0
  60. speed = .1
  61. while True:
  62.     i = -1
  63.     for w in range(0, width, rectSize):
  64.         for h in range(0, height, rectSize):
  65.             i += 1
  66.             x, y = toWindowCoords(w, h)
  67.             shade=equation(w*scaleX, h*scaleY, time)
  68.             rect(newPenPool[int(i / rectsPerPen)], x, y, rectSize, rectSize, hsv_to_rgb(shade, 1, 1) if not grayscale else [shade,shade,shade])
  69.             if i % rectsPerPen == 0:
  70.                 oldPenPool[int(i / rectsPerPen) - 1].clear()
  71.                 canvas.update()
  72.     time += speed
  73.     tempPenPool = oldPenPool
  74.     oldPenPool = newPenPool
  75.     newPenPool = tempPenPool
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement