Skip to content

Commit aa5828f

Browse files
committed
geom2d/polygon/Polyline2DTest.java: add test for distance to point
1 parent d05294a commit aa5828f

File tree

1 file changed

+93
-46
lines changed

1 file changed

+93
-46
lines changed

src/test/java/math/geom2d/polygon/Polyline2DTest.java

Lines changed: 93 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,59 @@
4444
/**
4545
* @author Legland
4646
*/
47-
public class Polyline2DTest extends TestCase {
48-
47+
public class Polyline2DTest extends TestCase
48+
{
4949
/**
5050
* Constructor for Polyline2DTest.
5151
*
5252
* @param arg0
5353
*/
54-
public Polyline2DTest(String arg0) {
54+
public Polyline2DTest(String arg0)
55+
{
5556
super(arg0);
5657
}
5758

58-
public void testSimplify() {
59+
public void testSimplify()
60+
{
5961
CircleArc2D arc = new CircleArc2D(0, 0, 10, 0, Math.PI / 2);
6062
Polyline2D poly = arc.asPolyline(16);
6163
Polyline2D poly2 = poly.simplify(2.5);
6264

6365
assertEquals(3, poly2.vertexNumber());
6466
}
6567

66-
public void testGetLength() {
67-
Polyline2D polyline = new Polyline2D(new Point2D[] { new Point2D(0, 0),
68-
new Point2D(10, 0), new Point2D(10, 10), new Point2D(20, 10) });
69-
68+
public void testDistanceToPoint()
69+
{
70+
ArrayList<Point2D> listPoint= new ArrayList<>();
71+
listPoint.add(new Point2D(201093.16610711772, 1777477.3696508463));
72+
listPoint.add(new Point2D(202667.84818503872, 1777673.8963489647));
73+
Polyline2D polyline = Polyline2D.create(listPoint);
74+
Point2D currentPoint = new Point2D(201926.0433899812, 1777926.9596233366);
75+
double dist1 = polyline.distance(currentPoint);
76+
// System.out.println("distance1: " + dist1);
77+
78+
ArrayList<Point2D> listPoint2= new ArrayList<>();
79+
listPoint2.add(new Point2D(201.09316610711772, 1777.4773696508463));
80+
listPoint2.add(new Point2D(202.66784818503872, 1777.6738963489647));
81+
82+
Polyline2D polyline2 = Polyline2D.create(listPoint2);
83+
Point2D currentPoint2 = new Point2D(201.9260433899812, 1777.9269596233366);
84+
double dist2 = polyline2.distance(currentPoint2);
85+
// System.out.println("distance2: " + dist2*1000);
86+
87+
assertEquals(dist1, dist2 * 1000, 1.0);
88+
}
89+
90+
91+
public void testGetLength()
92+
{
93+
Polyline2D polyline = createDefaultPolyline();
7094
assertEquals(polyline.length(), 30, 1e-14);
7195
}
7296

73-
public void testGetLengthDouble() {
74-
Polyline2D polyline = new Polyline2D(new Point2D[] { new Point2D(0, 0),
75-
new Point2D(10, 0), new Point2D(10, 10), new Point2D(20, 10) });
97+
public void testGetLengthDouble()
98+
{
99+
Polyline2D polyline = createDefaultPolyline();
76100

77101
assertEquals(polyline.length(0), 0, 1e-14);
78102
assertEquals(polyline.length(.5), 5, 1e-14);
@@ -83,9 +107,9 @@ public void testGetLengthDouble() {
83107
assertEquals(polyline.length(3), 30, 1e-14);
84108
}
85109

86-
public void testGetPositionDouble() {
87-
Polyline2D polyline = new Polyline2D(new Point2D[] { new Point2D(0, 0),
88-
new Point2D(10, 0), new Point2D(10, 10), new Point2D(20, 10) });
110+
public void testGetPositionDouble()
111+
{
112+
Polyline2D polyline = createDefaultPolyline();
89113

90114
assertEquals(polyline.position(0), 0, 1e-14);
91115
assertEquals(polyline.position(5), .5, 1e-14);
@@ -96,7 +120,8 @@ public void testGetPositionDouble() {
96120
assertEquals(polyline.position(30), 3, 1e-14);
97121
}
98122

99-
public void testGetBufferDouble() {
123+
public void testGetBufferDouble()
124+
{
100125
Polyline2D polyline = new Polyline2D(new Point2D[] {
101126
new Point2D(50, 50), new Point2D(100, 50),
102127
new Point2D(100, 100), new Point2D(150, 100) });
@@ -107,7 +132,8 @@ public void testGetBufferDouble() {
107132
assertEquals(1, boundary.continuousCurves().size());
108133
}
109134

110-
public void testGetBufferDouble_MultipleVertex() {
135+
public void testGetBufferDouble_MultipleVertex()
136+
{
111137
Polyline2D polyline = new Polyline2D(new Point2D[] {
112138
new Point2D(50, 50), new Point2D(100, 50),
113139
new Point2D(100, 50), new Point2D(100, 100),
@@ -143,7 +169,8 @@ public void testGetBufferDouble_SmallAnglePolyline() {
143169
assertEquals(1, boundary.continuousCurves().size());
144170
}
145171

146-
public void testGetParallelDouble() {
172+
public void testGetParallelDouble()
173+
{
147174
Polyline2D polyline = new Polyline2D(new Point2D[] {
148175
new Point2D(50, 50), new Point2D(100, 50),
149176
new Point2D(100, 100), new Point2D(150, 100) });
@@ -152,23 +179,26 @@ public void testGetParallelDouble() {
152179
assertTrue(parallel != null);
153180
}
154181

155-
public void testGetLeftTangent() {
182+
public void testGetLeftTangent()
183+
{
156184
Polyline2D line = new Polyline2D(new Point2D[] { new Point2D(10, 10),
157185
new Point2D(20, 10), new Point2D(20, 20) });
158186

159187
assertTrue(line.leftTangent(1).equals(new Vector2D(10, 0)));
160188
assertTrue(line.leftTangent(2).equals(new Vector2D(0, 10)));
161189
}
162190

163-
public void testGetRightTangent() {
191+
public void testGetRightTangent()
192+
{
164193
Polyline2D line = new Polyline2D(new Point2D[] { new Point2D(10, 10),
165194
new Point2D(20, 10), new Point2D(20, 20) });
166195

167196
assertTrue(line.rightTangent(0).equals(new Vector2D(10, 0)));
168197
assertTrue(line.rightTangent(1).equals(new Vector2D(0, 10)));
169198
}
170199

171-
public void testAddVertex() {
200+
public void testAddVertex()
201+
{
172202
Point2D[] points = new Point2D[4];
173203
points[0] = new Point2D(0, 0);
174204
points[1] = new Point2D(10, 0);
@@ -179,7 +209,8 @@ public void testAddVertex() {
179209
assertEquals(line.vertexNumber(), points.length + 1);
180210
}
181211

182-
public void testRemoveVertex_Point2D() {
212+
public void testRemoveVertex_Point2D()
213+
{
183214
Point2D[] points = new Point2D[4];
184215
points[0] = new Point2D(0, 0);
185216
points[1] = new Point2D(10, 0);
@@ -190,7 +221,8 @@ public void testRemoveVertex_Point2D() {
190221
assertEquals(3, line.vertexNumber());
191222
}
192223

193-
public void testRemoveVertex_int() {
224+
public void testRemoveVertex_int()
225+
{
194226
Point2D[] points = new Point2D[4];
195227
points[0] = new Point2D(0, 0);
196228
points[1] = new Point2D(10, 0);
@@ -201,7 +233,8 @@ public void testRemoveVertex_int() {
201233
assertEquals(3, line.vertexNumber());
202234
}
203235

204-
public void testInsertVertex() {
236+
public void testInsertVertex()
237+
{
205238
Point2D[] points = new Point2D[4];
206239
points[0] = new Point2D(0, 0);
207240
points[1] = new Point2D(10, 0);
@@ -213,7 +246,8 @@ public void testInsertVertex() {
213246
assertEquals(line.vertex(2), new Point2D(15, 20));
214247
}
215248

216-
public void testGetPoint() {
249+
public void testGetPoint()
250+
{
217251
// initialize points
218252
Point2D[] points = new Point2D[4];
219253
points[0] = new Point2D(0, 0);
@@ -243,7 +277,8 @@ public void testGetPoint() {
243277
assertTrue(p30.equals(line1.point(3.5)));
244278
}
245279

246-
public void testGetPosition() {
280+
public void testGetPosition()
281+
{
247282
Polyline2D line = new Polyline2D(new Point2D[] { new Point2D(0, 0),
248283
new Point2D(10, 0), new Point2D(10, 10), new Point2D(20, 20) });
249284
double eps = 1e-14;
@@ -261,7 +296,8 @@ public void testGetPosition() {
261296

262297
}
263298

264-
public void testgetIntersections() {
299+
public void testgetIntersections()
300+
{
265301
// initialize points
266302
Point2D[] points = new Point2D[3];
267303
points[0] = new Point2D(0, -5);
@@ -288,27 +324,23 @@ public void testgetIntersections() {
288324
assertTrue(iter2.next().equals(new Point2D(15, 0)));
289325
}
290326

291-
public void testGetSubCurve() {
292-
// initialize points
293-
Point2D[] points = new Point2D[4];
294-
points[0] = new Point2D(0, 0);
295-
points[1] = new Point2D(10, 0);
296-
points[2] = new Point2D(10, 10);
297-
points[3] = new Point2D(20, 20);
327+
public void testGetSubCurve()
328+
{
329+
// create polyline
330+
Polyline2D line1 = createDefaultPolyline();
298331

299-
// create polyline, and subcurve
300-
Polyline2D line1 = new Polyline2D(points);
301-
Polyline2D sub = line1.subCurve(.5, 2.5);
332+
Polyline2D sub = line1.subCurve(.5, 2.5);
302333

303-
// to check result
304-
Polyline2D line2 = new Polyline2D(new Point2D[] { new Point2D(5, 0),
305-
new Point2D(10, 0), new Point2D(10, 10), new Point2D(15, 15) });
334+
// to check result
335+
Polyline2D line2 = new Polyline2D(new Point2D[] { new Point2D(15, 10),
336+
new Point2D(20, 10), new Point2D(20, 20), new Point2D(15, 20) });
306337

307-
// check two objects are the same
308-
assertTrue(line2.equals(sub));
309-
}
338+
// check two objects are the same
339+
assertTrue(line2.equals(sub));
340+
}
310341

311-
public void testIsInside() {
342+
public void testIsInside()
343+
{
312344
Polyline2D polyline = new Polyline2D(new Point2D[] {
313345
new Point2D(150, 50), new Point2D(150, 150),
314346
new Point2D(100, 100), new Point2D(50, 150),
@@ -325,7 +357,8 @@ public void testIsInside() {
325357
assertTrue(!polyline.isInside(new Point2D(0, 50)));
326358
}
327359

328-
public void testCreate_Collection() {
360+
public void testCreate_Collection()
361+
{
329362
ArrayList<Point2D> array = new ArrayList<Point2D>(4);
330363
array.add(new Point2D(10, 10));
331364
array.add(new Point2D(20, 10));
@@ -335,7 +368,8 @@ public void testCreate_Collection() {
335368
assertNotNull(ring);
336369
}
337370

338-
public void testCreate_Array() {
371+
public void testCreate_Array()
372+
{
339373
Point2D[] array = new Point2D[4];
340374
array[0] = new Point2D(10, 10);
341375
array[1] = new Point2D(20, 10);
@@ -345,7 +379,8 @@ public void testCreate_Array() {
345379
assertNotNull(ring);
346380
}
347381

348-
public void testCopyConstructor() {
382+
public void testCopyConstructor()
383+
{
349384
Polyline2D polyline = new Polyline2D(new Point2D[] {
350385
new Point2D(150, 50), new Point2D(150, 150),
351386
new Point2D(100, 100), new Point2D(50, 150),
@@ -354,4 +389,16 @@ public void testCopyConstructor() {
354389
Polyline2D copy = new Polyline2D(polyline);
355390
assertTrue(polyline.equals(copy));
356391
}
392+
393+
/**
394+
* @return an open polyline composed of three line segment of length 10.
395+
*/
396+
private static final Polyline2D createDefaultPolyline()
397+
{
398+
return new Polyline2D(new Point2D[] {
399+
new Point2D(10, 10),
400+
new Point2D(20, 10),
401+
new Point2D(20, 20),
402+
new Point2D(10, 20) });
403+
}
357404
}

0 commit comments

Comments
 (0)