| •1 |
|
| 2 |
|
| 3 |
|
| •4 |
|
| 5 |
|
| •6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| •11 |
|
| 12 |
|
| 13 |
|
| •14 |
|
| 15 |
|
| •16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package org.stathissideris.ascii2image.graphics; |
| 21 |
|
| •22 |
import java.util.ArrayList; |
| 23 |
import java.util.Iterator; |
| 24 |
|
| 25 |
import org.stathissideris.ascii2image.text.CellSet; |
| 26 |
import org.stathissideris.ascii2image.text.TextGrid; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public abstract class DiagramComponent { |
| 33 |
|
| 34 |
private static final boolean DEBUG = false; |
| 35 |
|
| 36 |
protected static ShapePoint makePointForCell(TextGrid.Cell cell, TextGrid grid, int cellWidth, int cellHeight, boolean allRound){ |
| 37 |
if (DEBUG) |
| 38 |
System.out.println("Found point at cell "+cell); |
| 39 |
if(grid.isCorner(cell) && allRound){ |
| 40 |
return new ShapePoint( |
| 41 |
cell.x * cellWidth + cellWidth/2, |
| 42 |
cell.y * cellHeight + cellHeight/2, |
| 43 |
ShapePoint.TYPE_ROUND |
| 44 |
); |
| 45 |
} else if(grid.isNormalCorner(cell)){ |
| 46 |
return new ShapePoint( |
| 47 |
cell.x * cellWidth + cellWidth/2, |
| 48 |
cell.y * cellHeight + cellHeight/2, |
| 49 |
ShapePoint.TYPE_NORMAL |
| 50 |
); |
| 51 |
} else if(grid.isRoundCorner(cell)){ |
| 52 |
return new ShapePoint( |
| 53 |
cell.x * cellWidth + cellWidth/2, |
| 54 |
cell.y * cellHeight + cellHeight/2, |
| 55 |
ShapePoint.TYPE_ROUND |
| 56 |
); |
| 57 |
} else if(grid.isLinesEnd(cell)){ |
| 58 |
return new ShapePoint( |
| 59 |
cell.x * cellWidth + cellWidth/2, |
| 60 |
cell.y * cellHeight + cellHeight/2, |
| 61 |
ShapePoint.TYPE_NORMAL |
| 62 |
); |
| 63 |
} else if(grid.isIntersection(cell)){ |
| 64 |
return new ShapePoint( |
| 65 |
cell.x * cellWidth + cellWidth/2, |
| 66 |
cell.y * cellHeight + cellHeight/2, |
| 67 |
ShapePoint.TYPE_NORMAL |
| 68 |
); |
| 69 |
} |
| 70 |
throw new RuntimeException("Cannot make point for cell "+cell); |
| 71 |
} |
| 72 |
|
| 73 |
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight){ |
| 74 |
return createClosedFromBoundaryCells(grid, cells, cellWidth, cellHeight, false); |
| 75 |
} |
| 76 |
|
| 77 |
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight, boolean allRound){ |
| 78 |
if(cells.getType(grid) == CellSet.TYPE_OPEN) throw new IllegalArgumentException("CellSet is closed and cannot be handled by this method"); |
| 79 |
if(cells.size() < 2) return null; |
| 80 |
|
| 81 |
DiagramShape shape = new DiagramShape(); |
| 82 |
shape.setClosed(true); |
| 83 |
if(grid.containsAtLeastOneDashedLine(cells)) shape.setStrokeDashed(true); |
| 84 |
|
| 85 |
TextGrid workGrid = new TextGrid(grid.getWidth(), grid.getHeight()); |
| 86 |
grid.copyCellsTo(cells, workGrid); |
| 87 |
|
| 88 |
if (DEBUG){ |
| 89 |
System.out.println("Making closed shape from buffer:"); |
| 90 |
workGrid.printDebug(); |
| 91 |
} |
| 92 |
|
| 93 |
TextGrid.Cell start = (TextGrid.Cell) cells.getFirst(); |
| 94 |
if(workGrid.isCorner(start)) shape.addToPoints(makePointForCell(start, workGrid, cellWidth, cellHeight, allRound)); |
| 95 |
TextGrid.Cell previous = start; |
| 96 |
TextGrid.Cell cell = null; |
| 97 |
CellSet nextCells = workGrid.followCell(previous); |
| 98 |
if(nextCells.size() == 0) return null; |
| 99 |
cell = (TextGrid.Cell) nextCells.getFirst(); |
| 100 |
if(workGrid.isCorner(cell)) shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound)); |
| 101 |
|
| 102 |
while(!cell.equals(start)){ |
| 103 |
nextCells = workGrid.followCell(cell, previous); |
| 104 |
if(nextCells.size() == 1) { |
| 105 |
previous = cell; |
| 106 |
cell = (TextGrid.Cell) nextCells.getFirst(); |
| 107 |
if(!cell.equals(start) && workGrid.isCorner(cell)) |
| 108 |
shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound)); |
| 109 |
} else if(nextCells.size() > 1) { |
| 110 |
return null; |
| 111 |
} else { |
| 112 |
throw new RuntimeException("cannot create closed shape from boundary cells, nowhere to go from " |
| •113 |
+ cell + " coming from " + previous + " in grid:\n" + grid |
| 114 |
+"\nmaybe you have an edge pointing nowhere?"); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
return shape; |
| 119 |
|
| 120 |
} |
| 121 |
} |