任意多边形面积计算
绝世美人儿
559次浏览
2021年01月25日 05:45
最佳经验
本文由作者推荐
描写河流的句子-
任意多边形面积计算
根据坐标直接按公式计算。
-- -------------------------------------------------- -----------
这是一个问题早已经解决的经典问题
假定多边形n个顶点坐标依次是
(x1,y1), ... (xn,yn)
如果n个顶点是逆时针排列,面积就是:
|x1 y1|
|x2 y2 |
...
|xn
yn|
s = 0.5 * { |
| +|
|+
|
| }
|x2 y2|
|x3 y3 |
...
|x1
y1|
如果是顺时针就是上面这个公式的负数
把上面的公式整理得到的就是hnyyy的表达式,
注意他的算法对最后一个行列式的处理有问题
double dMj=0;//
面积
//n
是点数
x[n]=x[0];
y[n]=y[0];
for(int i=0; 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;
放心用
,
没有问题的
.
计算几何算法实现
-
任意多边形的面积
点击数:
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.
Fig. 1 The lines his sister had drawn
triangle, rectangle and circle. I'm sure I can find out how to calculate the area of this figure.
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.
Fig.2 The polygon with the coordinates of vertexes
Of
course,
he
was
not
satisfied
with
the
solution
of
such
an
easy
problem.
if
there's
a
random polygon on the paper, then how can I calculate the area?
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.
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).
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.
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
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
the vertexes is not enough to form a closed polygon, the output message should be
either.
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)
#include
#include
#include
#include