Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. import math
  2. import matplotlib.pyplot as plt
  3.  
  4. FOV = 65
  5. RATIO = 16/10
  6. STEPS = 100
  7.  
  8. # returns [(Z, X, Y), ...] of viewport corners, Z points forward, X to the left, Y to upwards.
  9. def GetFrontProjection():
  10.     v = math.tan(FOV * math.pi / 360)
  11.     h = v * RATIO
  12.     return [(1, -h, v), (1, -h, -v), (1, h, -v), (1, h, v)]
  13.  
  14. def TurnHeadLeft(xs):
  15.     res = []
  16.     for x in xs:
  17.         res.append((x[1], -x[0], x[2]))
  18.     return res
  19.  
  20. # Projects coordinate to a sphere.
  21. def CartesianToSpherical(z, x, y):
  22.     r = math.sqrt(z*z + x*x + y*y)
  23.     theta = math.acos(z/r)
  24.     phi = math.atan2(y, x)
  25.     return (theta, phi)
  26.  
  27. # Azimuthal equidistant projection
  28. def SphericalToXY(theta, phi):
  29.     return (theta/math.pi*math.cos(phi), theta/math.pi*math.sin(phi))
  30.  
  31. def Interpolate(x, y, theta):
  32.     return (x*theta + y*(1-theta))
  33.  
  34. # Draws projection of line from 'f' to 't'.
  35. def DrawParametric(gca, f, t, color):
  36.     x = []
  37.     y = []
  38.     for i in range(STEPS):
  39.         theta = i / STEPS
  40.         c = (Interpolate(f[0], t[0], theta), Interpolate(f[1], t[1], theta), Interpolate(f[2], t[2], theta))
  41.         xy = SphericalToXY(*CartesianToSpherical(*c))
  42.         x.append(xy[0])
  43.         y.append(xy[1])
  44.     gca.plot(x, y, color=color)
  45.    
  46.    
  47. plt.axes()
  48.  
  49. gca = plt.gca()
  50. gca.add_patch(plt.Circle((0, 0), radius=1, fill=False))
  51.  
  52.  
  53. p = GetFrontProjection()
  54. colors = 'gyby'
  55. for i in range(4):
  56.     # xys = [SphericalToXY(*CartesianToSpherical(*x)) for x in p]
  57.     DrawParametric(gca, p[0], p[1], colors[i])
  58.     DrawParametric(gca, p[1], p[2], colors[i])
  59.     DrawParametric(gca, p[2], p[3], colors[i])
  60.     DrawParametric(gca, p[3], p[0], colors[i])
  61.     p = TurnHeadLeft(p)
  62.    
  63. plt.axis('scaled')
  64. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement