electronicFence(1996).c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "electronicFence.h"
  2. //----------------param
  3. // 定义多边形的经纬度坐标集合
  4. double polygonLat[REC_COORDINATE_DEPTH] = {0};
  5. double polygonLng[REC_COORDINATE_DEPTH] = {0};
  6. /**
  7. * 是否有 横断<br/> 参数为四个点的坐标
  8. *
  9. * @param px1
  10. * @param py1
  11. * @param px2
  12. * @param py2
  13. * @param px3
  14. * @param py3
  15. * @param px4
  16. * @param py4
  17. * @return
  18. */
  19. static bool isIntersect(double px1, double py1, double px2, double py2,
  20. double px3, double py3, double px4, double py4) {
  21. bool flag = false;
  22. double d = (px2 - px1) * (py4 - py3) - (py2 - py1) * (px4 - px3);
  23. if (d != 0) {
  24. double r = ((py1 - py3) * (px4 - px3) - (px1 - px3) * (py4 - py3)) / d;
  25. double s = ((py1 - py3) * (px2 - px1) - (px1 - px3) * (py2 - py1)) / d;
  26. if ((r >= 0) && (r <= 1) && (s >= 0) && (s <= 1)) {
  27. flag = true;
  28. }
  29. }
  30. return flag;
  31. }
  32. static double Multiply(double px0, double py0, double px1, double py1,
  33. double px2, double py2) {
  34. return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0));
  35. }
  36. /**
  37. * 目标点是否在目标边上边上<br/>
  38. *
  39. * @param px0
  40. * 目标点的经度坐标
  41. * @param py0
  42. * 目标点的纬度坐标
  43. * @param px1
  44. * 目标线的起点(终点)经度坐标
  45. * @param py1
  46. * 目标线的起点(终点)纬度坐标
  47. * @param px2
  48. * 目标线的终点(起点)经度坐标
  49. * @param py2
  50. * 目标线的终点(起点)纬度坐标
  51. * @return
  52. */
  53. static bool isPointOnLine(double px0, double py0, double px1,
  54. double py1, double px2, double py2) {
  55. bool flag = false;
  56. double ESP = 1e-9;
  57. if ((fabs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP)
  58. && ((px0 - px1) * (px0 - px2) <= 0)
  59. && ((py0 - py1) * (py0 - py2) <= 0)) {
  60. flag = true;
  61. }
  62. return flag;
  63. }
  64. /**
  65. * 判断目标点是否在多边形内(由多个点组成)<br/>
  66. *
  67. * @param px
  68. * 目标点的经度坐标
  69. * @param py
  70. * 目标点的纬度坐标
  71. * @param polygonXA
  72. * 多边形的经度坐标集合
  73. * @param polygonYA
  74. * 多边形的纬度坐标集合
  75. * @param num
  76. * 多边形的坐标集个数
  77. * @return
  78. */
  79. bool isPointInPolygon(double px, double py,
  80. double polygonXA[], double polygonYA[], int num) {
  81. bool isInside = false;
  82. double ESP = 1e-9;
  83. int count = 0;
  84. double linePoint1x;
  85. double linePoint1y;
  86. double linePoint2x = 180;
  87. double linePoint2y;
  88. linePoint1x = px;
  89. linePoint1y = py;
  90. linePoint2y = py;
  91. for (int i = 0; i < num; i++) {
  92. double cx1 = polygonXA[i];
  93. double cy1 = polygonYA[i];
  94. double cx2 = polygonXA[i + 1];
  95. double cy2 = polygonYA[i + 1];
  96. if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {
  97. return true;
  98. }
  99. if (fabs(cy2 - cy1) < ESP) {
  100. continue;
  101. }
  102. if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x,
  103. linePoint2y)) {
  104. if (cy1 > cy2)
  105. count++;
  106. }
  107. else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y,
  108. linePoint2x, linePoint2y)) {
  109. if (cy2 > cy1)
  110. count++;
  111. }
  112. else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y,
  113. linePoint2x, linePoint2y)) {
  114. count++;
  115. }
  116. }
  117. if (count % 2 == 1) {
  118. isInside = true;
  119. }
  120. return isInside;
  121. }