|
11 | 11 | */ |
12 | 12 | package org.locationtech.jts.geom; |
13 | 13 |
|
14 | | -import junit.framework.TestCase; |
| 14 | +import static org.junit.Assert.assertEquals; |
| 15 | +import static org.junit.Assert.assertTrue; |
| 16 | + |
15 | 17 | import junit.textui.TestRunner; |
| 18 | +import test.jts.GeometryTestCase; |
16 | 19 |
|
17 | 20 |
|
18 | 21 | /** |
19 | 22 | * Test LineSegment methods |
20 | 23 | */ |
21 | | -public class LineSegmentTest extends TestCase { |
| 24 | +public class LineSegmentTest extends GeometryTestCase { |
22 | 25 |
|
23 | 26 | public static void main(String args[]) { |
24 | 27 | TestRunner.run(LineSegmentTest.class); |
@@ -53,6 +56,60 @@ public void testProjectionFactor() |
53 | 56 | assertTrue(seg2.projectionFactor(new Coordinate(11, 0)) == 0.1); |
54 | 57 | } |
55 | 58 |
|
| 59 | + public void testProjectPoint() { |
| 60 | + //-- interior point |
| 61 | + checkProjectPoint("LINESTRING (4 0, 8 0)", "POINT (5 2)", 5, 0); |
| 62 | + //-- endpoint |
| 63 | + checkProjectPoint("LINESTRING (4 0, 8 0)", "POINT (8 2)", 8, 0); |
| 64 | + //-- beyond end |
| 65 | + checkProjectPoint("LINESTRING (4 0, 8 0)", "POINT (9 2)", 9, 0); |
| 66 | + //-- before end |
| 67 | + checkProjectPoint("LINESTRING (4 0, 8 0)", "POINT (3 2)", 3, 0); |
| 68 | + //-- collinear |
| 69 | + checkProjectPoint("LINESTRING (4 0, 8 0)", "POINT (2 0)", 2, 0); |
| 70 | + } |
| 71 | + |
| 72 | + private void checkProjectPoint(String wkt1, String wkt2, double x, double y) { |
| 73 | + LineSegment seg1 = readLineSegment(wkt1); |
| 74 | + Point pt = (Point) read(wkt2); |
| 75 | + Coordinate p = pt.getCoordinate(); |
| 76 | + Coordinate actual = seg1.project(p); |
| 77 | + |
| 78 | + checkEqualXY(new Coordinate(x, y), actual, 0.0001); |
| 79 | + } |
| 80 | + |
| 81 | + public void testProjectSegment() { |
| 82 | + //-- project onto interior segment |
| 83 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (1 2, 2 3)", "LINESTRING(1 0, 2 0)"); |
| 84 | + //-- project onto interior point |
| 85 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (1 2, 1 4)", "LINESTRING(1 0, 1 0)"); |
| 86 | + //-- projection includes endpoint |
| 87 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (0 2, 1 4)", "LINESTRING(0 0, 1 0)"); |
| 88 | + //- projection onto endpoint |
| 89 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (8 2, 8 4)", "LINESTRING(8 0, 8 0)"); |
| 90 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (0 2, 0 4)", "LINESTRING(0 0, 0 0)"); |
| 91 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (0 2, -1 4)", "LINESTRING(0 0, 0 0)"); |
| 92 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (9 1, 8 0)", "LINESTRING(8 0, 8 0)"); |
| 93 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (9 1, 8 1)", "LINESTRING(8 0, 8 0)"); |
| 94 | + //-- no projection |
| 95 | + checkProjectSegment("LINESTRING (0 0, 8 0)", "LINESTRING (9 1, 9 2)", null); |
| 96 | + } |
| 97 | + |
| 98 | + private void checkProjectSegment(String wkt1, String wkt2, String wktExpected) { |
| 99 | + LineSegment seg1 = readLineSegment(wkt1); |
| 100 | + LineSegment seg2 = readLineSegment(wkt2); |
| 101 | + LineSegment actual = seg1.project(seg2); |
| 102 | + |
| 103 | + LineSegment expected = wktExpected == null ? null : readLineSegment(wktExpected); |
| 104 | + checkEqual(expected, actual, 0.0001); |
| 105 | + } |
| 106 | + |
| 107 | + private LineSegment readLineSegment(String wkt) { |
| 108 | + Geometry g = read(wkt); |
| 109 | + LineString line = (LineString) g; |
| 110 | + return new LineSegment(line.getCoordinateN(0), line.getCoordinateN(1)); |
| 111 | + } |
| 112 | + |
56 | 113 | public void testLineIntersection() { |
57 | 114 | // simple case |
58 | 115 | checkLineIntersection( |
|
0 commit comments