Question: Equatable and Comparable in the Swift Programming: The Big Nerd Ranch Guide (2nd Ed.) e-book: Points current conformance to Comparable yields some confusing results. let
"Equatable and Comparable" in the Swift Programming: The Big Nerd Ranch Guide (2nd Ed.) e-book: Points current conformance to Comparable yields some confusing results. let c = Point(x: 3, y: 4) let d = Point(x: 2, y: 5) let cGreaterThanD = (c > d) // false let cLessThanD = (c < d) // false let cEqualToD = (c == d) // false As the above example demonstrates, the trouble arises in comparing two points when one points x and y properties are not both larger than the other points. In actuality, it is not reasonable to compare two points in this manner. Fix this problem by changing Points conformance to Comparable. Calculate each points Euclidean distance from the origin instead of comparing x and y values. This implementation should return true for a < b when a is closer to the origin than b. Use the formula shown in below Figure 25.1 to calculate a points Euclidean distance. Figure 25.1 Euclidean distance Euclidean distance
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
