View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.geometry.euclidean.twod;
23  
24  import org.hipparchus.exception.MathIllegalArgumentException;
25  import org.hipparchus.geometry.euclidean.oned.Euclidean1D;
26  import org.hipparchus.geometry.euclidean.oned.OrientedPoint;
27  import org.hipparchus.geometry.euclidean.oned.SubOrientedPoint;
28  import org.hipparchus.geometry.euclidean.oned.Vector1D;
29  import org.hipparchus.geometry.partitioning.Transform;
30  import org.hipparchus.util.FastMath;
31  import org.hipparchus.util.MathUtils;
32  import org.junit.jupiter.api.Test;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  import static org.junit.jupiter.api.Assertions.assertFalse;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  class LineTest {
39  
40      @Test
41      void testContains() {
42          Line l = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
43          assertTrue(l.contains(new Vector2D(0, 1)));
44          assertTrue(l.contains(new Vector2D(1, 2)));
45          assertTrue(l.contains(new Vector2D(7, 8)));
46          assertFalse(l.contains(new Vector2D(8, 7)));
47      }
48  
49      @Test
50      void testAbscissa() {
51          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
52          assertEquals(0.0,
53                              (l.toSubSpace(new Vector2D(-3,  4))).getX(),
54                              1.0e-10);
55          assertEquals(0.0,
56                              (l.toSubSpace(new Vector2D( 3, -4))).getX(),
57                              1.0e-10);
58          assertEquals(-5.0,
59                              (l.toSubSpace(new Vector2D( 7, -1))).getX(),
60                              1.0e-10);
61          assertEquals( 5.0,
62                               (l.toSubSpace(new Vector2D(-1, -7))).getX(),
63                               1.0e-10);
64      }
65  
66      @Test
67      void testOffset() {
68          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
69          assertEquals(-5.0, l.getOffset(new Vector2D(5, -3)), 1.0e-10);
70          assertEquals(+5.0, l.getOffset(new Vector2D(-5, 2)), 1.0e-10);
71      }
72  
73      @Test
74      void testDistance() {
75          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
76          assertEquals(+5.0, l.distance(new Vector2D(5, -3)), 1.0e-10);
77          assertEquals(+5.0, l.distance(new Vector2D(-5, 2)), 1.0e-10);
78      }
79  
80      @Test
81      void testPointAt() {
82          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
83          for (double a = -2.0; a < 2.0; a += 0.2) {
84              Vector1D pA = new Vector1D(a);
85              Vector2D point = l.toSpace(pA);
86              assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10);
87              assertEquals(0.0, l.getOffset(point),   1.0e-10);
88              for (double o = -2.0; o < 2.0; o += 0.2) {
89                  point = l.getPointAt(pA, o);
90                  assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10);
91                  assertEquals(o, l.getOffset(point),   1.0e-10);
92              }
93          }
94      }
95  
96      @Test
97      void testOriginOffset() {
98          Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
99          assertEquals(FastMath.sqrt(0.5), l1.getOriginOffset(), 1.0e-10);
100         Line l2 = new Line(new Vector2D(1, 2), new Vector2D(0, 1), 1.0e-10);
101         assertEquals(-FastMath.sqrt(0.5), l2.getOriginOffset(), 1.0e-10);
102     }
103 
104     @Test
105     void testParallel() {
106         Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
107         Line l2 = new Line(new Vector2D(2, 2), new Vector2D(3, 3), 1.0e-10);
108         assertTrue(l1.isParallelTo(l2));
109         Line l3 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.5), 1.0e-10);
110         assertTrue(l1.isParallelTo(l3));
111         Line l4 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.51), 1.0e-10);
112         assertFalse(l1.isParallelTo(l4));
113     }
114 
115     @Test
116     void testTransform() throws MathIllegalArgumentException {
117 
118         Line l1 = new Line(new Vector2D(1.0 ,1.0), new Vector2D(4.0 ,1.0), 1.0e-10);
119         Transform<Euclidean2D, Vector2D, Line, SubLine, Euclidean1D, Vector1D, OrientedPoint, SubOrientedPoint> t1 =
120             Line.getTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5);
121         assertEquals(MathUtils.SEMI_PI, t1.apply(l1).getAngle(), 1.0e-10);
122 
123         Line l2 = new Line(new Vector2D(0.0, 0.0), new Vector2D(1.0, 1.0), 1.0e-10);
124         Transform<Euclidean2D, Vector2D, Line, SubLine, Euclidean1D, Vector1D, OrientedPoint, SubOrientedPoint> t2 =
125             Line.getTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5);
126         assertEquals(FastMath.atan2(1.0, -2.0), t2.apply(l2).getAngle(), 1.0e-10);
127 
128     }
129 
130     @Test
131     void testIntersection() {
132         Line    l1 = new Line(new Vector2D( 0, 1), new Vector2D(1, 2), 1.0e-10);
133         Line    l2 = new Line(new Vector2D(-1, 2), new Vector2D(2, 1), 1.0e-10);
134         Vector2D p  = l1.intersection(l2);
135         assertEquals(0.5, p.getX(), 1.0e-10);
136         assertEquals(1.5, p.getY(), 1.0e-10);
137     }
138 
139     @Test
140     public void testMove() {
141         Line    line = new Line(new Vector2D( 0, 1), new Vector2D(1, 2), 1.0e-10);
142         assertTrue(line.emptyHyperplane().isEmpty());
143         assertEquals( 0.0, line.getOffset(new Vector2D(-1, 0)), 1.0e-10);
144         assertEquals( 1.0, line.getOffset(line.moveToOffset(new Vector2D(-1, 0),  1.0)), 1.0e-10);
145         assertEquals(-2.0, line.getOffset(line.moveToOffset(new Vector2D(-1, 0), -2.0)), 1.0e-10);
146     }
147 
148     @Test
149     public void testTranslate() {
150         Line    line = new Line(new Vector2D( 0, 1), new Vector2D(1, 2), 1.0e-10);
151         assertEquals(FastMath.sqrt(0.5), line.getOffset(Vector2D.ZERO), 1.0e-10);
152         line.translateToPoint(Vector2D.ZERO);
153         assertEquals(0.0, line.getOffset(Vector2D.ZERO), 1.0e-10);
154     }
155 
156     @Test
157     public void testSetAngle() {
158         Vector2D p1 = new Vector2D(0, 1);
159         Vector2D p2 = new Vector2D(1, 2);
160         Line    line = new Line(p1, p2, 1.0e-10);
161         assertEquals(0.0, line.getOffset(p1), 1.0e-10);
162         assertEquals(0.0, line.getOffset(p2), 1.0e-10);
163         assertEquals(FastMath.sqrt(0.5), line.getOffset(Vector2D.ZERO), 1.0e-10);
164 
165         line.setAngle(0.0);
166         // changing the angle does not change the offset
167         assertEquals(FastMath.sqrt(0.5), line.getOffset(Vector2D.ZERO), 1.0e-10);
168 
169         // first point is not on the line anymore
170         assertEquals(FastMath.sqrt(0.5) - 1.0, line.getOffset(p1), 1.0e-10);
171 
172     }
173 
174     @Test
175     public void testSetOriginOffset() {
176         Vector2D p1 = new Vector2D(0, 1);
177         Vector2D p2 = new Vector2D(1, 2);
178         Line    line = new Line(p1, p2, 1.0e-10);
179         assertEquals(0.0, line.getOffset(p1), 1.0e-10);
180         assertEquals(0.0, line.getOffset(p2), 1.0e-10);
181         assertEquals(FastMath.sqrt(0.5), line.getOffset(Vector2D.ZERO), 1.0e-10);
182 
183         line.setOriginOffset(0.0);
184         // changing the origin offset does not change the angle
185         assertEquals(FastMath.PI / 4, line.getAngle(), 1.0e-10);
186         assertEquals(0, line.getOffset(Vector2D.ZERO), 1.0e-10);
187 
188         // first point is not on the line anymore
189         assertEquals(-FastMath.sqrt(0.5), line.getOffset(p1), 1.0e-10);
190 
191     }
192 
193 }