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.Point;
26  import org.hipparchus.geometry.euclidean.oned.Euclidean1D;
27  import org.hipparchus.geometry.euclidean.oned.Vector1D;
28  import org.hipparchus.geometry.partitioning.Transform;
29  import org.hipparchus.util.FastMath;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  public class LineTest {
34  
35      @Test
36      public void testContains() {
37          Line l = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
38          Assert.assertTrue(l.contains(new Vector2D(0, 1)));
39          Assert.assertTrue(l.contains(new Vector2D(1, 2)));
40          Assert.assertTrue(l.contains(new Vector2D(7, 8)));
41          Assert.assertTrue(! l.contains(new Vector2D(8, 7)));
42      }
43  
44      @Test
45      public void testAbscissa() {
46          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
47          Assert.assertEquals(0.0,
48                              (l.toSubSpace(new Vector2D(-3,  4))).getX(),
49                              1.0e-10);
50          Assert.assertEquals(0.0,
51                              (l.toSubSpace(new Vector2D( 3, -4))).getX(),
52                              1.0e-10);
53          Assert.assertEquals(-5.0,
54                              (l.toSubSpace(new Vector2D( 7, -1))).getX(),
55                              1.0e-10);
56          Assert.assertEquals( 5.0,
57                               (l.toSubSpace(new Vector2D(-1, -7))).getX(),
58                               1.0e-10);
59      }
60  
61      @Test
62      public void testOffset() {
63          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
64          Assert.assertEquals(-5.0, l.getOffset(new Vector2D(5, -3)), 1.0e-10);
65          Assert.assertEquals(+5.0, l.getOffset(new Vector2D(-5, 2)), 1.0e-10);
66      }
67  
68      @Test
69      public void testDistance() {
70          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
71          Assert.assertEquals(+5.0, l.distance(new Vector2D(5, -3)), 1.0e-10);
72          Assert.assertEquals(+5.0, l.distance(new Vector2D(-5, 2)), 1.0e-10);
73      }
74  
75      @Test
76      public void testPointAt() {
77          Line l = new Line(new Vector2D(2, 1), new Vector2D(-2, -2), 1.0e-10);
78          for (double a = -2.0; a < 2.0; a += 0.2) {
79              Point<Euclidean1D> pA = new Vector1D(a);
80              Point<Euclidean2D> point = l.toSpace(pA);
81              Assert.assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10);
82              Assert.assertEquals(0.0, l.getOffset(point),   1.0e-10);
83              for (double o = -2.0; o < 2.0; o += 0.2) {
84                  point = l.getPointAt((Vector1D) pA, o);
85                  Assert.assertEquals(a, (l.toSubSpace(point)).getX(), 1.0e-10);
86                  Assert.assertEquals(o, l.getOffset(point),   1.0e-10);
87              }
88          }
89      }
90  
91      @Test
92      public void testOriginOffset() {
93          Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
94          Assert.assertEquals(FastMath.sqrt(0.5), l1.getOriginOffset(), 1.0e-10);
95          Line l2 = new Line(new Vector2D(1, 2), new Vector2D(0, 1), 1.0e-10);
96          Assert.assertEquals(-FastMath.sqrt(0.5), l2.getOriginOffset(), 1.0e-10);
97      }
98  
99      @Test
100     public void testParallel() {
101         Line l1 = new Line(new Vector2D(0, 1), new Vector2D(1, 2), 1.0e-10);
102         Line l2 = new Line(new Vector2D(2, 2), new Vector2D(3, 3), 1.0e-10);
103         Assert.assertTrue(l1.isParallelTo(l2));
104         Line l3 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.5), 1.0e-10);
105         Assert.assertTrue(l1.isParallelTo(l3));
106         Line l4 = new Line(new Vector2D(1, 0), new Vector2D(0.5, -0.51), 1.0e-10);
107         Assert.assertTrue(! l1.isParallelTo(l4));
108     }
109 
110     @Test
111     public void testTransform() throws MathIllegalArgumentException {
112 
113         Line l1 = new Line(new Vector2D(1.0 ,1.0), new Vector2D(4.0 ,1.0), 1.0e-10);
114         Transform<Euclidean2D, Euclidean1D> t1 =
115             Line.getTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5);
116         Assert.assertEquals(0.5 * FastMath.PI,
117                             ((Line) t1.apply(l1)).getAngle(),
118                             1.0e-10);
119 
120         Line l2 = new Line(new Vector2D(0.0, 0.0), new Vector2D(1.0, 1.0), 1.0e-10);
121         Transform<Euclidean2D, Euclidean1D> t2 =
122             Line.getTransform(0.0, 0.5, -1.0, 0.0, 1.0, 1.5);
123         Assert.assertEquals(FastMath.atan2(1.0, -2.0),
124                             ((Line) t2.apply(l2)).getAngle(),
125                             1.0e-10);
126 
127     }
128 
129     @Test
130     public void testIntersection() {
131         Line    l1 = new Line(new Vector2D( 0, 1), new Vector2D(1, 2), 1.0e-10);
132         Line    l2 = new Line(new Vector2D(-1, 2), new Vector2D(2, 1), 1.0e-10);
133         Vector2D p  = l1.intersection(l2);
134         Assert.assertEquals(0.5, p.getX(), 1.0e-10);
135         Assert.assertEquals(1.5, p.getY(), 1.0e-10);
136     }
137 
138 }