Utility functions for working with 2D geometry degrees, vectors, and constants.

Most Python libraries (such as the math module) expect angles to be specified in radians. While this is a good convention, new students may have trouble using radians, so this module provides degree-accepting variants of the standard trig functions, as well as a utility to convert between radians and degrees.

Students often also represent 2D vectors by their components, so we also provide conversions to vector magnitude and vector angle.

The mathematical constants PI and E are also exported so that students don’t have to import the separate math module for those constants.

With all of these functions, the numerical precision is only as accurate as that provided by the underlying math module. = 3.141592653589793

The mathematical constant e, equal to the base of the natural logarithm.[source]

Convert an angle from radians to degrees.


print(to_degrees(PI))  # => 180.0
print(to_degrees(PI / 2))  # => 90.0

There is no restriction of the domain of this function, but that also means that the codomain is unrestricted. As a concrete example:

print(to_degrees(-PI))  # => -180.0
print(to_degrees(4 * PI))  # => 720.0
Parameters:radians – An angle (in radians)
Returns:The same angle (in degrees)[source]

Convert an angle from degrees to radians.


print(to_radians(180.0))  # => 3.141592653589793
print(to_radians(90.0))  # => 1.5707963267948966

There is no restriction of the domain of this function, but that also means that the codomain is unrestricted. As a concrete example:

print(to_radians(-180.0))  # => -3.141592653589793
print(to_radians(720))  # => 12.566370614359172
Parameters:degrees – An angle (in degrees)
Returns:The same angle (in radians)[source]

Return the trigonometric sine of an angle (in degrees).


print(sin_degrees(30))  # => 0.49999999999999994
print(sin_degrees(0))  # => 0.0
print(sin_degrees(45.0))  # => 0.7071067811865475
Parameters:angle – An angle (in degrees)
Returns:The sine of the given angle[source]

Return the trigonometric cosine of an angle (in degrees).


print(cos_degrees(60))  # => 0.5000000000000001
print(cos_degrees(90))  # => 6.123233995736766e-17
print(cos_degrees(45.0))  # => 0.7071067811865476
Parameters:angle – An angle (in degrees)
Returns:The cosine of the given angle[source]

Return the trigonometric tangent of an angle (in degrees).


print(tan_degrees(0))  # => 0.0
print(tan_degrees(90))  # => 1.633123935319537e+16
print(tan_degrees(45.0))  # => 0.9999999999999999
Parameters:angle – An angle (in degrees)
Returns:The tangent of the given angle, y)[source]

Compute the magnitude of a vector given by two components.

To compute the magnitude of vector with two components:

print(vector_magnitude(3, 4))  # => 5.0

To compute the magnitude of a GPoint:

pt = GPoint(3, 4)
print(vector_magnitude(*pt))  # => 5.0
  • x – x-coordinate of vector
  • y – y-coordinate of vector

magnitude of the vector (x, y), y)[source]

Return the angle (in degrees) from the origin to the given point.

The angle is measured counterclockwise from the positive x-axis, as is standard.

This function accounts for the fact that, in our graphical coordinate system, the y-axis is flipped (with respect to the traditional Cartesian plane). That is, in the graphical coordinate system, the y-coordinate of a point increases as that point descends on the screen.


print(vector_angle(3, 3))  # => -45.0
print(vector_angle(3, 3 * math.sqrt(3)))  # => -60.00000000000001
print(vector_angle(3 * math.sqrt(3), 3))  # => -29.999999999999996

The returned angles in these examples are negative because the y-coordinate represents a point in the fourth quadrant.

  • x – x-coordinate of point
  • y – y-coordinate of point

angle (in degrees) from (0, 0) to (x, -y), base=10)[source]

Count the number of digits in a number in some base.

Any non-integral part of the number is discarded: count_digits(3.1415) is the same as count_digits(3).

The provided n must be finite and the provided base must be a positive integer, otherwise an error is raised.


count_digits(4)  # => 1
count_digits(41)  # => 2
count_digits(413)  # => 3

count_digits(9.9)  # => 1
count_digits(10.1)  # => 2

count_digits(-15)  # => 2
count_digits(-314.15)  # => 3

count_digits(3, base=2)  # => 2
count_digits(4, base=2)  # => 3
count_digits(8, base=3)  # => 2
count_digits(41, base=5)  # => 3

Invalid Usage:

count_digits(float('inf'))  # raises error
count_digits(5, base=-3)  # raises error
count_digits(5, base=2.2)  # raises error
  • n – number whose digits to count
  • base – base system to use

the number of digits of n when written in the given base.