Advertisement
here2share

# tk_refracted_water.py

Jan 28th, 2023
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.13 KB | None | 0 0
  1. # tk_refracted_water.py
  2.  
  3. import tkinter as tk
  4. import math
  5. import random
  6.  
  7. root = tk.Tk()
  8. canvas = tk.Canvas(root, width=600, height=600)
  9. canvas.pack()
  10.  
  11. zzz = []
  12.  
  13. def shading(normal_vector, light_direction):
  14.     # calculate the angle between the normal vector and the light direction
  15.     dot_product = sum([a*b for a,b in zip(normal_vector, light_direction)])
  16.     dot_product = max(0, min(1, (dot_product + 1000) * 0.0005))
  17.     angle = math.acos(dot_product)
  18.     # use the angle to determine the shade of blue
  19.     shade = int(255 * 2 * (1 - angle / math.pi))
  20.     if shade > 255:
  21.         shade -= 255
  22.         rgb = (shade, shade, 255)
  23.     else:
  24.         rgb = (0, 0, shade)
  25.     return "#{:02x}{:02x}{:02x}".format(*rgb)
  26.    
  27. def vector(x, y):
  28.     if (x, y) in vectors:
  29.         return vectors[x, y]
  30.     else:
  31.         x1 = x + random.randint(-7, 7)
  32.         y1 = y + random.randint(-7, 7)
  33.         z1 = random.randint(0, 20)
  34.         vectors[x, y] = x1, y1, z1
  35.         return x1, y1, z1
  36.    
  37. triangles = []
  38. depths = []
  39. vectors = {}
  40. t = 0
  41. for i in range(-1, 26):
  42.     for j in range(-1, 26):
  43.         x = i * 24
  44.         y = j * 24
  45.         x1, y1, z1 = vector(x, y)
  46.         x2, y2, z2 = vector(x + 24, y)
  47.         x3, y3, z3 = vector(x, y + 24)
  48.         vectors[t] = [(x, y), (x + 24, y), (x, y + 24)]
  49.         t += 1
  50.         triangles.append([x1, y1, x2, y2, x3, y3])
  51.         depths.append([z1, z2, z3])
  52.        
  53.         x1 = x2
  54.         y1 = y2
  55.         x2, y2, z2 = vector(x + 24, y + 24)
  56.         vectors[t] = [(x + 24, y + 24)]
  57.         t += 1
  58.         triangles.append([x1, y1, x2, y2, x3, y3])
  59.         depths.append([z1, z2, z3])
  60.        
  61. # set the light direction
  62. light_direction = (1, 1, 1)
  63.  
  64. while 1:
  65.     canvas.delete('all')
  66.     for i in range(len(triangles)):
  67.         x1, y1, x2, y2, x3, y3 = triangles[i]
  68.         z1, z2, z3 = depths[i]
  69.         # calculate the normal vector using the cross product of the two vectors of the triangle
  70.         a = [x2-x1, y2-y1, z2-z1]
  71.         b = [x3-x1, y3-y1, z3-z1]
  72.         normal_vector = [a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]]
  73.         # call the shading function with the normal vector and light direction
  74.         shade = shading(normal_vector, light_direction)
  75.         # create the polygon with the returned fill color
  76.         canvas.create_polygon(x1, y1, x2, y2, x3, y3, outline='', fill=shade)
  77.     root.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement