Commit 77a6ee18 authored by Nico Mack's avatar Nico Mack

Added new shape related utility method

parent 6b0e82c7
......@@ -32,6 +32,7 @@ import java.awt.geom.PathIterator;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;
......@@ -620,6 +621,66 @@ public class ShapeUtils {
return point;
}
// ---------------------------------------------------------------------------
/**
* Inspired by http://alienryderflex.com/polygon_fill/
*
* @param shape
* @param cellSize
* @return
*/
// ---------------------------------------------------------------------------
public static int[][] generateGrid(Shape shape, int cellSize) {
int[][] grid;
Rectangle2D bounds;
List<Segment> segments = getSegments(shape);
segments = simplify(segments, 5);
bounds = shape.getBounds2D();
int rows = (int) Math.round(bounds.getHeight() / cellSize);
int columns = (int) Math.round(bounds.getWidth() / cellSize);
int increment = 1;
grid = new int[rows][columns];
List<Integer> nodes = new ArrayList<>();
int left = (int) bounds.getX();
int top = (int) bounds.getY();
int bottom = (int) (bounds.getHeight() - bounds.getY());
for (int y = top; y < bottom; y += increment) {
int row = (y - top) / cellSize;
for (Segment segment : segments) {
double x0 = segment.x0;
double y0 = segment.y0;
double x1 = segment.x1;
double y1 = segment.y1;
if (((y0 > y) && (y1 <= y)) || ((y1 > y) && (y0 <= y))) {
nodes.add((int) (x0 + (y - y0) / (y1 - y0) * (x1 - x0)));
}
}
Collections.sort(nodes);
for (int i = 1; i < nodes.size(); i++) {
int c0 = ((nodes.get(i - 1) - left) / cellSize);
int c1 = ((nodes.get(i) - left) / cellSize);
if ((c1 - c0) >= 1) {
for (int col = c0; col < c1; col++) {
grid[row][col] = 1;
}
increment = cellSize;
}
}
nodes.clear();
}
return grid;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
// * End of Class
......
......@@ -16,6 +16,8 @@
*/
package lu.list.itis.dkd.tui.utility;
import lu.list.itis.dkd.tui.widget.corona.ShapeFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
......@@ -35,13 +37,15 @@ public class ShapeUtilsTest {
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {}
public void setUp() throws Exception {
}
/**
* @throws java.lang.Exception
*/
@After
public void tearDown() throws Exception {}
public void tearDown() throws Exception {
}
@Test
public void testcubicBSpline() {
......@@ -51,6 +55,11 @@ public class ShapeUtilsTest {
controlSegments.add(new Point(100, 0, 0, ScreenCoordinates.class));
Path2D poly = ShapeUtils.quadraticBSpline(controlSegments, 4);
}
@Test
public void generateGridTest() {
ShapeUtils.generateGrid(ShapeFactory.buildCircle(100), 5);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment