Valid Boomerang
We need to check if three given points form a boomerang. A set of points forms a boomerang if all points are distinct and they are not in a straight line.
We can check for distinct points by using a set, which removes any duplicates. Then, we can check if the points are in a straight line by checking the slope between the pairs of points. The points are in a straight line if the slopes are equal.
Here is a Python solution:


Now, the code first calculates the differences in x and y coordinates needed to compare slopes. Then it calculates the products for each pair of differences. After that, it checks whether the two products are equal, which would indicate the slopes are the same. Finally, it returns whether the slopes are not the same (using not is_same_slope
). If the slopes aren’t the same, then the points aren’t on the same line, and they form a boomerang.
In this code:
 We create a set of tuples from the points, which removes any duplicates because sets only contain unique items. If the length of this set is less than 3, then not all points are distinct, and we return False.
 We calculate the slope between the pairs of points: (points[0], points[1]) and (points[1], points[2]). The slope between two points (x1, y1) and (x2, y2) is given by (y2  y1) / (x2  x1). However, to avoid division by zero issues, we compare the cross products (y2  y1) * (x3  x2) and (y3  y2) * (x2  x1). If these are equal, then the slopes are equal, and the points are in a straight line.
 If the slopes are not equal, then the points are not in a straight line, and we return True, indicating that the points form a boomerang.
Q&A
Explain in simple terms and elaborate: Assuming three points are A, B, C.
The first idea is that, calculate the area of ABC. We can reuse the conclusion and prove in 812. Largest Triangle Area
The other idea is to calculate the slope of AB and AC. K_AB = (p[0][0]  p[1][0]) / (p[0][1]  p[1][1]) K_AC = (p[0][0]  p[2][0]) / (p[0][1]  p[2][1])
We check if K_AB != K_AC, instead of calculate a fraction.
Sure, let’s take these two ideas and explain them in a more simple way:
Calculating the Area:
We can check if the three points form a boomerang by calculating the area of the triangle they would form. If the area is zero, it means the points are in a straight line, not a boomerang. Here’s how it works:
Let’s say our points are A(x1,y1), B(x2,y2), and C(x3,y3). The formula to calculate the area of a triangle given its vertices is:
Area = 1/2 * abs((x1*(y2y3) + x2*(y3y1) + x3*(y1y2)))
In this formula,
abs
refers to the absolute value. If this formula results in zero, it means that all three points are on the same line (so, it’s not a boomerang).Checking the Slope:
Another way to check if the points are in a straight line (and thus not a boomerang) is to compare the slopes of the lines formed by the points.
The slope of a line through points A and B can be calculated as
(y2y1) / (x2x1)
. This gives us how much ‘y’ changes for each change in ‘x’. If we calculate this for lines AB and AC, and the slopes are different, then the three points are not in a straight line.However, to avoid issues with division by zero (when x1 equals x2), we can compare without actually dividing. We do this by checking if
(x1x2) * (y1y3) == (x1x3) * (y1y2)
. If these are not equal, then the slopes are different, and the points do not form a straight line.
If the points are not in a straight line, they form a boomerang, so we return true in these cases. If they are in a straight line (zero area or same slope), they don’t form a boomerang, and we return false.