69 lines
1.8 KiB
Python
69 lines
1.8 KiB
Python
import numpy as np
|
|
from sympy import *
|
|
import math
|
|
|
|
unit = lambda v: v/np.linalg.norm(v)
|
|
|
|
def problem_1():
|
|
p = np.array([1, 4, 8])
|
|
e = np.array([0, 0, 0])
|
|
s = np.array([2, 2, 10])
|
|
|
|
i = p - e
|
|
print("incoming", i)
|
|
print("|I| =", np.linalg.norm(i))
|
|
n = s - p
|
|
print("normal", n)
|
|
|
|
n_norm = unit(n)
|
|
print("normal_norm", n_norm)
|
|
cos_theta_i = np.dot(-i, n) / (np.linalg.norm(i) * np.linalg.norm(n))
|
|
print("part a = cos^{-1} of ", cos_theta_i)
|
|
print(np.arccos(cos_theta_i))
|
|
|
|
proj = n_norm * np.linalg.norm(i) * cos_theta_i
|
|
print("proj", proj)
|
|
|
|
p_ = p + proj
|
|
print("proj point", p_)
|
|
|
|
v2 = p_ - e
|
|
print("v2", v2)
|
|
|
|
sin_theta_i = np.sin(np.arccos(cos_theta_i))
|
|
print("sin theta_i =", sin_theta_i)
|
|
|
|
print("approx answer for part d", np.arcsin(1.0 / 1.5 * sin_theta_i))
|
|
|
|
def problem_4():
|
|
print("part 4a.")
|
|
|
|
def problem_8():
|
|
def P(left, right, bottom, top, near, far):
|
|
return np.array([
|
|
[2.0 * near / (right - left), 0, (right + left) / (right - left), 0],
|
|
[0, 2.0 * near / (top - bottom), (top + bottom) / (top - bottom), 0],
|
|
[0, 0, -(far + near) / (far - near), -(2.0 * far * near) / (far - near)],
|
|
[0, 0, -1, 0],
|
|
])
|
|
|
|
near = 0.5
|
|
far = 20
|
|
|
|
def compute_view(vfov, hfov):
|
|
left = -math.tan(hfov) * near
|
|
right = math.tan(hfov) * near
|
|
bottom = -math.tan(vfov) * near
|
|
top = math.tan(vfov) * near
|
|
return left, right, bottom, top
|
|
|
|
|
|
print("part 8a")
|
|
vfov = hfov = math.radians(60)
|
|
left, right, bottom, top = compute_view(vfov, hfov)
|
|
print(P(left, right, bottom, top, near, far))
|
|
print()
|
|
|
|
print("\nPROBLEM 4 -------------------------"); problem_4()
|
|
print("\nPROBLEM 8 -------------------------"); problem_8()
|
|
print("\nPROBLEM 1 -------------------------"); problem_1()
|