Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid overlapping of elements #30

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@ public Grid layout(SortedDiagram diagram) {
Grid grid = new Grid();
for (Shape shape : diagram.getShapes()) {
Position positionOfCurrentShape = positionShape(diagram, grid, shape);
grid.add(positionOfCurrentShape);
putOnGrid(grid, positionOfCurrentShape);
log.debug("Adding {}:\n{}", shape::getName, () -> toAscii(grid));
addRowsWhenShapeIsASplit(diagram, grid, shape, positionOfCurrentShape);
}
compactGrid(grid);
return grid;
}

private void putOnGrid(Grid grid, Position positionOfCurrentShape) {
if (grid.isFilled(positionOfCurrentShape)) {
//never overlap an element
grid.addRowAfter(positionOfCurrentShape.getY());
grid.add(positionOfCurrentShape.toBuilder().y(positionOfCurrentShape.getY() + 1).build());
} else {
grid.add(positionOfCurrentShape);
}
}

private void addRowsWhenShapeIsASplit(SortedDiagram diagram, Grid grid, Shape shape, Position positionOfCurrentShape) {
List<Edge> outgoingEdges = diagram.getOutgoingEdges(shape.getId());
if (outgoingEdges.size() > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ public void removeEmptyRow(int y) {
positions.add(cellToMove.toBuilder().y(cellToMove.getY() - 1).build());
}
}

public boolean isFilled(Position position) {
return positions.stream().anyMatch(p -> p.getX() == position.getX() && p.getY() == position.getY());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,40 @@ public void should_compact_grid_in_order_to_remove_extra_space() {
position(end, 4, 1))));
}


@Test
public void should_not_overlap_joins() {
//+---------------------+
//|step1 |
//|step2 step4 |
//| end |
//| step5 |
//|step3 |
//+---------------------+
SortedDiagram diagram = SortedDiagram.builder()
.shape(step1)
.shape(step2)
.shape(step3)
.shape(step4)
.shape(step5)
.shape(end)
.edge(edge(step1, step4))
.edge(edge(step2, step5))
.edge(edge(step3, step4))
.edge(edge(step4, end))
.edge(edge(step5, end))
.build();


Grid grid = shapeLayouter.layout(diagram);

assertThat(toAscii(grid)).isEqualTo(toAscii(Grid.of(
position(step1, 0, 0),
position(step2, 0, 1),
position(step3, 0, 4),
position(step4, 1, 1),
position(step5, 1, 3),
position(end, 2, 2))));
}

}