HDU2108 Shape of HDU 计算几何 C语言

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

题目大意:给出点数n, 逆时针顺序给出n个点的坐标,都为整数,判断所围多边形是凸的还是凹的。

思路:按照逆时针顺序连出向量p0p1,p1p2,p3p4……若是凸多边形,那么b相对于a一定是向逆时针方向旋转的,判断两向量的旋转方向,可以使用向量的叉积 a×b = x1×y2 - x2×y1。

自己想了好久不会,看了别人的题解,发现思路还是很清晰的……

提交情况:AC 1次

 

AC code :

View Code
 1 #include <stdio.h>
2
3 #include <stdlib.h>
4
5 #include <string.h>
6
7
8
9 #define MAXN 500
10
11
12
13 struct Point{
14
15 int x;
16
17 int y;
18
19 }point[MAXN], a, b;
20
21
22
23 int Edge() {
24
25 return a.x * b.y - a.y * b.x; //向量叉积
26
27 }
28
29
30
31 int Fuction(int n) {
32
33 int i, j;
34
35 //int flag = 0;
36
37 for(i = 0; i < n; i++) {
38
39 a.x = point[(i + 1) % n].x - point[i % n].x;
40
41 a.y = point[(i + 1) % n].y - point[i % n].y;
42
43 b.x = point[(i + 2) % n].x - point[(i + 1) % n].x;
44
45 b.y = point[(i + 2) % n].y - point[(i + 1) % n].y;
46
47 if(Edge() < 0) //凹多边形
48
49 return 1;
50
51 }
52
53 return 0;
54
55 }
56
57
58
59 int main() {
60
61 int n;
62
63 int i, j;
64
65 while(scanf("%d", &n), n) {
66
67 //int flag = 0;
68
69 for(i = 0; i < n; i++)
70
71 scanf("%d%d", &point[i].x, &point[i].y);
72
73 if(Fuction(n))
74
75 printf("concave\n");
76
77 else
78
79 printf("convex\n");
80
81 }
82
83 return 0;
84
85 }

posted @ 2011-07-20 16:32  cloehui  阅读(311)  评论(0编辑  收藏  举报