Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Last active July 24, 2020 13:19
Show Gist options
  • Save omaraflak/f81af3f734e54c94aa6221593efc7ee6 to your computer and use it in GitHub Desktop.
Save omaraflak/f81af3f734e54c94aa6221593efc7ee6 to your computer and use it in GitHub Desktop.
import numpy as np
import matplotlib.pyplot as plt
def normalize(vector):
return vector / np.linalg.norm(vector)
width = 300
height = 200
camera = np.array([0, 0, 1])
ratio = float(width) / height
screen = (-1, 1 / ratio, 1, -1 / ratio) # left, top, right, bottom
image = np.zeros((height, width, 3))
for i, y in enumerate(np.linspace(screen[1], screen[3], height)):
for j, x in enumerate(np.linspace(screen[0], screen[2], width)):
pixel = np.array([x, y, 0])
origin = camera
direction = normalize(pixel - origin)
# image[i, j] = ...
print("progress: %d/%d" % (i + 1, height))
plt.imsave('image.png', image)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment