Skip to content

Commit 14199e0

Browse files
Yukti-09cclauss
andauthored
Create Transformations2D.py (#2310)
* Create Transformations2D.py * Update Transformations2D.py * Drop numpy and add type hints and doctests * Rename Transformations2D.py to transformations_2d.py Co-authored-by: Christian Clauss <[email protected]>
1 parent fcc8a28 commit 14199e0

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
2D Transformations are regularly used in Linear Algebra.
3+
4+
I have added the codes for reflection, projection, scaling and rotation 2D matrices.
5+
6+
scaling(5) = [[5.0, 0.0], [0.0, 5.0]]
7+
rotation(45) = [[0.5253219888177297, -0.8509035245341184],
8+
[0.8509035245341184, 0.5253219888177297]]
9+
projection(45) = [[0.27596319193541496, 0.446998331800279],
10+
[0.446998331800279, 0.7240368080645851]]
11+
reflection(45) = [[0.05064397763545947, 0.893996663600558],
12+
[0.893996663600558, 0.7018070490682369]]
13+
"""
14+
from math import cos, sin
15+
from typing import List
16+
17+
18+
def scaling(scaling_factor: float) -> List[List[float]]:
19+
"""
20+
>>> scaling(5)
21+
[[5.0, 0.0], [0.0, 5.0]]
22+
"""
23+
scaling_factor = float(scaling_factor)
24+
return [[scaling_factor * int(x == y) for x in range(2)] for y in range(2)]
25+
26+
27+
def rotation(angle: float) -> List[List[float]]:
28+
"""
29+
>>> rotation(45) # doctest: +NORMALIZE_WHITESPACE
30+
[[0.5253219888177297, -0.8509035245341184],
31+
[0.8509035245341184, 0.5253219888177297]]
32+
"""
33+
c, s = cos(angle), sin(angle)
34+
return [[c, -s], [s, c]]
35+
36+
37+
def projection(angle: float) -> List[List[float]]:
38+
"""
39+
>>> projection(45) # doctest: +NORMALIZE_WHITESPACE
40+
[[0.27596319193541496, 0.446998331800279],
41+
[0.446998331800279, 0.7240368080645851]]
42+
"""
43+
c, s = cos(angle), sin(angle)
44+
cs = c * s
45+
return [[c * c, cs], [cs, s * s]]
46+
47+
48+
def reflection(angle: float) -> List[List[float]]:
49+
"""
50+
>>> reflection(45) # doctest: +NORMALIZE_WHITESPACE
51+
[[0.05064397763545947, 0.893996663600558],
52+
[0.893996663600558, 0.7018070490682369]]
53+
"""
54+
c, s = cos(angle), sin(angle)
55+
cs = c * s
56+
return [[2 * c - 1, 2 * cs], [2 * cs, 2 * s - 1]]
57+
58+
59+
print(f" {scaling(5) = }")
60+
print(f" {rotation(45) = }")
61+
print(f"{projection(45) = }")
62+
print(f"{reflection(45) = }")

0 commit comments

Comments
 (0)