Search code examples
pythonpygamepythagorean

How Do I Uses Pythagoras In Python?


How do you write the Pythagoras equation in python? Is it written as shown below or not. If not please describe why this is incorrect and how I can correct it.

math.sqrt(XLength^2+YLength^2)


Solution

  • In pygame you can use pygame.math.Vector2.length:

    l = pygame.math.Vector2(XLength, YLength).length()
    

    In general you can use math.hypot:

    l = math.hypot(XLength, YLength)
    

    ^ however is the Bitwise Exclusive Or operator (see opertor). The Exponentiation operator is **:

    l = math.sqrt(XLength**2 + YLength**2)