Skip to content

Commit

Permalink
Add CoordinateList.toCoordinateArray() with direction (#482)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Davis <[email protected]>
  • Loading branch information
dr-jts authored Oct 1, 2019
1 parent 7064723 commit dd2c9a2
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,27 @@ public Coordinate[] toCoordinateArray()
return (Coordinate[]) toArray(coordArrayType);
}

/**
* Creates an array containing the coordinates in this list,
* oriented in the given direction (forward or reverse).
*
* @param direction the direction value: true for forward, false for reverse
* @return an oriented array of coordinates
*/
public Coordinate[] toCoordinateArray(boolean isForward)
{
if (isForward) {
return (Coordinate[]) toArray(coordArrayType);
}
// construct reversed array
int size = size();
Coordinate[] pts = new Coordinate[size];
for (int i = 0; i < size; i++) {
pts[i] = get(size - i - 1);
}
return pts;
}

/**
* Returns a deep copy of this <tt>CoordinateList</tt> instance.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.locationtech.jts.geom;

import junit.framework.TestCase;
import junit.textui.TestRunner;

public class CoordinateListTest extends TestCase {
public static void main(String args[]) {
TestRunner.run(CoordinateListTest.class);
}

public CoordinateListTest(String name) { super(name); }

public void testForward() {
checkValue(coordList(0,0,1,1,2,2).toCoordinateArray(true),
0,0,1,1,2,2);
}

public void testReverse() {
checkValue(coordList(0,0,1,1,2,2).toCoordinateArray(false),
2,2,1,1,0,0);
}

public void testReverseEmpty() {
checkValue(coordList().toCoordinateArray(false) );
}

private void checkValue(Coordinate[] coordArray, double ... ords) {

assertEquals( coordArray.length * 2, ords.length);

for (int i = 0 ; i < coordArray.length; i += 2) {
Coordinate pt = coordArray[i];
assertEquals(pt.getX(), ords[2 * i]);
assertEquals(pt.getY(), ords[2 * i + 1]);
}
}

private CoordinateList coordList(double ... ords) {
CoordinateList cl = new CoordinateList();
for (int i = 0 ; i < ords.length; i += 2) {
cl.add(new Coordinate(ords[i], ords[i+1]), false);
}
return cl;
}

}

0 comments on commit dd2c9a2

Please sign in to comment.