forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1037. Valid Boomerang.cpp
54 lines (31 loc) · 986 Bytes
/
1037. Valid Boomerang.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
A boomerang is a set of 3 points that are all distinct and not in a straight line.
Given a list of three points in the plane, return whether these points are a boomerang.
Example 1:
Input: [[1,1],[2,3],[3,2]]
Output: true
Example 2:
Input: [[1,1],[2,2],[3,3]]
Output: false
Note:
points.length == 3
points[i].length == 2
0 <= points[i][j] <= 100
// Using Herons Formula
// If area of triangle is zero, => collinear
class Solution {
public:
bool isBoomerang(vector<vector<int>>& points) {
int x1=points[0][0];
int y1=points[0][1];
int x2=points[1][0];
int y2=points[1][1];
int x3=points[2][0];
int y3=points[2][1];
double a = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
double b = sqrt((x3-x2)*(x3-x2)+(y3-y2)*(y3-y2));
double c = sqrt((x1-x3)*(x1-x3)+(y3-y1)*(y3-y1));
double s = (a+b+c)/2;
double area = sqrt(s*(s-a)*(s-b)*(s-c));
return area ? true: false;
}
};