Compare commits
3
Commits
3a5bbedad1
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2d3df3e5de | ||
|
|
d4da7cfd37 | ||
|
|
f5cd79a40c |
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)
|
||||||
File renamed without changes.
@@ -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