Distances

The questions below are due on Friday June 14, 2024; 10:00:00 PM.
 
You are not logged in.

Please Log In for full access to the web site.
Note that this link will take you to an external site (https://shimmer.mit.edu) to authenticate, and then you will be redirected back to this page.

1) Instructions

For some exercises, you will submit a python file that will then be automatically graded. For example, the prompt might be to write a function to compute and print the Manhattan distance between two points, (x_1, y_1) and (x_2, y_2).

def manhattan_distance(x1, y1, x2, y2):
    """
    Given two points (x1, y1) and (x2, y2), calculate and print the manhattan
    distance between the points.
    """
    # The code you would add
    return abs(x1-x2) + abs(y1-y2)

if __name__ == "__main__":
    # Code for testing:
    print("manhattan distance between (0, 4) and (6, 2) is 8")
    result = manhattan_distance(0, 4, 6, 2)
    print(result)

2) Part 1: Point-to-Point

Description

In this section, you'll write a short function to compute the Euclidean distance between two points, (x_1, y_1) and (x_2, y_2). This can be calculated with the following formula: \sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}

Create a function euclidean_distance that, given arguments x1, y1, x2, and y2, returns the distance between the two points.

Starter code:

def euclidean_distance(x1, y1, x2, y2):
    """
    Given two points (x1, y1) and (x2, y2), calculate and return the euclidean
    distance between the points.
    """
    return None  # your code here

if __name__ == "__main__":
    # these lines are to help you test your code to make sure it is working -
    # feel free to add your own tests!
    print("euclidean distance between (0, 4) and (6, 2) is 6.32")
    print(euclidean_distance(0, 4, 6, 2))
    print("euclidean distance between (-3, 1) and (-3, -5) is 6.0")
    print(euclidean_distance(-3, 1, -3, -5))

Link to starter code file

Notes

  • Link to relevant part of reading
  • Note that you'll need a way to find a square root to do this calculation. How can you do this using the pieces we already know (addition, subtraction, multiplication, division, exponentiation)?

Submission

When you are ready, upload your Python file below:

  No file selected

3) Part 2: Point-to-Line

Description

Now, write a short function to compute the Euclidean distance between a point (p_x, p_y) in two-dimensional space and a line specified by ax + by + c = 0. This can be calculated with the following formula: \frac{\lvert a * p_x + b * p_y + c \rvert} {\sqrt{a^2 + b^2}}

Starter code:

def point_line_distance(px, py, a, b, c):
    """
    Given a point (px, py) and line coefficients a, b, and c, calculate and
    return the distance between the point and the line ax+by+c=0.
    """
    return None  # your code here

if __name__ == "__main__":
    # Local testing -- feel free to add your own tests as well!
    print("Distance between point (1, 2) and line  3x + 4y + 5 = 0 is 3.2")
    print(point_line_distance(1, 2, 3, 4, 5))

Link to starter code file

Your function should return a single number, the distance between the point and the line.

Notes

  • Note that you'll need to find an absolute value in order to compute this value. You can find the absolute value of a number x with: abs(x)

Submission

When you are ready, upload your Python file below:

  No file selected

Next Exercise: Packaging

Back to exercises