永夜初晗凝碧天

本博客现已全部转移到新地址,欲获取更多精彩文章,请访问http://acshiryu.github.io/

导航

hdu2108和hdu2036 叉乘的运用

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=2108

http://acm.hdu.edu.cn/showproblem.php?pid=2036

这两道题目分别是判断一个多边形是否是凸多边形,求一个多边形的面积

题意很简单,题目也不难,算是最基础的计算几何了

参考资料:计算几何基础

参考代码:

hdu2108 by ACShiryu

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 struct ac {
9 int x ;
10 int y ;
11 }po[555555];
12 int cross(ac a , ac b ,ac c)
13 {
14 return (b.x-a.x)*(c.y-b.y)-(b.y-a.y)*(c.x-b.x);
15 }
16 bool convex(int n)
17 {
18 for(int i = 0 ; i < n ; i ++)
19 if(cross(po[i],po[(i+1)%n],po[(i+2)%n])<0)
20 return false;
21 return true;
22 }
23 int main()
24 {
25 int n ;
26 while (cin >> n , n)
27 {
28 for (int i=0;i < n ; i ++ )
29 cin >> po[i].x >> po[i].y;
30 if (convex(n))
31 cout<<"convex"<<endl;
32 else
33 cout<<"concave"<<endl;
34 }
35 return 0;
36 }

  hdu2036 by ACShiryu

 1 #include<iostream>
2 #include<cstdlib>
3 #include<cstdio>
4 #include<cstring>
5 #include<algorithm>
6 #include<cmath>
7 using namespace std;
8 struct ac {
9 int x ;
10 int y ;
11 }po[100];
12 int cross (ac a , ac b)
13 {
14 return a.x*b.y-a.y*b.x;
15 }
16 int main()
17 {
18 int n ;
19 while ( scanf("%d",&n) , n)
20 {
21 int i ;
22 for ( i = 0 ; i < n ; i ++ )
23 {
24 scanf("%d%d",&po[i].x,&po[i].y);
25 }
26 double sum = 0 ;
27 for ( i = 0 ; i < n ; i ++ )
28 sum += cross(po[i],po[(i+1)%n])/2.0;
29 printf("%.1lf\n",sum);
30 }
31 return 0;
32 }

posted on 2011-08-28 11:18  ACShiryu  阅读(870)  评论(0编辑  收藏  举报