-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharea.cpp
More file actions
36 lines (28 loc) · 738 Bytes
/
area.cpp
File metadata and controls
36 lines (28 loc) · 738 Bytes
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
#include<iostream>
using namespace std;
struct Point{
float x,y;
};
// O(1)
float area(Point a, Point b, Point c){
return (a.x*(b.y-c.y) + b.x*(c.y - a.y) + c.x*(a.y - b.y))/2.0f;
}
// O(N)
float calculateAreaOfHull(Point* hull,int N){
float sum = 0;
for(int i = 1; i < N - 1; i++)
sum += abs(area(hull[0],hull[i],hull[i+1])); // O(1)
return sum;
}
int main(){
// Square with 1 unit area
Point test1[4] = {{0,0},{0,1},{1,0},{1,1}};
cout<<calculateAreaOfHull(test1,4)<<endl;
// Rohmbus with 1 unit area
Point test2[4] = {{0,0},{1,1},{1,0},{2,1}};
cout<<calculateAreaOfHull(test2,4)<<endl;
// Hexagon
Point test3[6] = {{0,0},{1,0},{2,1},{1,2},{0,2},{-1,1}};
cout<<calculateAreaOfHull(test3,6)<<endl;
return 0;
}