View Javadoc
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.Assert.assertEquals;
18  import static org.junit.Assert.assertFalse;
19  import static org.junit.Assert.assertTrue;
20  
21  import org.junit.Test;
22  
23  /**
24   * Tests for {@link Line2D}.
25   *
26   * @author Ronald Brill
27   */
28  public class Line2DTest {
29  
30      /**
31       * @throws Exception if the test fails
32       */
33      @Test
34      public void contains() throws Exception {
35          // y = 4x + 3
36          assertTrue(new Line2D(-0.75, 0, 0, 3).contains(-0.75, 0));
37          assertTrue(new Line2D(-0.75, 0, 0, 3).contains(0, 3));
38  
39          assertTrue(new Line2D(-0.75, 0, 0, 3).contains(-0.5, 1));
40          assertTrue(new Line2D(-0.75, 0, 0, 3).contains(-0.2469858418795, 2.0120566324819));
41  
42          assertFalse(new Line2D(-0.75, 0, 0, 3).contains(-0.246, 2.0120566324819));
43          assertFalse(new Line2D(-0.75, 0, 0, 3).contains(-1, -1));
44          assertFalse(new Line2D(-0.75, 0, 0, 3).contains(1, 7));
45      }
46  
47      /**
48       * @throws Exception if the test fails
49       */
50      @Test
51      public void containsVertical() throws Exception {
52          assertTrue(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(4, 7));
53          assertTrue(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(4, 13));
54          assertTrue(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(4, 11));
55  
56          assertFalse(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(4, 13.1));
57          assertFalse(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(4, 6.9));
58          assertFalse(new Line2D(new Point2D(4, 7), new Point2D(4, 13)).contains(-1, 11));
59      }
60  
61      /**
62       * @throws Exception if the test fails
63       */
64      @Test
65      public void intersect() throws Exception {
66          Line2D testLine = new Line2D(new Point2D(4, 7), new Point2D(7, 13));
67  
68          Line2D testLine2 = new Line2D(new Point2D(5, 7), new Point2D(2, 10));
69          Point2D intersectionPoint = testLine.intersect(testLine2);
70          assertEquals(4.333333, intersectionPoint.getX(), 0.00001);
71          assertEquals(7.666666, intersectionPoint.getY(), 0.00001);
72  
73          testLine2 = new Line2D(new Point2D(0, 6), new Point2D(10, 8));
74          intersectionPoint = testLine.intersect(testLine2);
75          assertEquals(3.888888, intersectionPoint.getX(), 0.00001);
76          assertEquals(6.777777, intersectionPoint.getY(), 0.00001);
77  
78          testLine = new Line2D(new Point2D(1, 3), new Point2D(1, 7));
79          testLine2 = new Line2D(new Point2D(0.9, 3), new Point2D(5, 5));
80          intersectionPoint = testLine.intersect(testLine2);
81          assertEquals(1, intersectionPoint.getX(), 0.00001);
82          assertEquals(3.0487804, intersectionPoint.getY(), 0.00001);
83      }
84  
85      /**
86       * @throws Exception if the test fails
87       */
88      @Test
89      public void intersectParallel() throws Exception {
90          final Line2D testLine = new Line2D(new Point2D(4, 7), new Point2D(4, 13));
91  
92          final Line2D testLine2 = new Line2D(new Point2D(5, 7), new Point2D(5, 11));
93          final Point2D intersectionPoint = testLine.intersect(testLine2);
94          assertEquals(null, intersectionPoint);
95      }
96  }