Skip to content

Commit 1b637ba

Browse files
amaank404dhruvmanilacclauss
authored
Create vector3_for_2d_rendering.py (#2496)
* Create vector3_for_2d_rendering.py Edited for passing travis test * Delete vector3_for_2d_rendering.py * Create vector3_for_2d_rendering.py * Update vector3_for_2d_rendering.py Compressed the line 19 to 28 into 19 to 21 * Update vector3_for_2d_rendering.py * Update vector3_for_2d_rendering.py * Update vector3_for_2d_rendering.py completly corrected pep8 errors using Pycharm IDE * Update vector3_for_2d_rendering.py * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Dhruv <[email protected]> * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Dhruv <[email protected]> * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Dhruv <[email protected]> * Update vector3_for_2d_rendering.py * Update vector3_for_2d_rendering.py * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Christian Clauss <[email protected]> * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Christian Clauss <[email protected]> * Apply suggestions from code review Co-authored-by: Christian Clauss <[email protected]> * Update vector3_for_2d_rendering.py Added A few extra names to __author__ 😄 * Update vector3_for_2d_rendering.py Used Pycharm to fix PEP8 errors, doctest errors * Update vector3_for_2d_rendering.py Added enough doctests * Update graphics/vector3_for_2d_rendering.py Co-authored-by: Christian Clauss <[email protected]> * Remove second main() Co-authored-by: Dhruv <[email protected]> Co-authored-by: Christian Clauss <[email protected]>
1 parent ceacfc6 commit 1b637ba

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

graphics/vector3_for_2d_rendering.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
"""
2+
render 3d points for 2d surfaces.
3+
"""
4+
5+
from __future__ import annotations
6+
import math
7+
8+
__version__ = "2020.9.26"
9+
__author__ = "xcodz-dot, cclaus, dhruvmanila"
10+
11+
12+
def convert_to_2d(x: float, y: float, z: float, scale: float,
13+
distance: float) -> tuple[float, float]:
14+
"""
15+
Converts 3d point to a 2d drawable point
16+
17+
>>> convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0)
18+
(7.6923076923076925, 15.384615384615385)
19+
20+
>>> convert_to_2d(1, 2, 3, 10, 10)
21+
(7.6923076923076925, 15.384615384615385)
22+
23+
>>> convert_to_2d("1", 2, 3, 10, 10) # '1' is str
24+
Traceback (most recent call last):
25+
...
26+
TypeError: Input values must either be float or int: ['1', 2, 3, 10, 10]
27+
"""
28+
if not all(isinstance(val, (float, int)) for val in locals().values()):
29+
raise TypeError("Input values must either be float or int: "
30+
f"{list(locals().values())}")
31+
projected_x = ((x * distance) / (z + distance)) * scale
32+
projected_y = ((y * distance) / (z + distance)) * scale
33+
return projected_x, projected_y
34+
35+
36+
def rotate(x: float, y: float, z: float, axis: str,
37+
angle: float) -> tuple[float, float, float]:
38+
"""
39+
rotate a point around a certain axis with a certain angle
40+
angle can be any integer between 1, 360 and axis can be any one of
41+
'x', 'y', 'z'
42+
43+
>>> rotate(1.0, 2.0, 3.0, 'y', 90.0)
44+
(3.130524675073759, 2.0, 0.4470070007889556)
45+
46+
>>> rotate(1, 2, 3, "z", 180)
47+
(0.999736015495891, -2.0001319704760485, 3)
48+
49+
>>> rotate('1', 2, 3, "z", 90.0) # '1' is str
50+
Traceback (most recent call last):
51+
...
52+
TypeError: Input values except axis must either be float or int: ['1', 2, 3, 90.0]
53+
54+
>>> rotate(1, 2, 3, "n", 90) # 'n' is not a valid axis
55+
Traceback (most recent call last):
56+
...
57+
ValueError: not a valid axis, choose one of 'x', 'y', 'z'
58+
59+
>>> rotate(1, 2, 3, "x", -90)
60+
(1, -2.5049096187183877, -2.5933429780983657)
61+
62+
>>> rotate(1, 2, 3, "x", 450) # 450 wrap around to 90
63+
(1, 3.5776792428178217, -0.44744970165427644)
64+
"""
65+
if not isinstance(axis, str):
66+
raise TypeError("Axis must be a str")
67+
input_variables = locals()
68+
del input_variables["axis"]
69+
if not all(isinstance(val, (float, int)) for val in input_variables.values()):
70+
raise TypeError("Input values except axis must either be float or int: "
71+
f"{list(input_variables.values())}")
72+
angle = (angle % 360) / 450 * 180 / math.pi
73+
if axis == 'z':
74+
new_x = x * math.cos(angle) - y * math.sin(angle)
75+
new_y = y * math.cos(angle) + x * math.sin(angle)
76+
new_z = z
77+
elif axis == 'x':
78+
new_y = y * math.cos(angle) - z * math.sin(angle)
79+
new_z = z * math.cos(angle) + y * math.sin(angle)
80+
new_x = x
81+
elif axis == 'y':
82+
new_x = x * math.cos(angle) - z * math.sin(angle)
83+
new_z = z * math.cos(angle) + x * math.sin(angle)
84+
new_y = y
85+
else:
86+
raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'")
87+
88+
return new_x, new_y, new_z
89+
90+
91+
if __name__ == "__main__":
92+
import doctest
93+
94+
doctest.testmod()
95+
print(f"{convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) = }")
96+
print(f"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }")

0 commit comments

Comments
 (0)