use of org.eclipse.elk.core.math.ElkRectangle in project elk by eclipse.
the class RectlinearConvexHullTest method visualize.
private static void visualize(final List<Point> points, final RectilinearConvexHull rch) {
KVector offset = new KVector(10, 10);
double scale = 10;
@SuppressWarnings("serial") JPanel p = new JPanel() {
public void paint(java.awt.Graphics g) {
super.paint(g);
g.setColor(Color.DARK_GRAY);
g.fillPolygon(toPolygon(rch, offset, scale));
g.setColor(Color.GRAY);
for (Point p : points) {
g.fillRect((int) (p.x * scale + offset.x) - 1, (int) (p.y * scale + offset.y) - 1, 3, 3);
}
g.setColor(Color.PINK);
for (ElkRectangle r : rch.splitIntoRectangles()) {
g.drawRect((int) (r.x * scale + offset.x), (int) ((r.y) * scale + offset.y), (int) (r.width * scale), (int) (r.height * scale));
}
g.setColor(Color.GRAY);
}
};
JFrame f = new JFrame();
f.setSize(1500, 1500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
f.getContentPane().add(p);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
use of org.eclipse.elk.core.math.ElkRectangle in project elk by eclipse.
the class RectlinearConvexHullTest method main.
/* ----------------
* Simple JFrame that draws the hull and rects.
*/
public static void main(String[] args) {
List<Point> points = Lists.newArrayList();
points.addAll(Arrays.asList(new Point(1d, 6d), new Point(3d, 5d), new Point(5d, 2d), new Point(7d, 2d), new Point(9d, 4d), new Point(11d, 4d), new Point(11d, 8d), new Point(8d, 8d), new Point(8d, 12d), new Point(6d, 12d), new Point(6d, 10d), new Point(4d, 6d)));
Random r = new Random();
for (int i = 0; i < 100; ++i) {
points.add(new Point((int) (r.nextDouble() * 100), (int) (r.nextDouble() * 100)));
}
RectilinearConvexHull rch = RectilinearConvexHull.of(points);
System.out.println("HULL: " + rch.getHull());
List<ElkRectangle> rects = rch.splitIntoRectangles();
System.out.println("Rectangles: " + rects);
visualize(points, rch);
}
use of org.eclipse.elk.core.math.ElkRectangle in project elk by eclipse.
the class CompactionTest method testLeftCompaction.
@Test
public void testLeftCompaction() {
CGraph graph = new CGraph(EnumSet.allOf(Direction.class));
CTestNode left = new CTestNode(new ElkRectangle(0, 0, 20, 20));
graph.cNodes.add(left);
CTestNode right = new CTestNode(new ElkRectangle(30, 0, 20, 20));
graph.cNodes.add(right);
compacter(graph).changeDirection(Direction.LEFT).compact().finish();
assertEquals(0, left.hitbox.x, EPSILON);
assertEquals(25, right.hitbox.x, EPSILON);
}
use of org.eclipse.elk.core.math.ElkRectangle in project elk by eclipse.
the class CompactionTest method testUpGroupCompaction.
/**
* The connection indicates a grouping, not an edge.
*
* +--+
* | |
* +--+ +--+
* | |
* +--+---+--+
* | |
* +--+
*/
@Test
public void testUpGroupCompaction() {
CGraph graph = new CGraph(EnumSet.allOf(Direction.class));
CTestNode upperLeft = new CTestNode(new ElkRectangle(0, 0, 20, 20));
graph.cNodes.add(upperLeft);
CTestNode lowerLeft = new CTestNode(new ElkRectangle(5, 40, 20, 20));
graph.cNodes.add(lowerLeft);
CTestNode right = new CTestNode(new ElkRectangle(25, 30, 20, 20));
graph.cNodes.add(right);
CGroup group = new CGroup(lowerLeft, right);
graph.cGroups.add(group);
compacter(graph).changeDirection(Direction.UP).compact().finish();
assertEquals(0, upperLeft.hitbox.y, EPSILON);
assertEquals(25, lowerLeft.hitbox.y, EPSILON);
assertEquals(15, right.hitbox.y, EPSILON);
}
use of org.eclipse.elk.core.math.ElkRectangle in project elk by eclipse.
the class Block method placeRectsIn.
/**
* Checks whether rectangles can fit in a given width.
* Optionally the rectangles can be directly placed.
* @param width The width
* @param placeRects Whether the rectangles should be directly placed.
* @return The bounds of this block if you would place all rectangles in the given width.
*/
private ElkRectangle placeRectsIn(final double width, final boolean placeRects) {
// Next x coordinate at which a rect shall be placed in a blockrow
double currentX = 0;
// Next y coordinate at which a rect shall be placed in a blockrow
double currentY = this.y;
double currentWidth = 0;
double currentHeight = 0;
double maxHeightInRow = 0;
double widthInRow = 0;
int row = 0;
if (placeRects) {
rows.clear();
rows.add(new BlockRow(this.x, this.y, nodeNodeSpacing));
}
// Current index in row.
int index = 0;
for (ElkNode rect : children) {
if (currentX + rect.getWidth() + (index > 0 ? nodeNodeSpacing : 0) > width && maxHeightInRow > 0) {
// Case new row
currentX = 0;
currentY += maxHeightInRow + nodeNodeSpacing;
currentWidth = Math.max(currentWidth, widthInRow);
currentHeight += maxHeightInRow + nodeNodeSpacing;
maxHeightInRow = 0;
widthInRow = 0;
if (placeRects) {
row++;
rows.add(new BlockRow(this.x, currentY, nodeNodeSpacing));
}
// Reset current index in row
index = 0;
}
widthInRow += rect.getWidth() + (index > 0 ? nodeNodeSpacing : 0);
maxHeightInRow = Math.max(maxHeightInRow, rect.getHeight());
if (placeRects) {
rows.get(row).addRectangle(rect);
}
currentX += rect.getWidth() + (index > 0 ? nodeNodeSpacing : 0);
index++;
}
currentWidth = Math.max(currentWidth, widthInRow);
currentHeight += maxHeightInRow;
if (placeRects) {
this.width = currentWidth;
this.height = currentHeight;
this.parentRow.notifyAboutNodeChange();
}
return new ElkRectangle(x, y, currentWidth, currentHeight);
}
Aggregations