Valid Boomerang
Problem
Given an array points
where points[i] = [xi, yi]
represents a point on the X-Y plane, return true
if these points are a boomerang.
A boomerang is a set of three points that are all distinct and not in a straight line.
Constraints
points.length == 3
points[i].length == 2
0 <= xi, yi <= 100
Solution
The problem Valid Boomerang
can be solved by first checking if all points are distinct and then checking if they are in a straight line.
Implementation
static const int fast_io = []()
{
std::ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
return 0;
}();
class Solution
{
public:
bool isBoomerang(vector<vector<int>> &points)
{
if (points[0] == points[1] || points[1] == points[2] || points[2] == points[0])
return false;
double old_gradient = DBL_MIN;
for (int i = 1; i < 3; i++)
{
double new_gradient = points[i][0] - points[i - 1][0];
new_gradient = new_gradient ? (points[i][1] - points[i - 1][1]) / new_gradient : DBL_MAX;
if (old_gradient == new_gradient)
return false;
old_gradient = new_gradient;
}
return true;
}
};