1 /*
2 * Copyright (c) 2002-2025 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.util.geometry;
16
17 import static org.junit.jupiter.api.Assertions.assertFalse;
18 import static org.junit.jupiter.api.Assertions.assertTrue;
19
20 import org.junit.jupiter.api.Test;
21
22 /**
23 * Tests for {@link Polygon2D}.
24 * Some tests cases are taken from
25 * https://github.com/sromku/polygon-contains-point/blob/master/src/test/java/Tests.java
26 *
27 * @author Ronald Brill
28 */
29 public class Polygon2DTest {
30
31 /**
32 * @throws Exception if the test fails
33 */
34 @Test
35 public void testSimplePolygon() {
36 final Polygon2D polygon =
37 new Polygon2D(1, 3)
38 .lineTo(2, 8)
39 .lineTo(5, 4)
40 .lineTo(5, 9)
41 .lineTo(7, 5)
42 .lineTo(6, 1)
43 .lineTo(3, 1);
44
45 assertTrue(polygon.contains(5.5, 7));
46 assertFalse(polygon.contains(4.5, 7));
47
48 assertTrue(polygon.contains(5, 9));
49 assertTrue(polygon.contains(2, 2));
50
51 assertFalse(polygon.contains(100, 200));
52 }
53
54 /**
55 * @throws Exception if the test fails
56 */
57 @Test
58 public void testPolygonFigure6() {
59 // example 1
60 Polygon2D polygon =
61 new Polygon2D(1, 3)
62 .lineTo(9, 3)
63 .lineTo(9, 7)
64 .lineTo(7, 5)
65 .lineTo(5, 7)
66 .lineTo(3, 5)
67 .lineTo(1, 7);
68
69 assertTrue(polygon.contains(5, 5));
70
71 // example 2
72 polygon = new Polygon2D(1, 3)
73 .lineTo(3, 5)
74 .lineTo(5, 3)
75 .lineTo(7, 5)
76 .lineTo(9, 3)
77 .lineTo(9, 7)
78 .lineTo(1, 7);
79
80 assertTrue(polygon.contains(5, 5));
81 }
82
83 /**
84 * Test issue: https://github.com/sromku/polygon-contains-point/issues/1.
85 */
86 @Test
87 public void testMapCoordinates1() {
88 final Polygon2D polygon =
89 new Polygon2D(42.499148, 27.485196)
90 .lineTo(42.498600, 27.480000)
91 .lineTo(42.503800, 27.474680)
92 .lineTo(42.510000, 27.468270)
93 .lineTo(42.510788, 27.466904)
94 .lineTo(42.512116, 27.465350)
95 .lineTo(42.512000, 27.467000)
96 .lineTo(42.513579, 27.471027)
97 .lineTo(42.512938, 27.472668)
98 .lineTo(42.511829, 27.474922)
99 .lineTo(42.507945, 27.480124)
100 .lineTo(42.509082, 27.482892)
101 .lineTo(42.536026, 27.490519)
102 .lineTo(42.534470, 27.499703)
103 .lineTo(42.499148, 27.485196);
104
105 assertTrue(polygon.contains(42.508956f, 27.483328f));
106 assertTrue(polygon.contains(42.505f, 27.48f));
107 }
108
109 /**
110 * Test issue: https://github.com/sromku/polygon-contains-point/issues/1.
111 */
112 @Test
113 public void testMapCoordinates2() {
114 final Polygon2D polygon =
115 new Polygon2D(40.481171, 6.4107070)
116 .lineTo(40.480248, 6.4101200)
117 .lineTo(40.480237, 6.4062790)
118 .lineTo(40.481161, 6.4062610);
119
120 assertTrue(polygon.contains(40.480890f, 6.4081030f));
121 }
122
123 /**
124 * Test issue: https://github.com/sromku/polygon-contains-point/issues/3.
125 */
126 @Test
127 public void testParallel() {
128 final Polygon2D polygon =
129 new Polygon2D(0, 0)
130 .lineTo(0, 1)
131 .lineTo(1, 2)
132 .lineTo(1, 99)
133 .lineTo(100, 0);
134
135 assertTrue(polygon.contains(3, 4));
136 assertTrue(polygon.contains(3, 4.1));
137 assertTrue(polygon.contains(3, 3.9));
138 }
139
140 /**
141 * @throws Exception if the test fails
142 */
143 @Test
144 public void testBorders() {
145 /*
146 * Unfortunately, this method won't work if the point is on the edge of the polygon.
147 * https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm
148 */
149 final Polygon2D polygon =
150 new Polygon2D(-1, -1)
151 .lineTo(-1, 1)
152 .lineTo(1, 1)
153 .lineTo(1, -1);
154
155 assertTrue(polygon.contains(0, 1));
156 assertTrue(polygon.contains(-1, 0));
157 }
158 }