Skip to content
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

integer cast function for the math.VectorX #3109

Closed
XFajk opened this issue Sep 20, 2024 · 6 comments
Closed

integer cast function for the math.VectorX #3109

XFajk opened this issue Sep 20, 2024 · 6 comments
Labels
enhancement math pygame.math New API This pull request may need extra debate as it adds a new class or function to pygame

Comments

@XFajk
Copy link
Contributor

XFajk commented Sep 20, 2024

I was thinking if making a method that turns floats in the Vector to integers would be a good addition because in my last project I found my self doing pygame.Vector2(int(other_vector.x), int(other_vector.y)) and something like other_vector.copy().int() would maybe be more readable

also I can and would want to implement this I am just making an issue to get it approved that I can add this and also I to link the pull request to this issue

@damusss damusss added New API This pull request may need extra debate as it adds a new class or function to pygame math pygame.math labels Sep 20, 2024
@damusss
Copy link
Member

damusss commented Sep 20, 2024

It is technically not the same thing, but have considered using the round() function instead? It returns a vector with each component rounded. (1.2 -> 1, 2.6 -> 3)

@XFajk
Copy link
Contributor Author

XFajk commented Sep 20, 2024

It is technically not the same thing, but have considered using the round() function instead? It returns a vector with each component rounded. (1.2 -> 1, 2.6 -> 3)

maybe the best approach would be to implement normal logical round like you said but also a floor and ceil methods

@damusss
Copy link
Member

damusss commented Sep 20, 2024

@XFajk the round() functionality is implement trough __round__ magic method (95% confident) so the best course of action could be to implement the __int__ method, but there might be reasons it wasn't done before that I'm not aware of.

class Custom:
    def __init__(self, any: float):
        self.any = any

    def __int__(self):
        print("int called")
        return int(self.any)

print(int(Custom(2.8)))

(example, for Vector2 it would be needed to be done in C)

@XFajk
Copy link
Contributor Author

XFajk commented Sep 20, 2024

@damusss I could just try to implement the int method if some unrelated tests fail I will just close the issue

@aatle
Copy link
Contributor

aatle commented Sep 20, 2024

Hey @XFajk, note that you would not use the __int__ method to implement this behavior because __int__ must return an integer.
Better options would be __trunc__, __floor__, and __ceil__. However, that the math module docs for these all say that the method should return a valid number.Integral value.

Personally, I don't think any new implementation is needed, because there are ways to get similar behavior:

  • All pygame functions that take pixel coordinates automatically truncate the decimal
  • You can floor (note that your examples truncates) a vector with floor division by 1: pygame.Vector2(1.3, -1.6) // 1, giving a new vector
  • You can round a vector with round(), giving a new vector
  • I think the provided use case is uncommon. Feel free to elaborate more on your use case.

@XFajk
Copy link
Contributor Author

XFajk commented Sep 22, 2024

@aatle yeah you right I tried to implement it but I ran into the int can only return a integer problem and I dont see a better way to implement this that would feel weird

@XFajk XFajk closed this as completed Sep 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement math pygame.math New API This pull request may need extra debate as it adds a new class or function to pygame
Projects
None yet
Development

No branches or pull requests

3 participants