Skip to content

Commit de81f05

Browse files
authored
Create Area of Polygon.cpp
1 parent 9ce3b6c commit de81f05

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

Area of Polygon.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <bits/stdc++.h>
2+
#include <ext/rope>
3+
using namespace std;
4+
using namespace __gnu_cxx;
5+
6+
using ci = const int;
7+
using ld = long double;
8+
using llint = long long;
9+
using ullint = unsigned long long;
10+
using pii = pair <int,int>;
11+
using pcc = pair <char,char>;
12+
using pss = pair <string,string>;
13+
using vi = vector <int>;
14+
using vb = vector <bool>;
15+
using vii = vi::iterator;
16+
17+
#define INF (1<<30)
18+
#define MOD 1000000007
19+
#define mp make_pair
20+
#define mt make_tuple
21+
#define all(c) c.begin(), c.end()
22+
#define ms(name,val) memset(name, val, sizeof name)
23+
#define np nullptr
24+
25+
26+
struct Point
27+
{
28+
int x, y;
29+
};
30+
31+
int n;
32+
vector <Point> v;
33+
34+
/*
35+
Polygon area is half of this matrix's discriminant
36+
37+
X0 Y0
38+
X1 Y1
39+
X2 Y2
40+
...
41+
Xn-1 Yn-1
42+
43+
(X0*Y1 - X1*Y0)...
44+
*/
45+
46+
double polygonArea()
47+
{
48+
double area = 0;
49+
50+
for (int t1 = 0; t1 < n-1; ++t1)
51+
area += (v[t1].x*v[t1+1].y - v[t1+1].x*v[t1].y);
52+
53+
area += (v[n-1].x*v[0].y - v[0].x*v[n-1].y);
54+
return fabs(area) / 2.0;
55+
}
56+
57+
int main()
58+
{
59+
ios_base::sync_with_stdio(0);
60+
//cin.tie(0);
61+
62+
cin >> n;
63+
v.resize(n);
64+
65+
for (int t1 = 0; t1 < n; ++t1)
66+
cin >> v[t1].x >> v[t1].y;
67+
68+
cout << polygonArea() << '\n';
69+
70+
71+
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)