Skip to content

Position

gradysim.protocol.position

This module contains some helpers useful when working with positions. Positions are used to localize the nodes inside the python simulation.

Position = Tuple[float, float, float] module-attribute

Represents a node's position inside the simulation. It is a tuple of three floating point numbers representing the euclidean coordinates of the node.

rotate_position_2d(position, angle_degrees)

Rotates a position's (x, y) coordinates around the z-axis by angle_degrees (in degrees) counter-clockwise, keeping the z coordinate unchanged.

Parameters:

Name Type Description Default
position Position

The original (x, y, z) position.

required
angle_degrees float

The angle in degrees to rotate by (counter-clockwise).

required

Returns:

Type Description
Position

The rotated (x, y, z) position.

Source code in gradysim/protocol/position.py
def rotate_position_2d(position: Position, angle_degrees: float) -> Position:
    """
    Rotates a position's (x, y) coordinates around the z-axis by angle_degrees (in degrees)
    counter-clockwise, keeping the z coordinate unchanged.

    Args:
        position: The original (x, y, z) position.
        angle_degrees: The angle in degrees to rotate by (counter-clockwise).

    Returns:
        The rotated (x, y, z) position.
    """
    rad = math.radians(angle_degrees)
    cos_a = math.cos(rad)
    sin_a = math.sin(rad)
    x, y, z = position
    return x * cos_a - y * sin_a, x * sin_a + y * cos_a, z

squared_distance(start, end)

Calculates the squared distance between two positions.

Parameters:

Name Type Description Default
start Position

First position

required
end Position

Second position

required

Returns:

Type Description
float

The distance squared

Source code in gradysim/protocol/position.py
def squared_distance(start: Position, end: Position) -> float:
    """
    Calculates the squared distance between two positions.

    Args:
        start: First position
        end: Second position

    Returns:
        The distance squared
    """
    return (end[0] - start[0]) ** 2 + (end[1] - start[1]) ** 2 + (end[2] - start[2]) ** 2