Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Write a class Point with the attributes x and y, and a method distance_to(other) that gives the distance to another point. Check it with the points (0, 0) and (3, 4).
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The distance between two points is the square root of the sum of the squares of the differences: . The module math has hypot, which does exactly this.
Check. The differences are 3 and 4, and the distance is 5.0, the long side of the 3, 4, 5 triangle. It is the same in both directions. The distance of a point to itself is 0.0.
Answer: 5.0, 5.0 and 0.0.
Log in to join the discussion.
Loading…