EOJ 1127. 多边形面积(计算几何)
题目链接:1127. 多边形面积(计算几何)
题意
按逆时针顺序给出 \(n\) 个点的坐标,求这些点围成的多边形的面积。
思路
选择多边形上的一个点,然后每次枚举之后的两个点,计算叉积,注意要保留符号,对所有的叉积的结果相加就是多边形的面积。
举个栗子:

计算上图多边形 \(ABCDEFGH\) 的面积,选择 \(A\) 点,则面积等于 \(\frac{1}{2} (\boldsymbol {AB \times AC} + \boldsymbol {AC \times AD} + \boldsymbol {AD \times AE} + \boldsymbol {AE \times AF} + \boldsymbol {AF \times AG} + \boldsymbol {AG \times AH})\)。其中 \(\triangle ABC\) 的面积是负的,而 \(\triangle ACD\) 与 \(\triangle ADE\) 的面积都是正的,则多边形 \(ABCDE\) 的面积相当于多边形 \(ACDE\) 的面积减去 \(\triangle ABC\) 的面积。
代码
#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 100 + 10;
inline int dcmp(db x) {
if(fabs(x) < eps) return 0;
return x > 0? 1: -1;
}
class Point {
public:
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y) {}
void input() {
scanf("%lf%lf", &x, &y);
}
bool operator<(const Point &a) const {
return (!dcmp(x - a.x))? dcmp(y - a.y) < 0: x < a.x;
}
bool operator==(const Point &a) const {
return dcmp(x - a.x) == 0 && dcmp(y - a.y) == 0;
}
db dis2(const Point a) {
return pow(x - a.x, 2) + pow(y - a.y, 2);
}
db dis(const Point a) {
return sqrt(dis2(a));
}
db dis2() {
return x * x + y * y;
}
db dis() {
return sqrt(dis2());
}
Point operator+(const Point a) {
return Point(x + a.x, y + a.y);
}
Point operator-(const Point a) {
return Point(x - a.x, y - a.y);
}
Point operator*(double p) {
return Point(x * p, y * p);
}
Point operator/(double p) {
return Point(x / p, y / p);
}
db dot(const Point a) {
return x * a.x + y * a.y;
}
db cross(const Point a) {
return x * a.y - y * a.x;
}
};
Point p[maxn];
int n;
db area() {
if(n < 3) return 0.0;
db ans = 0.0;
for(int i = 2; i < n; ++i) {
ans += (p[i] - p[1]).cross(p[i + 1] - p[1]);
}
return ans * 0.5;
}
int main() {
while(~scanf("%d", &n) && n) {
db s = 0;
for(int i = 1; i <= n; ++i) {
p[i].input();
}
s = area();
printf("%.1lf\n", fabs(s));
}
return 0;
}
EOJ 1127. 多边形面积(计算几何)的更多相关文章
- poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207
Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7038 Accepted: 3242 Description ...
- EOJ 1058. 挤模具 (多边形面积)
题目链接:1058. 挤模具 题意 给出模具的底和体积,求模具的高. 思路 模具的底为多边形,因此求出多边形面积,用体积除以底的面积就是答案. 多边形的面积求解见 EOJ 1127. 多边形面积(计算 ...
- [知识点]计算几何I——基础知识与多边形面积
// 此博文为迁移而来,写于2015年4月9日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102vxaq.html 1.前言 ...
- hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)
Area Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- poj 1654 Area(计算几何--叉积求多边形面积)
一个简单的用叉积求任意多边形面积的题,并不难,但我却错了很多次,double的数据应该是要转化为long long,我转成了int...这里为了节省内存尽量不开数组,直接计算,我MLE了一发...,最 ...
- poj 1265 Area【计算几何:叉积计算多边形面积+pick定理计算多边形内点数+计算多边形边上点数】
题目:http://poj.org/problem?id=1265 Sample Input 2 4 1 0 0 1 -1 0 0 -1 7 5 0 1 3 -2 2 -1 0 0 -3 -3 1 0 ...
- HDU 2036 改革春风吹满地【计算几何/叉乘求多边形面积】
改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 多边形面积(Area_Of_Polygons)
原理: 任意多边形的面积可由任意一点与多边形上依次两点连线构成的三角形矢量面积求和得出. 分析: 由于给出的点是相对于我们的坐标原点的坐标,每个点实际上我们可以当作一个顶点相对于原点的向量,如下图所示 ...
- 简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping
题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角 ...
随机推荐
- python开发必备pycharm专业版破解方法
修改hosts文件 添加下面一行到hosts文件,目的是屏蔽掉Pycharm对激活码的验证 0.0.0.0 account.jetbrains.com 注:hosts文件路径,Windows在C:\W ...
- spring data jpa 多对多查询
package com.ytkj.dao; import com.ytkj.entity.Customer; import com.ytkj.entity.Role; import org.sprin ...
- loadrunner 快捷键
lr 不同的界面切换 crt f6lr:选中要查找的内容 ctrl +f3 超找文本 选中所有字母大写 ctl +shift+ u;小写
- spring注解开发:bean的作用域与懒加载
1.bean的作用域 1.新建一个maven工程,添加如下依赖 <dependency> <groupId>org.springframework</groupId> ...
- 39.Binary Tree Inorder Traversal(二叉树中序遍历)
Level: Medium 题目描述: Given a binary tree, return the inorder traversal of its nodes' values. 思路分析: ...
- python2和python3编码
python2编码 unicode:unicode 你好 u'\u4f60\u597d' | | | | encode('utf8')| |decode('utf8') encode('gbk')| ...
- redux 介绍及配合 react开发
前言 本文是 Redux 及 Redux 配合 React 开发的教程,主要翻译自 Leveling Up with React: Redux,并参考了 Redux 的文档及一些博文,相对译文原文内容 ...
- sql查询静态数据
select * from ( ,,,'高中')) AS Education ( EducationId,EducationName )
- (playbook)ubuntu下ansible安装docker
--- - hosts: test sudo: yes vars: apt_packages_ca: - apt-transport-https - ca-certificates tasks: - ...
- 力扣—Remove Duplicates from Sorted List(删除排序链表中的重复元素)python实现
题目描述: 中文: 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次. 示例 1: 输入: 1->1->2输出: 1->2 示例 2: 输入: 1->1->2 ...