1、已知三顶点(x1,y1) (x2,y2) (x3,y3)求三角形面积:
2、任意凸多边形(x1,y1) (x2,y2) (x3,y3)......(xn,yn)面积公式:
其中S在算完后需要再取一个绝对值。
这里有道例题:例题链接
这个是代码:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<math.h>#include<cmath>using namespace std;const int maxn = 10;typedef long long ll;typedef unsigned long long ull;struct Node {int x;int y;}p[maxn];int n;int main(){cin >> n;for (int i = 1; i <= n; i++) {cin >> p[i].x >> p[i].y;}int s = 0;if (n == 1 || n == 2) {cout << 0 << endl;return 0;}for (int i = 1; i<n; i++) {s += (p[i].x*p[i + 1].y - p[i + 1].x*p[i].y);}s += (p[n].x*p[1].y - p[1].x*p[n].y);s = abs(s);if (s % 2 == 0)cout << s / 2 << endl;elsecout << s / 2 + 1 << endl;return 0;}