ACM--多边形重心–-HDOJ 1115-- Lifting the Stone
来源:互联网 发布:秋冬防晒 知乎 编辑:程序博客网 时间:2024/06/05 08:19
HDOJ题目地址:传送门
Lifting the Stone
245 00 5-5 00 -541 111 111 111 11
0.00 0.006.00 6.00
计算多边形重心方法:
(1)划分多边形为三角形:
以多边形的一个顶点V为源点(V可取输入的第一个顶点),作连结V与所有非相邻顶点的线段,即将原N边形或分为(N-2)个三角形;
(2)求每个三角形的重心和面积:
设某个三角形的重心为G(cx,cy),顶点坐标分别为A1(x1,y1),A2(x2,y2),A3(x3,y3),则有cx = (x1 + x2 + x3)/3.同理求得cy。求面积的方法是s = ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2,当A1,A2,A3顺时针排列时取-,否则取正(此定理不证)。事实上,在求每个三角形时不需要辨别正负,之后有方法抵消负号,见下述。
(3)求原多边形的重心:
公式:cx = (∑ cx[i]*s[i]) / ∑s[i]; cy = (∑ cy[i]*s[i] ) / ∑s[i];其中(cx[i], cy[i]), s[i]分别是所划分的第i个三角形的重心坐标和面积。由题“ connect the points in the given order”知每个s[i]的正负号相同,故而∑ cx[i]*s[i]能与∑s[i]消号,所以根本不需要在第(2)步判断每个s[i]的正负。另外,在(2)中求每个重心坐标时要除以3,实际上不需要在求每个三角形坐标时都除以3,只需要求出∑ cx[i]*s[i]后一次性除以3即可。即是多边形重心坐标变为:cx = (∑ cx[i]*s[i]) / (3*∑s[i]); cy = (∑ cy[i]*s[i] ) / (3*∑s[i]);
总结:
每个三角形重心:cx = x1 + x2 + x3;cy同理。
每个三角形面积:s = ( (x2 - x1) * (y3 - y1) - (x3 - x1) * (y2 - y1) ) / 2;
多边形重心:cx = (∑ cx[i]*s[i]) / (3*∑s[i]); cy = (∑ cy[i]*s[i] ) / (3*∑s[i]);
#include <iostream>#include <cstdio>using namespace std;int T, N, x[3], y[3];double sumx, sumy, sumarea;int main() { scanf("%d", &T); while(T--){ sumx = sumy = sumarea = 0.0; scanf("%d", &N); scanf("%d %d %d %d", x, y, x+1, y+1); N -= 2; while(N--){ scanf("%d %d", x+2, y+2); //求新添加的三角形的面积 double s = ((x[1] - x[0]) * (y[2] - y[0]) - (x[2] - x[0]) * (y[1] - y[0])) / 2.0; //求∑x[i]*s[i]和∑y[i]*s[i] sumx += ((x[0] + x[1] + x[2]) * s); sumy += ((y[0] + y[1] + y[2]) * s); //求总面积 sumarea += s; x[1] = x[2]; y[1] = y[2]; } printf("%.2lf %.2lf\n", sumx / sumarea / 3, sumy / sumarea / 3); } return 0;}
参考博客:http://blog.csdn.net/cs_zlg/article/details/7870799
- ACM--多边形重心–-HDOJ 1115-- Lifting the Stone
- HDOJ 1115 Lifting the Stone 多边形重心
- hdoj-1115-Lifting the Stone 求多边形重心问题
- HDOJ 1115 Lifting the Stone (求多边形重心坐标)
- HDOJ 1115 Lifting the Stone (计算几何+多边形重心)
- hdu acm 1115 Lifting the Stone(多边形重心)
- HDU 1115 Lifting the Stone(多边形重心)
- 【多边形重心】hdu 1115 Lifting the Stone
- HDU 1115 Lifting the Stone(多边形重心)
- hdu 1115 Lifting the Stone【多边形重心】
- hdu 1115 Lifting the Stone【多边形重心】
- HDU 1115 Lifting the Stone (多边形重心)
- [ACM] hdu Lifting the Stone(多边形的重心)
- hdu 1115 Lifting the Stone 密度均匀多边形重心
- HDU 1115 Lifting the Stone(求多边形重心)
- hdu 1115 Lifting the Stone 多边形的重心
- HDU 1115 Lifting the Stone 多边形重心 模板体
- HDU 1115 Lifting the Stone(求多边形重心)
- Android消息机制详细解析
- HDU 1695(数论,筛选+素因子分解+容斥)
- 观察者模式
- 自己写的一个功能类似于strtok函数的函数
- 正态分布的前世今生
- ACM--多边形重心–-HDOJ 1115-- Lifting the Stone
- Java环境搭建,以win10为例
- Android 开发中的微技巧和小知识
- 读完这100篇论文 就能成大数据高手
- 训练3 习题15
- 高精度模板2.0
- modelsim10.2c安装教程
- 发几张18650锂电池的放电曲线
- 喜剧者联盟观后感