From cf1a540808e5858cc8ca70ed04c9f0b945a86d9d Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sat, 4 Feb 2023 22:55:07 -0600 Subject: [PATCH] Participation assignment 2023-02-01 --- flake.nix | 2 ++ participation/2023-02-01.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 participation/2023-02-01.py diff --git a/flake.nix b/flake.nix index ff4f3e2..336b83d 100644 --- a/flake.nix +++ b/flake.nix @@ -21,6 +21,8 @@ unzip texlive.combined.scheme-full imagemagick + + (python310.withPackages (p: with p; [ numpy ])) ]) ++ (with toolchain; [ cargo rustc diff --git a/participation/2023-02-01.py b/participation/2023-02-01.py new file mode 100644 index 000000000..d1ecbf5 --- /dev/null +++ b/participation/2023-02-01.py @@ -0,0 +1,29 @@ +import numpy as np + +I_lambda = np.array([1, 0, 0]) +I_dc_lambda = np.array([0, 0, 0]) + +a_min = 0.0 +a_max = 1.0 + +d_near = 1.0 +d_far = 21.0 + +eye = np.array([0, 0, 0]) + +def dist(a, b): + return np.linalg.norm(b - a, 2) + +def a_dc(d_obj): + if d_obj < d_near: return a_max + elif d_obj > d_far: return a_min + return a_min + (a_max - a_min) * (d_far - d_obj) / (d_far - d_near) + +def get_color(point): + d_obj = dist(eye, point) + I_lambda_prime = a_dc(d_obj) * I_lambda + (1 - a_dc(d_obj)) * I_dc_lambda + return I_lambda_prime + +print(get_color(np.array([1, 0, 0]))) # [1, 0, 0] +print(get_color(np.array([5, 0, 0]))) # [0.8, 0, 0] +print(get_color(np.array([20, 0, 0]))) # [0.05, 0, 0]