2016年4月25日 星期一

(Zj) a871: 11. Museum Area [逆時針排序點,外積]

http://zerojudge.tw/ShowProblem

我的想法是:先把點點按照順or逆時針排序(直接看code比較快),然後取一個定點,算出那個定點和另外兩個連續的點構成的三角形面積。

http://codepad.org/WBmoELb5

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <iomanip>
using namespace std;

const int MAX_N = 12;

struct Point {
 double x;
 double y;
} point[MAX_N];

double cross(Point o,Point a,Point b) {
 return (a.x - o.x) * (b.y - o.y) - (a.y - o.y) * (b.x - o.x);
}

double dis(Point a,Point b) {
 return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}

double get_area(Point a,Point b,Point c) {
 double p = dis(a,b);
 double q = dis(b,c);
 double r = dis(a,c);
 double s = (p+q+r)/2;
 return sqrt(s*(s-p)*(s-q)*(s-r));
}

bool cmp(const Point &p1,const Point &p2) {
 return cross(point[0],p1,p2) < 0.0;
}

int main () {
 int n;
 while (scanf("%d",&n) != EOF) {
  for (int X=0;n>X;X++) {
   double i,j;
   cin >> i >> j;
   point[X].x=i;
   point[X].y=j;
  }
  sort(point+1,point+n,cmp);
  double ans = 0.0;
  for (int x=1;n-1>x;x++) {
   ans+=get_area(point[0],point[x],point[x+1]);
  }
  cout<<fixed<<setprecision(2)<<ans<<endl;
 }
}

沒有留言:

張貼留言