- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
|x1 y1| |x2 y2 | ... |xn yn|
s = 0.5 * { | | +| |+ | | }
|x2 y2| |x3 y3 | ... |x1 y1|
如果是顺时针就是上面这个公式的负数
把上面的公式整理得到的就是hnyyy的表达式,
注意他的算法对最后一个行列式的处理有问题
double dMj=0;//面积
v1.first =p2.first -p.first ;
v1.second=p2.second-p.second;
v2.first =p1.first -p.first;
v2.second=p1.second-p.second;
return v1.first*v2.second-v1.second*v2.first;
double det(point p0,point p1,point p2) //根据叉积计算三点组成的面积.
{
return (p1.first-p0.first)*(p2.second-p0.second)-(p1.second-p0.second)*(p2.first-p0.first);
}
double area(int n,point p[]) //计算所有点组成几何图形的面积
The input ends with an empty figure (n = 0). And this figure not be processed.
Output
As shown below, the output of each figure should contain the figure number and a colon followed by the area of the figure or the string "Impossible".
In the following n lines, each contain a pair of real numbers, which describes the coordinates of the vertexes, (xi, yi). The figure in each test case starts from the first vertex to the second one, then from the second to the third, …… and so on. At last, it closes from the nth vertex to the first one.
else cout<<"the area is:"<<s;
getch();
return 1;
}
Fig. 1 The lines his sister had drawn
"Great!" he thought, "The polygon seems so regular. I had just learned how to calculate the area of triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure." And so he did. First of all, he marked the vertexes in the polygon with their coordinates as Fig. 2 shows. And then he found the result--0.75 effortless.
#include <iostream>
#include <utility>
#include <vector>
#include <algorithm>
#include<cmath>
#include<conio.h>
using namespace std;
typedef pair<double ,double>point;
}
if(intersects(n,p)==0){cout<<"impossible! intersect!";getch();return 0;}
s=area(n,p);
if(s==0){cout<<"impossible!";getch();return 0;} //面积为一即所有点集合在一条直线上.
}
int intersects(int n,point p[]) //判断该点集是否存在交叉的线段,如果有则返回0否则返回1
{
int i=1;
for(;i<n-2;i++)
if(direction(p[0],p[i],p[i+1])*direction(p[0],p[i+1],p[i+2])<0)return 0;
return 1;源自文库
}
int main()
{
int i,n;
double s;
point p[10];
cout<<"enter the n:";
cin>>n;
for(i=0;i<n;i++)
{
cout<<endl<<"p["<<i<<"]=";
cin>>p[i].first>>p[i].second;
Print a blank line between each test cases.
Sample Input
5
0 0
0 1
0.5 0.5
1 1
1 0
4
0 0
0 1
1 0
1 1
0
Sample Output
Figure 1: 0.75
Figure 2: Impossible
settling methord:(c++ description)
Input
The input data consists of several figures. The first line of the input for each figure contains a single integer n, the number of vertexes in the figure. (0≤n≤1000).
If the figure is a polygon, compute its area (accurate to two fractional digits). According to the input vertexes, if they cannot form a polygon (that is, one line intersects with another which shouldn't be adjoined with it, for example, in a figure with four lines, the first line intersects with the third one), just display "Impossible", indicating the figure can't be a polygon. If the amount of the vertexes is not enough to form a closed polygon, the output message should be "Impossible" either.
Fig.2 The polygon with the coordinates of vertexes
Of course, he was not satisfied with the solution of such an easy problem. "Mmm, if there's a random polygon on the paper, then how can I calculate the area?" he asked himself. Till then, he hadn't found out the general rules on calculating the area of a random polygon. He clearly knew that the answer to this question is out of his competence. So he asked you, an erudite expert, to offer him help. The kind behavior would be highly appreciated by him.
任意多边形面积计算
根据坐标直接按公式计算。
---------------------------------------------------------------
这是一个问题早已经解决的经典问题
假定多边形n个顶点坐标依次是(x1,y1), ... (xn,yn)
如果n个顶点是逆时针排列,面积就是:
//n是点数
x[n]=x[0];
y[n]=y[0];
for(int i=0; i<n-1; i++)
{
dMj+=x[i]*y[i+1]-x[i+1]*y[i];
}
if (n>1)
dMj += x[n]*y[0]-x[0]*y[n]
dMj=fabs(dMj)*0.5;
放心用,没有问题的.
计算几何算法实现-任意多边形的面积
{
double s=0.00;
int i=1;
for(;i<n-1;i++)
s=s+det(p[0],p[i],p[i+1]);
return fabs(s)/2.00;
}
double direction(point p,point p1,point p2) //叉积计算函数
{
point v1,v2;
点击数:1082发布日期:2006-4-27 21:21:00
【收藏】【评论】【打印】【编程爱好者论坛】【关闭】
Tag:算法
//time: 4.27 night
description:
AREA
Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are really too easy to an expert. But as an amateur, especially as a 15-year-old boy, he had done very well. He is so rolling in thinking the mathematical problem that he is easily to try to solve every problem he met in a mathematical way. One day, he found a piece of paper on the desk. His younger sister, Mary, a four-year-old girl, had drawn some lines. But those lines formed a special kind of concave polygon by accident as Fig. 1 shows.