Distances
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.
For some exercises, you will submit a python file that will then be automatically graded. For example, the prompt might be to write a program to compute and print the Manhattan distance between two points, (x_1, y_1) and (x_2, y_2). We might ask you to instantiate some variables at the top of the program, and the ultimate file could look like:
# Variable instantiation
x1 = 12
y1 = 20
x2 = 3
y2 = 4
# The code you would add
print(abs(x1-x2) + abs(y1-y2))
Description
In this section, you'll write a short program to compute the Euclidean distance between two points, (x_1, y_1) and (x_2, y_2).
The first four lines of your program should set up variables to hold the relevant values (x_1, y_1, x_2, and y_2). So the first four lines of your program should look like, for example:
x1 = 1
y1 = 2
x2 = 3
y2 = 4
Your program should print a single number, the distance between the two points.
Notes
- 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)?
- Python isn't picky about variable names, but our checker is. It's important that you call the variables mentioned above
x1,y1,x2, andy2, rather than giving them other names.
Submission
When you are ready, upload your Python file below:
Description
Now, write a short program 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.
The first five lines of your program should set up variables to hold the relevant values (p_x, p_y, a, b, and c). So the first four lines of your program should look like, for example:
px = 1
py = 2
a = 3
b = 4
c = 5
Your program should print a single number, the distance between the point and the line.
Notes
- We don't necessarily expect you to remember or rederive this formula; try to rederive it on your own, and use web search if you need help.
- 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
xwith:abs(x) - Python isn't picky about variable names, but our checker is. It's important that you call the variables mentioned above
a,b,c,px, andpy, rather than giving them other names.
Submission
When you are ready, upload your Python file below:
6.s090