判断点是否在任意多边形内(java)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
判断点是否在任意多边形内(java)
1.import java.util.ArrayList;
2.
3.public class Test {
4.
5. public static void main(String[] args) {
6. double px = 113.0253;
7. double py = 23.98049;
8. ArrayList
9. ArrayList
10. polygonXA.add(113.0253);
11. polygonXA.add(113.4121);
12. polygonXA.add(113.37109);
13. polygonXA.add(113.02148);
14. // 113.18359,23.8496
15.
16. // 113.0253,23.98049 113.4121,23.9687 113.37109,2.73828
17.
18. // 113.02148,23.7539C
19.
20. polygonYA.add(23.98049);
21. polygonYA.add(23.9687);
22. polygonYA.add(23.73828);
23. polygonYA.add(23.7539);
24. Test test = new Test();
25. System.out.println(test.isPointInPolygon(px, py, polygonXA, polygonYA));
26. }
27.
28. public boolean isPointInPolygon(double px, double py,
29. ArrayList
30. boolean isInside = false;
31. double ESP = 1e-9;
32. int count = 0;
33. double linePoint1x;
34. double linePoint1y;
35. double linePoint2x = 180;
36. double linePoint2y;
37.
38. linePoint1x = px;
39. linePoint1y = py;
40. linePoint2y = py;
41.
42. for (int i = 0; i < polygonXA.size() - 1; i++) {
43. double cx1 = polygonXA.get(i);
44. double cy1 = polygonYA.get(i);
45. double cx2 = polygonXA.get(i + 1);
46. double cy2 = polygonYA.get(i + 1);
47. if (isPointOnLine(px, py, cx1, cy1, cx2, cy2)) {
48. return true;
49. }
50. if (Math.abs(cy2 - cy1) < ESP) {
51. continue;
52. }
53.
54. if (isPointOnLine(cx1, cy1, linePoint1x, linePoint1y, linePoint2x,
55. linePoint2y)) {
56. if (cy1 > cy2)
57. count++;
58. } else if (isPointOnLine(cx2, cy2, linePoint1x, linePoint1y,
59. linePoint2x, linePoint2y)) {
60. if (cy2 > cy1)
61. count++;
62. } else if (isIntersect(cx1, cy1, cx2, cy2, linePoint1x,
63. linePoint1y, linePoint2x, linePoint2y)) {
64. count++;
65. }
66. }
67. if (count % 2 == 1) {
68. isInside = true;
69. }
70.
71. return isInside;
72. }
73.
74. public double Multiply(double px0, double py0, double px1, double py1,
75. double px2, double py2) {
76. return ((px1 - px0) * (py2 - py0) - (px2 - px0) * (py1 - py0));
77. }
78.
79. public boolean isPointOnLine(double px0, double py0, double px1,
80. double py1, double px2, double py2) {
81. boolean flag = false;
82. double ESP = 1e-9;
83. if ((Math.abs(Multiply(px0, py0, px1, py1, px2, py2)) < ESP)
84. && ((px0 - px1) * (px0 - px2) <= 0)
85. && ((py0 - py1) * (py0 - py2) <= 0)) {
86. flag = true;
87. }