Compare commits
5
Commits
754f6bef90
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d3df3e5de | ||
|
|
d4da7cfd37 | ||
|
|
f5cd79a40c | ||
|
|
3a5bbedad1 | ||
|
|
41093db2ad |
No files matched your search
@@ -0,0 +1,36 @@
|
|||||||
|
"""Machine tests for the orthographic projection in compute.py.
|
||||||
|
|
||||||
|
Every test is structured according to the
|
||||||
|
Arrange - Act - Assert pattern.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from compute import project_ortho
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_ortho_without_scale() -> None:
|
||||||
|
"""project_ortho drops z and returns (x, y) unchanged."""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
expected = (1.0, 2.0)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
result = project_ortho(point)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
|
|
||||||
|
|
||||||
|
def test_project_ortho_with_scale_100() -> None:
|
||||||
|
"""project_ortho with scale=100 scales the projected point."""
|
||||||
|
# Arrange
|
||||||
|
point = (1.0, 2.0, 3.0)
|
||||||
|
scale = 100
|
||||||
|
expected = (100.0, 200.0)
|
||||||
|
|
||||||
|
# Act
|
||||||
|
result = project_ortho(point, scale)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
assert result == pytest.approx(expected)
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
"""Machine tests for the rotation functions in compute.py.
|
||||||
|
|
||||||
|
Every test is structured according to the
|
||||||
|
Arrange - Act - Assert pattern.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from math import pi, sqrt
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from compute import matmul, rot_2D, rot_3D
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# rot_2D
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def test_rot_2D_quarter_turn() -> None:
|
||||||
|
"""rot_2D(pi/2) maps the unit vector (1, 0) onto (0, 1)."""
|
||||||
|
# Arrange
|
||||||
|
expected = [[0.0, -1.0], [1.0, 0.0]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_2D(pi / 2)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(matrix, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rot_2D_full_turn_is_identity() -> None:
|
||||||
|
"""rot_2D(2*pi) is the identity matrix."""
|
||||||
|
# Arrange
|
||||||
|
expected = [[1.0, 0.0], [0.0, 1.0]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_2D(2 * pi)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(matrix, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rot_2D_known_values_45_degrees() -> None:
|
||||||
|
"""rot_2D(pi/4) has the well-known sqrt(2)/2 entries."""
|
||||||
|
# Arrange
|
||||||
|
s = sqrt(2) / 2
|
||||||
|
expected = [[s, -s], [s, s]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_2D(pi / 4)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(matrix, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# rot_3D
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def test_rot_3D_z_axis_quarter_turn() -> None:
|
||||||
|
"""rot_3D(pi/2, 'z') rotates (1, 0, 0) onto (0, 1, 0)."""
|
||||||
|
# Arrange
|
||||||
|
vector = [[1.0], [0.0], [0.0]] # column vector
|
||||||
|
expected = [[0.0], [1.0], [0.0]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_3D(pi / 2, "z")
|
||||||
|
result = matmul(matrix, vector)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(result, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rot_3D_x_axis_45_degrees() -> None:
|
||||||
|
"""rot_3D(pi/4, 'x') has the well-known sqrt(2)/2 entries."""
|
||||||
|
# Arrange
|
||||||
|
s = sqrt(2) / 2
|
||||||
|
expected = [[1.0, 0.0, 0.0],
|
||||||
|
[0.0, s, -s],
|
||||||
|
[0.0, s, s]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_3D(pi / 4, "x")
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(matrix, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rot_3D_y_axis_keeps_y_axis() -> None:
|
||||||
|
"""rot_3D(angle, 'y') leaves the y-axis unchanged."""
|
||||||
|
# Arrange
|
||||||
|
vector = [[0.0], [1.0], [0.0]] # y-axis as column vector
|
||||||
|
expected = [[0.0], [1.0], [0.0]]
|
||||||
|
|
||||||
|
# Act
|
||||||
|
matrix = rot_3D(1.234, "y") # arbitrary angle
|
||||||
|
result = matmul(matrix, vector)
|
||||||
|
|
||||||
|
# Assert
|
||||||
|
for row, exp_row in zip(result, expected):
|
||||||
|
for value, exp_value in zip(row, exp_row):
|
||||||
|
assert value == pytest.approx(exp_value)
|
||||||
|
|
||||||
|
|
||||||
|
def test_rot_3D_invalid_axis_raises() -> None:
|
||||||
|
"""rot_3D raises a ValueError for an unknown axis."""
|
||||||
|
# Arrange
|
||||||
|
bad_axis = "a"
|
||||||
|
|
||||||
|
# Act / Assert
|
||||||
|
with pytest.raises(ValueError):
|
||||||
|
rot_3D(pi / 2, bad_axis)
|
||||||
+24
-1
@@ -104,7 +104,30 @@ def print_matrix(matrix: Matrix) -> None:
|
|||||||
"""Print a matrix as a nicely formatted table."""
|
"""Print a matrix as a nicely formatted table."""
|
||||||
print(tabulate(matrix, tablefmt="grid"))
|
print(tabulate(matrix, tablefmt="grid"))
|
||||||
|
|
||||||
def rot_3D(angle: float, axis: str) -> list[list[float]]
|
def rot_3D(angle: float, axis: str) -> list[list[float]]:
|
||||||
|
axis = axis.lower()
|
||||||
|
c, s = cos(angle), sin(angle)
|
||||||
|
|
||||||
|
if axis == "x":
|
||||||
|
return [[1.0, 0.0, 0.0],
|
||||||
|
[0.0, c, -s],
|
||||||
|
[0.0, s, c]]
|
||||||
|
if axis == "y":
|
||||||
|
return [[c, 0.0, s],
|
||||||
|
[0.0, 1.0, 0.0],
|
||||||
|
[-s, 0.0, c]]
|
||||||
|
if axis == "z":
|
||||||
|
return [[c, -s, 0.0],
|
||||||
|
[s, c, 0.0],
|
||||||
|
[0.0, 0.0, 1.0]]
|
||||||
|
|
||||||
|
raise ValueError(f"Invalid axis '{axis}': expected 'x', 'y' or 'z'.")
|
||||||
|
|
||||||
|
def project_ortho(point: tuple[float, float, float], scale: float = 1,) -> tupel[float, float]:
|
||||||
|
x, y, _z = point
|
||||||
|
return(x * scale, y * scale)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@@ -5,3 +5,6 @@ dependencies = ["tabulate", "ruff"]
|
|||||||
|
|
||||||
[project.optional-dependencies]
|
[project.optional-dependencies]
|
||||||
dev = ["pytest", "ruff", "pytest-cov"]
|
dev = ["pytest", "ruff", "pytest-cov"]
|
||||||
|
|
||||||
|
[tool.pytest.ini_options]
|
||||||
|
pythonpath = "src"
|
||||||
Whitespace-only changes.
@@ -0,0 +1,70 @@
|
|||||||
|
"""A rotating wireframe cube rendered with pygame."""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from typing import List, Tuple
|
||||||
|
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from compute import matmul, project_ortho, rot_3D
|
||||||
|
from game import Game
|
||||||
|
|
||||||
|
# Cube vertices (corner points) of a cube with edge length 2.
|
||||||
|
VERTICES: List[List[float]] = [
|
||||||
|
[-1.0, -1.0, -1.0], [ 1.0, -1.0, -1.0],
|
||||||
|
[ 1.0, 1.0, -1.0], [-1.0, 1.0, -1.0],
|
||||||
|
[-1.0, -1.0, 1.0], [ 1.0, -1.0, 1.0],
|
||||||
|
[ 1.0, 1.0, 1.0], [-1.0, 1.0, 1.0],
|
||||||
|
]
|
||||||
|
|
||||||
|
EDGES: List[Tuple[int, int]] = [
|
||||||
|
(0, 1), (1, 2), (2, 3), (3, 0), # back face
|
||||||
|
(4, 5), (5, 6), (6, 7), (7, 4), # front face
|
||||||
|
(0, 4), (1, 5), (2, 6), (3, 7), # connecting edges
|
||||||
|
]
|
||||||
|
|
||||||
|
WHITE = (255, 255, 255)
|
||||||
|
SPEED = 0.01 # rotation per frame in radians
|
||||||
|
SCALE = 100 # pixels per world unit
|
||||||
|
|
||||||
|
|
||||||
|
class CubeGame(Game):
|
||||||
|
"""Rotates a wireframe cube continuously around two axes."""
|
||||||
|
|
||||||
|
def __init__(self, title: str = "Wuerfel") -> None:
|
||||||
|
"""Store the initial rotation angles."""
|
||||||
|
super().__init__(title=title)
|
||||||
|
self.angle_x = 0.0
|
||||||
|
self.angle_y = 0.0
|
||||||
|
|
||||||
|
def update_game(self) -> bool:
|
||||||
|
"""Advance both rotation angles by one step."""
|
||||||
|
self.angle_x += SPEED
|
||||||
|
self.angle_y += 0.5 * SPEED
|
||||||
|
return True
|
||||||
|
|
||||||
|
def rotated_vertices(self) -> List[Tuple[float, float]]:
|
||||||
|
"""Rotate all vertices and project them onto the screen."""
|
||||||
|
rotation = matmul(rot_3D(self.angle_x, "x"),
|
||||||
|
rot_3D(self.angle_y, "y"))
|
||||||
|
projected: List[Tuple[float, float]] = []
|
||||||
|
for vertex in VERTICES:
|
||||||
|
column = [[vertex[0]], [vertex[1]], [vertex[2]]]
|
||||||
|
rotated = matmul(rotation, column)
|
||||||
|
point = (rotated[0][0], rotated[1][0], rotated[2][0])
|
||||||
|
x, y = project_ortho(point, SCALE)
|
||||||
|
projected.append(self.absolut((x, y)))
|
||||||
|
return projected
|
||||||
|
|
||||||
|
def draw_game(self) -> None:
|
||||||
|
"""Draw the projected cube edges."""
|
||||||
|
self.clear()
|
||||||
|
vertices = self.rotated_vertices()
|
||||||
|
for start, end in EDGES:
|
||||||
|
pygame.draw.line(self.screen, WHITE,
|
||||||
|
vertices[start], vertices[end], 2)
|
||||||
|
self.display()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
CubeGame().start()
|
||||||
Reference in new issue
Block a user