From 9b09fc2aacfa33c79cdb631fd351a03e02cd6961 Mon Sep 17 00:00:00 2001 From: Phexi Date: Thu, 29 Oct 2026 12:15:39 +0100 Subject: [PATCH] new ways to project --- src/compute.py | 12 ++++++++-- test/test_compute.py | 1 + test/test_projection.py | 49 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 test/test_projection.py diff --git a/src/compute.py b/src/compute.py index 6a14156..85eeeb0 100644 --- a/src/compute.py +++ b/src/compute.py @@ -140,8 +140,16 @@ def rot_3D(angle:float, axis: str) -> list[list[float]]: return [row_1, row_2, row_3] -def project_ortho(point: tuple[float, float, float], scale: float = 1) -> tuple[float, float, float]: - return +def project_ortho(point: tuple[float, float, float], scale: float = 1) -> tuple[float, float]: + return point[0] * scale, point[1] * scale + +def project_perspective(point: tuple[float, float, float], + focal_length: float = 1.0, + scale: float = 1.0, + camera_distance: float = 4.0) -> tuple[float, float]: + x2 = ((point[0] * focal_length) / (point[2] + camera_distance)) * scale + y2 = ((point[1] * focal_length) / (point[2] + camera_distance)) * scale + return x2, y2 def transpose(matrix: list[list[float]]) -> list[list[float]]: diff --git a/test/test_compute.py b/test/test_compute.py index 79c2fd6..9d33ab1 100644 --- a/test/test_compute.py +++ b/test/test_compute.py @@ -31,3 +31,4 @@ def test_get_3D_rotation_matrix(): #Assert assert rot == expected_matrix + diff --git a/test/test_projection.py b/test/test_projection.py new file mode 100644 index 0000000..9118ba1 --- /dev/null +++ b/test/test_projection.py @@ -0,0 +1,49 @@ +from src.compute import project_ortho, project_perspective + +def test_ortho_proj_unscaled(): + #Arrange + point = (1.0, 2.0, 3.0) + expected = (1.0, 2.0) + + #Act + result = project_ortho(point) + + #Assert + assert result == expected + + +def test_ortho_proj_scaled(): + # Arrange + point = (1.0, 2.0, 3.0) + scale = 100.0 + expected = (100.0, 200.0) + + # Act + result = project_ortho(point, scale) + + # Assert + assert result == expected + +def test_perspective_proj_unscaled(): + # Arrange + point = (1.0, 2.0, 3.0) + expected = (1/7, 2/7) + + # Act + result = project_perspective(point) + + # Assert + assert result == expected + +def test_perspective_proj_scaled(): + # Arrange + point = (1.0, 2.0, 3.0) + scale = 100.0 + focal_length = 2 + expected = (200/7, 400/7) + + # Act + result = project_perspective(point, scale, focal_length) + + # Assert + assert result == expected \ No newline at end of file