Advertisement
here2share

# Tk_ray_trace.py

May 13th, 2020
1,249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.17 KB | None | 0 0
  1. # Tk_ray_trace.py
  2.  
  3. from Tkinter import *
  4. from PIL import Image, ImageTk
  5. import math
  6.  
  7. class vector:
  8.     def __init__(self, x, y, z):
  9.         self.x = x
  10.         self.y = y
  11.         self.z = z
  12.  
  13. def scan():
  14.     radius = 0.5
  15.     point = vector(0.0, 0.0, 0.0)
  16.     fromvec = vector(0.0, -1.0, 3.0)
  17.     to = vector(0.0, 0.0, -1.0)
  18.     to = vector(0.0, 0.0, -1.0)
  19.     while fromvec.y < 1.0:
  20.         fromvec.x = -1.0
  21.         while fromvec.x < 1.0:
  22.             vec = vminus(fromvec, point)
  23.             b = innerproduct(to, vec)
  24.             c = innerproduct(vec, vec) - radius*radius
  25.             d = b * b - c
  26.             if d < 0: # ray is not intersect
  27.                 fromvec.x = fromvec.x + 2.0/200
  28.                 continue
  29.             det = math.sqrt(d)
  30.             t = -b + det
  31.             if t < 0: # object is behind eye
  32.                 fromvec.x = fromvec.x + 2.0/200
  33.                 continue
  34.             shading(t, fromvec, to, point)
  35.             fromvec.x = fromvec.x + 2.0/200
  36.         fromvec.y = fromvec.y + 2.0/200
  37.  
  38. def shading(t, fromvec, to, point):
  39.     l = vector(0.577, -0.577, -0.577) # light normalize(1, -1, -1)
  40.     ac = vector(0.1, 0.1, 0.1)        # ambient light color
  41.     col = vector(1, 0, 0)             # color of an object
  42.     too = vector(to.x * t, to.y * t, to.z * t)
  43.     c = vplus(fromvec, too)
  44.     c = vminus(c, point)
  45.     c = normalize(c)
  46.     val = innerproduct(c, l)
  47.     r = int(255.0 * (col.x * val + ac.x))
  48.     g = int(255.0 * (col.y * val + ac.y))
  49.     b = int(255.0 * (col.z * val + ac.z))
  50.     if r > 255:
  51.         r = 255
  52.     if g > 255:
  53.         g = 255
  54.     if b > 255:
  55.         b = 255
  56.     if r < 0:
  57.         r = 0
  58.     if g < 0:
  59.         g = 0
  60.     if b < 0:
  61.         b = 0
  62.     pic[int(fromvec.x*100+100)+(int(fromvec.y*100+100)*200)] = (r, g, b)
  63.  
  64. def vplus(a, b):
  65.     return vector( a.x + b.x, a.y + b.y, a.z + b.z)
  66.  
  67. def vminus(a, b):
  68.     return vector( a.x - b.x, a.y - b.y, a.z - b.z)
  69.  
  70. def innerproduct(a, b):
  71.     return a.x * b.x + a.y * b.y + a.z * b.z
  72.  
  73. def normalize(a):
  74.     s = a.x * a.x + a.y * a.y + a.z * a.z
  75.     d = math.sqrt(s)
  76.     return vector(a.x/d, a.y/d, a.z/d)
  77.  
  78. sq = 200
  79. pic = [(100,100,100) for z in range(sq*sq)]
  80. scan()
  81. root = Tk()
  82. root.title("Tk Ray Trace")
  83. root.geometry("%dx%d+0+0"%(sq,sq))
  84. canvas = Canvas(root, width=sq, height=sq)
  85. canvas.grid()
  86. img = Image.new("RGB",(sq, sq))
  87. img.putdata(pic)
  88. imgTk = ImageTk.PhotoImage(img)
  89. canvas.create_image(0, 0, anchor=NW, image=imgTk)
  90. canvas.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement