Ben Nguyen - Completed Tasks 1-4#182
Conversation
| """ | ||
| Calculate Euclidean distance between two locations. | ||
| """ | ||
| return (self._calculate_distance_squared(pos1, pos2)) ** 0.5 |
There was a problem hiding this comment.
using the square root method is computationally expensive, can you think of another distance calculation method to use?
| 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 |
There was a problem hiding this comment.
you shouldn't need this for this part of the task, it's just the one landing pad
| 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) |
There was a problem hiding this comment.
you shouldn't need this as there's a function for relative distance calculation already
| 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}") |
There was a problem hiding this comment.
again, for this task, you shouldn't need to calculate this
| 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 |
There was a problem hiding this comment.
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 :)
| 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 |
There was a problem hiding this comment.
this alone doesn't fully calculate the distance
| 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
use different distance formula as mentioned
| 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 |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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: |
There was a problem hiding this comment.
this becomes incorrect if you're using the squared distance method
|
reviewed! |
|
lgtm! |
No description provided.