Overview
The Location
class provides a robust interface for managing two-dimensional coordinates.
Attributes
x
intHorizontal coordinate representing the x-axis position.
y
intVertical coordinate representing the y-axis position.
Methods
add(direction: Direction)
Adds the given direction to the current location.
Parameters
The direction to add to the current location.
Returns
A new Location object one unit away in the given direction.
Example:
loc = create_location(3, 2)
north_location = loc.add(Direction.NORTH)
# Returns location at (3, 3)
direction_to(location: Location)
Calculate the direction between the current location and a target location.
Parameters
The target location.
Returns
A Direction enum value representing the direction between locations.
Example:
start = create_location(0, 0)
goal = create_location(0, 5)
dir = start.direction_to(goal)
# Returns Direction.NORTH
distance_to(location: Location)
Warning: Incorrect Heuristic
This should not be used as your A* heuristic! Using an improper heuristic can lead to suboptimal or incorrect pathfinding results.
Calculates the squared distance between the current location and the given location.
Parameters
The location to which the distance is calculated.
Returns
The squared distance to the given location.
Example:
start = create_location(1, 2)
goal = create_location(3, 5)
dist = start.distance_to(goal)
# Returns 13
Factory Functions
create_location(x: int, y: int)
Create a new Location object at the specified coordinates.
Parameters
The x-coordinate of the location.
The y-coordinate of the location.
Returns
A new Location object at the specified coordinates.
Example:
loc = create_location(5, 4)
# Returns a Location object with the coordinates (5, 4)
Additional Notes
The Location
class is designed to support comparison operations. Locations can be
compared using standard operators such as ==
, !=
, <
, >
, <=
, and >=
.
This allows for easy sorting and comparison of locations. For example, locations can
be stored in sorted containers like lists, sets, and heaps.