From Evernote: |
uva 10002 |
題目要找質心,重力均勻的話也就是找重心。
多邊形找重心的方式:先取有序的凸邊形的點,利用任意一個點(我用(0.0,0.0))與有序多邊形的點組成多個三角形
每個三角形的點x乘上面積,y乘上面積,各別加在紀錄ansx,ansy上
也把每個三角形的面積ansa紀錄並累加起來
之後ansx/(ansa*3),ansy/(ansa*3)
即是答案
差積順時針為負,逆時針為正
[sourcecode language="cpp"]
//============================================================================
// Name : Center of Masses.cpp
// Date : 2013 2013/1/29 上午10:50:27
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 105
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
struct point{
double x,y;
point(double x,double y):x(x),y(y){};
point(){x=0.0;y=0.0;};
};
point p[M],ch[M];
int n;
void swap(point& a,point &b){point c=a; a=b;b=c;}
double cross(point a,point b,point o){
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
bool cmpangle(const point& a,const point& b){
return xdy(cross(a,b,p[0]),0.0);
}
void fcen(){
int minx=oo,miny=oo;
int nc;
for(int i=0;i<n;i++){
if(p[i].x<minx){
minx=p[i].x;
miny=p[i].y;
nc=i;
}
else if(p[i].x==minx&&p[i].y<miny)
miny=p[i].y,nc=i;
}
swap(p[0],p[nc]);
}
void solve(){
point ans;
double a=0.0;
ans.x=0.0;ans.y=0.0;
for(int i=0;i<n;i++){
double tmp=cross(p[i],p[i+1],point(0.0,0.0));
ans.x+=(p[i].x+p[i+1].x)*tmp;
ans.y+=(p[i].y+p[i+1].y)*tmp;
a+=tmp;
}
ans.x=ans.x/(3*a);
ans.y=ans.y/(3*a);
printf("%.3f %.3f\n",ans.x,ans.y);
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d",&n)&&n>=3){
for(int i=0;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
fcen();
sort(&p[1],&p[n],cmpangle);
// for(int i=0;i<n;i++)
// debug("%.3f %.3f\n",p[i].x,p[i].y);
p[n]=p[0];
solve();
}
}
[/sourcecode]
留言
張貼留言