Write a function calculating distance between Point a and Point b. Input coordinates fit…
- User
Write a function calculating distance between Point a and Point b.
Input coordinates fit in range − 50 ⩽ x , y ⩽ 50 −50⩽x,y⩽50. Tests compare expected result and actual answer with tolerance of 1e-6.
- Assistant
Certainly! Here's a function that calculates the Euclidean distance between two points ( a ) and ( b ) in a 2D plane. The coordinates of the points should be within the range ( -50 \leq x, y \leq 50 ).
Python Function
import math def calculate_distance(a, b): """ Calculate the Euclidean distance between two points a and b. Parameters: a (tuple or list): Coordinates of point a as (x, y). b (tuple or list): Coordinates of point b as (x, y). Returns: float: The distance between point a and point b. """ dx = a[0] - b[0] dy = a[1] - b[1] distance = math.hypot(dx, dy) return distanceUsage Example:
point_a = (x1, y1) point_b = (x2, y2) # Replace x1, y1, x2, y2 with actual numerical values within -50 and 50 point_a = (10, -20) point_b = (-15, 25) dist = calculate_distance(point_a, point_b) print(f"The distance between points a and b is: {dist}")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.