[codevs 1249]多邊形的面積,codevs1249面積

來源:互聯網
上載者:User

[codevs 1249]多邊形的面積,codevs1249面積

題解:

模板,利用Cross叉積表示的是有向面積,同樣可以求凹多邊形的面積。

代碼:

總時間耗費: 5ms
總記憶體耗費: 256 kB

#include<cstdio>#include<cmath>#include<vector>#include<algorithm>using namespace std;const int maxn = 100 + 10;int n;struct Point {    int x, y;    Point(int x=0, int y=0):x(x),y(y) {}}P[maxn];typedef Point Vector;Vector operator + (Vector A, Vector B) { return Vector(A.x+B.x, A.y+B.y); }Vector operator - (Vector A, Vector B) { return Vector(A.x-B.x, A.y-B.y); }Vector operator * (Vector A, int p) { return Vector(A.x*p, A.y*p); }Vector operator / (Vector A, int p) { return Vector(A.x/p, A.y/p); }bool operator < (const Vector& a, const Vector& b) {    return a.x < b.x || (a.x == b.x && a.y < b.y);}int Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }int Length(Vector A) { return sqrt(Dot(A, A)); }int Angle(Vector A, Vector B) { return acos(Dot(A, B) / Length(A) / Length(B)); }int Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; }int Area2(Point A, Point B, Point C) { return Cross(B-A, C-A); }int PolygonArea() {    int area = 0;    for(int i = 1; i < n-1; i++)         area += Cross(P[i]-P[0], P[i+1]-P[0]);    return area/2;}int main() {    scanf("%d", &n);    for(int i = 0; i < n; i++)         scanf("%d%d", &P[i].x, &P[i].y);    printf("%d\n", PolygonArea());    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.