Skip to content
This repository was archived by the owner on Sep 21, 2025. It is now read-only.

Ben Nguyen - Completed Tasks 1-4#182

Closed
benpvnguyen wants to merge 5 commits into
UWARG:mainfrom
benpvnguyen:main
Closed

Ben Nguyen - Completed Tasks 1-4#182
benpvnguyen wants to merge 5 commits into
UWARG:mainfrom
benpvnguyen:main

Conversation

@benpvnguyen

Copy link
Copy Markdown

No description provided.

"""
Calculate Euclidean distance between two locations.
"""
return (self._calculate_distance_squared(pos1, pos2)) ** 0.5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the square root method is computationally expensive, can you think of another distance calculation method to use?

Comment on lines +170 to +191
def _find_closest_landing_pad(
self, reference_point: location.Location, landing_pad_locations: "list[location.Location]"
) -> location.Location:
"""
Find the landing pad closest to the reference point (waypoint).
Uses squared distance to avoid expensive square root calculations.
"""
if not landing_pad_locations:
return None

closest_pad = None
min_distance_squared = float("inf") # Initialize with infinity

for landing_pad in landing_pad_locations:
# Use squared distance to avoid square root calculation
distance_squared = self._calculate_distance_squared(reference_point, landing_pad)

if distance_squared < min_distance_squared:
min_distance_squared = distance_squared
closest_pad = landing_pad

return closest_pad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need this for this part of the task, it's just the one landing pad

Comment on lines +193 to +230
def _calculate_relative_movement(self, current_pos: location.Location) -> location.Location:
"""
Calculate relative movement towards current target, ensuring it stays within boundary.

Returns:
Location object with relative movement, or None if no valid movement
"""
# Use current target (either waypoint or closest landing pad)
target = self.current_target
if target is None:
target = self.waypoint # Fallback to waypoint

# Calculate direct vector to target
dx = target.location_x - current_pos.location_x
dy = target.location_y - current_pos.location_y

# If we're very close, no movement needed
if abs(dx) < 0.001 and abs(dy) < 0.001:
return None

# Calculate target position after movement
target_x = current_pos.location_x + dx
target_y = current_pos.location_y + dy

# Clamp to flight boundary
target_x = max(self.boundary_min, min(self.boundary_max, target_x))
target_y = max(self.boundary_min, min(self.boundary_max, target_y))

# Calculate final relative movement based on clamped target
final_dx = target_x - current_pos.location_x
final_dy = target_y - current_pos.location_y

# If no movement would occur, return None
if abs(final_dx) < 0.001 and abs(final_dy) < 0.001:
return None

# Return relative movement as Location object
return location.Location(final_dx, final_dy)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need this as there's a function for relative distance calculation already

Comment on lines +80 to +86
if not self.closest_landing_pad_calculated and landing_pad_locations:
# Calculate closest landing pad to waypoint (not current position)
self.closest_landing_pad = self._find_closest_landing_pad(
self.waypoint, landing_pad_locations
)
self.closest_landing_pad_calculated = True
print(f"Closest landing pad to waypoint: {self.closest_landing_pad}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again, for this task, you shouldn't need to calculate this

Comment on lines +94 to +96
if self._calculate_distance_squared(self.waypoint, self.closest_landing_pad) < 0.001:
# Waypoint and landing pad are essentially the same, go directly there
self.current_target = self.closest_landing_pad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a good thing to consider! but for the sake of the complexity of your code, assume the waypoint and the landing pad won't be in the same place :)

Comment on lines +94 to +98
def _calculate_distance(self, pos1: location.Location, pos2: location.Location) -> float:
# Calculate distance between two locations.
dx = pos2.location_x - pos1.location_x
dy = pos2.location_y - pos1.location_y
return dx * dx + dy * dy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this alone doesn't fully calculate the distance

Comment on lines +194 to +222
def _calculate_relative_movement(
self, current_pos: location.Location, target: location.Location
) -> location.Location:

# Calculate relative movement towards target, ensuring it stays within boundary.

# Calculate direct vector to target
dx = target.location_x - current_pos.location_x
dy = target.location_y - current_pos.location_y

# If we're very close, no movement needed
if abs(dx) < 0.001 and abs(dy) < 0.001:
return None

# Calculate target position after movement
target_x = current_pos.location_x + dx
target_y = current_pos.location_y + dy

# Clamp to flight boundary (copied from simple waypoint)
target_x = max(self.boundary_min, min(self.boundary_max, target_x))
target_y = max(self.boundary_min, min(self.boundary_max, target_y))

# Calculate final relative movement based on clamped target
final_dx = target_x - current_pos.location_x
final_dy = target_y - current_pos.location_y

# If no movement would occur, return None
if abs(final_dx) < 0.001 and abs(final_dy) < 0.001:
return None

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't need this as mentioned before


dx = pos2.location_x - pos1.location_x
dy = pos2.location_y - pos1.location_y
return (dx * dx + dy * dy) ** 0.5

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use different distance formula as mentioned

Comment on lines +99 to +101
elif self._calculate_distance(self.waypoint, self.closest_landing_pad) <= 0.1:
# If Waypoint and landing pad are same location, go directly to landing pad
target = self.closest_landing_pad

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned before, good case to consider but you can assume (to reduce complexity of the code) that this will not happen

command = commands.Command.create_null_command()
else:
# We need to move towards the target
relative_movement = self._calculate_relative_movement(current_position, target)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the removal of _calculate_relative_movement also changes this logic as well

# Calculate distance to waypoint
distance = self._calculate_distance_squared(current_position, self.waypoint)

if distance <= self.acceptance_radius:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this becomes incorrect if you're using the squared distance method

@ellyokes253

Copy link
Copy Markdown
Contributor

reviewed!

@ellyokes253

Copy link
Copy Markdown
Contributor

lgtm!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants