-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Create vector3_for_2d_rendering.py #2496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
9590d36
Create vector3_for_2d_rendering.py
amaank404 57ee51c
Delete vector3_for_2d_rendering.py
amaank404 2775efe
Create vector3_for_2d_rendering.py
amaank404 d9fcc66
Update vector3_for_2d_rendering.py
amaank404 ae70f41
Update vector3_for_2d_rendering.py
amaank404 3e52831
Update vector3_for_2d_rendering.py
amaank404 ccb6c9f
Update vector3_for_2d_rendering.py
amaank404 828f11a
Update vector3_for_2d_rendering.py
amaank404 9975733
Update graphics/vector3_for_2d_rendering.py
amaank404 022f7d5
Update graphics/vector3_for_2d_rendering.py
amaank404 7b13ddc
Update graphics/vector3_for_2d_rendering.py
amaank404 af6cd9e
Update vector3_for_2d_rendering.py
amaank404 1bad0cc
Update vector3_for_2d_rendering.py
amaank404 a098a17
Update graphics/vector3_for_2d_rendering.py
amaank404 b2fee5d
Update graphics/vector3_for_2d_rendering.py
amaank404 fe96c7f
Apply suggestions from code review
amaank404 ff55531
Update vector3_for_2d_rendering.py
amaank404 c5f6164
Update vector3_for_2d_rendering.py
amaank404 f5e9d4e
Update vector3_for_2d_rendering.py
amaank404 44404ef
Update graphics/vector3_for_2d_rendering.py
amaank404 c5f35b5
Remove second main()
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
""" | ||
render 3d points for 2d surfaces. | ||
""" | ||
|
||
from __future__ import annotations | ||
import math | ||
|
||
__version__ = "2020.9.26" | ||
__author__ = "xcodz-dot, cclaus, dhruvmanila" | ||
|
||
|
||
def convert_to_2d(x: float, y: float, z: float, scale: float, | ||
distance: float) -> tuple[float, float]: | ||
""" | ||
Converts 3d point to a 2d drawable point | ||
|
||
>>> convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) | ||
(7.6923076923076925, 15.384615384615385) | ||
|
||
>>> convert_to_2d(1, 2, 3, 10, 10) | ||
(7.6923076923076925, 15.384615384615385) | ||
|
||
>>> convert_to_2d("1", 2, 3, 10, 10) # '1' is str | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Input values must either be float or int: ['1', 2, 3, 10, 10] | ||
""" | ||
if not all(isinstance(val, (float, int)) for val in locals().values()): | ||
raise TypeError("Input values must either be float or int: " | ||
amaank404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
f"{list(locals().values())}") | ||
projected_x = ((x * distance) / (z + distance)) * scale | ||
projected_y = ((y * distance) / (z + distance)) * scale | ||
return projected_x, projected_y | ||
|
||
|
||
def rotate(x: float, y: float, z: float, axis: str, | ||
angle: float) -> tuple[float, float, float]: | ||
""" | ||
rotate a point around a certain axis with a certain angle | ||
angle can be any integer between 1, 360 and axis can be any one of | ||
'x', 'y', 'z' | ||
|
||
>>> rotate(1.0, 2.0, 3.0, 'y', 90.0) | ||
(3.130524675073759, 2.0, 0.4470070007889556) | ||
amaank404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
>>> rotate(1, 2, 3, "z", 180) | ||
(0.999736015495891, -2.0001319704760485, 3) | ||
|
||
>>> rotate('1', 2, 3, "z", 90.0) # '1' is str | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Input values except axis must either be float or int: ['1', 2, 3, 90.0] | ||
|
||
>>> rotate(1, 2, 3, "n", 90) # 'n' is not a valid axis | ||
Traceback (most recent call last): | ||
... | ||
ValueError: not a valid axis, choose one of 'x', 'y', 'z' | ||
|
||
>>> rotate(1, 2, 3, "x", -90) | ||
(1, -2.5049096187183877, -2.5933429780983657) | ||
|
||
>>> rotate(1, 2, 3, "x", 450) # 450 wrap around to 90 | ||
(1, 3.5776792428178217, -0.44744970165427644) | ||
""" | ||
if not isinstance(axis, str): | ||
raise TypeError("Axis must be a str") | ||
input_variables = locals() | ||
del input_variables["axis"] | ||
if not all(isinstance(val, (float, int)) for val in input_variables.values()): | ||
raise TypeError("Input values except axis must either be float or int: " | ||
f"{list(input_variables.values())}") | ||
angle = (angle % 360) / 450 * 180 / math.pi | ||
if axis == 'z': | ||
new_x = x * math.cos(angle) - y * math.sin(angle) | ||
new_y = y * math.cos(angle) + x * math.sin(angle) | ||
new_z = z | ||
elif axis == 'x': | ||
new_y = y * math.cos(angle) - z * math.sin(angle) | ||
new_z = z * math.cos(angle) + y * math.sin(angle) | ||
new_x = x | ||
elif axis == 'y': | ||
new_x = x * math.cos(angle) - z * math.sin(angle) | ||
new_z = z * math.cos(angle) + x * math.sin(angle) | ||
new_y = y | ||
else: | ||
raise ValueError("not a valid axis, choose one of 'x', 'y', 'z'") | ||
|
||
return new_x, new_y, new_z | ||
amaank404 marked this conversation as resolved.
Show resolved
Hide resolved
amaank404 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
print(f"{convert_to_2d(1.0, 2.0, 3.0, 10.0, 10.0) = }") | ||
print(f"{rotate(1.0, 2.0, 3.0, 'y', 90.0) = }") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.