当前位置:   article > 正文

判断一个多边形是顺时针还是逆时针的方法

qpolygon 判定多边形方向

1、关于如何判定多边形是顺时针还是逆时针对于凸多边形而言,只需对某一个点计算叉积 = ((xi - xi-1),(yi - yi-1)) x ((xi+1 - xi),(yi+1 - yi)) = (xi - xi-1) * (yi+1 - yi) - (yi - yi-1) * (xi+1 - xi)
如果上式的值为正,逆时针;为负则是顺时针。

而对于一般的简单多边形,则需对于多边形的每一个点计算上述值,如果正值比较多,是逆时针;负值较多则为顺时针。

2、还有一种说明是取多边形的极点值,多边形的方向和这个顶点与其相邻两边构成的方向相同。

需要注意的是在屏幕坐标中,Y是向下的,所以在屏幕坐标系中看到的顺时针既是在Y轴向上的直角坐标系中看到的逆时针方向。

1.凸包的时候,只要判断前三个点即可,计算叉积,判断方向

2.凹包情况就复杂了,可以从三个方面考虑

首先,可以去凸包上的特殊点,x最大最小的点,y最大最小的点,这些极值点肯定是在凸包上的,可以计算这些的叉积,其次,直接统计叉积正负的数量,正多负少,是逆时针,反之,顺时针。

一个简单的做法是,计算面积,用面积的正负判断方向。

【面积判断法】

  1. #include<cstdio>
  2. #include<string>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<cstring>
  7. #include<set>
  8. #include<queue>
  9. #include<algorithm>
  10. #include<vector>
  11. #include<map>
  12. #include<cctype>
  13. #include<stack>
  14. #include<sstream>
  15. #include<list>
  16. #include<assert.h>
  17. #include<bitset>
  18. #include<numeric>
  19. #define debug() puts("++++")
  20. #define gcd(a,b) __gcd(a,b)
  21. #define lson l,m,rt<<1
  22. #define rson m+1,r,rt<<1|1
  23. #define fi first
  24. #define se second
  25. #define pb push_back
  26. #define sqr(x) ((x)*(x))
  27. #define ms(a,b) memset(a,b,sizeof(a))
  28. #define sz size()
  29. #define be begin()
  30. #define pu push_up
  31. #define pd push_down
  32. #define cl clear()
  33. #define lowbit(x) -x&x
  34. #define all 1,n,1
  35. #define mod 2000000000000000003
  36. #define rep(i,n,x) for(int i=(x); i<(n); i++)
  37. #define in freopen("in.in","r",stdin)
  38. #define out freopen("out.out","w",stdout)
  39. using namespace std;
  40. typedef long long LL;
  41. typedef unsigned long long ULL;
  42. typedef pair<int,int> P;
  43. const int INF = 0x3f3f3f3f;
  44. const LL LNF = 1e18;
  45. const int MAXN = 1e3 + 5;
  46. const int maxm = 1e6 + 10;
  47. const double PI = acos(-1.0);
  48. const double eps = 1e-8;
  49. const int dx[] = {-1,1,0,0,1,1,-1,-1};
  50. const int dy[] = {0,0,1,-1,1,-1,1,-1};
  51. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  52. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  53. const int maxn = 200050;
  54. struct Point{
  55. double x, y;
  56. };
  57. double cross(Point a,Point b,Point c) {
  58. return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
  59. }
  60. //计算多边形面积
  61. double PolygonArea(Point p[], int n)
  62. {
  63. if(n < 3) return 0.0;
  64. double s = p[0].y * (p[n - 1].x - p[1].x);
  65. p[n] = p[0];
  66. for(int i = 1; i < n; ++ i)
  67. s += p[i].y * (p[i - 1].x - p[i + 1].x);
  68. return s * 0.5;
  69. }
  70. Point p1[maxn];
  71. int n1;
  72. int main()
  73. {
  74. while(scanf("%d",&n1) != EOF ){
  75. for(int i = 0; i < n1; i++)
  76. scanf("%lf%lf", &p1[i].x, &p1[i].y);
  77. if ( PolygonArea(p1,n1) > 0 )
  78. puts("counterclockwise");
  79. else
  80. puts("clockwise");
  81. }
  82. return 0;
  83. }

转载于:https://www.cnblogs.com/Roni-i/p/9058424.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/42541
推荐阅读
相关标签
  

闽ICP备14008679号