Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import matplotlib.pyplot as plt
- FOV = 65
- RATIO = 16/10
- STEPS = 100
- # returns [(Z, X, Y), ...] of viewport corners, Z points forward, X to the left, Y to upwards.
- def GetFrontProjection():
- v = math.tan(FOV * math.pi / 360)
- h = v * RATIO
- return [(1, -h, v), (1, -h, -v), (1, h, -v), (1, h, v)]
- def TurnHeadLeft(xs):
- res = []
- for x in xs:
- res.append((x[1], -x[0], x[2]))
- return res
- # Projects coordinate to a sphere.
- def CartesianToSpherical(z, x, y):
- r = math.sqrt(z*z + x*x + y*y)
- theta = math.acos(z/r)
- phi = math.atan2(y, x)
- return (theta, phi)
- # Azimuthal equidistant projection
- def SphericalToXY(theta, phi):
- return (theta/math.pi*math.cos(phi), theta/math.pi*math.sin(phi))
- def Interpolate(x, y, theta):
- return (x*theta + y*(1-theta))
- # Draws projection of line from 'f' to 't'.
- def DrawParametric(gca, f, t, color):
- x = []
- y = []
- for i in range(STEPS):
- theta = i / STEPS
- c = (Interpolate(f[0], t[0], theta), Interpolate(f[1], t[1], theta), Interpolate(f[2], t[2], theta))
- xy = SphericalToXY(*CartesianToSpherical(*c))
- x.append(xy[0])
- y.append(xy[1])
- gca.plot(x, y, color=color)
- plt.axes()
- gca = plt.gca()
- gca.add_patch(plt.Circle((0, 0), radius=1, fill=False))
- p = GetFrontProjection()
- colors = 'gyby'
- for i in range(4):
- # xys = [SphericalToXY(*CartesianToSpherical(*x)) for x in p]
- DrawParametric(gca, p[0], p[1], colors[i])
- DrawParametric(gca, p[1], p[2], colors[i])
- DrawParametric(gca, p[2], p[3], colors[i])
- DrawParametric(gca, p[3], p[0], colors[i])
- p = TurnHeadLeft(p)
- plt.axis('scaled')
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement