| 1 |
/* |
| 2 |
* DiTAA - Diagrams Through Ascii Art |
| 3 |
* |
| 4 |
* Copyright (C) 2004 Efstathios Sideris |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 19 |
* |
| 20 |
*/ |
| 21 |
package org.stathissideris.ascii2image.graphics; |
| 22 |
|
| 23 |
import org.stathissideris.ascii2image.text.CellSet; |
| 24 |
import org.stathissideris.ascii2image.text.TextGrid; |
| 25 |
|
| 26 |
/** |
| 27 |
* |
| 28 |
* @author Efstathios Sideris |
| 29 |
*/ |
| 30 |
public abstract class DiagramComponent { |
| 31 |
|
| 32 |
private static final boolean DEBUG = false; |
| 33 |
|
| 34 |
protected static ShapePoint makePointForCell(TextGrid.Cell cell, TextGrid grid, int cellWidth, int cellHeight, boolean allRound){ |
| 35 |
if (DEBUG) |
| 36 |
System.out.println("Found point at cell "+cell); |
| 37 |
if(grid.isCorner(cell) && allRound){ |
| 38 |
return new ShapePoint( |
| 39 |
cell.x * cellWidth + cellWidth/2, |
| 40 |
cell.y * cellHeight + cellHeight/2, |
| 41 |
ShapePoint.TYPE_ROUND |
| 42 |
); |
| 43 |
} else if(grid.isNormalCorner(cell)){ |
| 44 |
return new ShapePoint( |
| 45 |
cell.x * cellWidth + cellWidth/2, |
| 46 |
cell.y * cellHeight + cellHeight/2, |
| 47 |
ShapePoint.TYPE_NORMAL |
| 48 |
); |
| 49 |
} else if(grid.isRoundCorner(cell)){ |
| 50 |
return new ShapePoint( |
| 51 |
cell.x * cellWidth + cellWidth/2, |
| 52 |
cell.y * cellHeight + cellHeight/2, |
| 53 |
ShapePoint.TYPE_ROUND |
| 54 |
); |
| 55 |
} else if(grid.isLinesEnd(cell)){ |
| 56 |
return new ShapePoint( |
| 57 |
cell.x * cellWidth + cellWidth/2, |
| 58 |
cell.y * cellHeight + cellHeight/2, |
| 59 |
ShapePoint.TYPE_NORMAL |
| 60 |
); |
| 61 |
} else if(grid.isIntersection(cell)){ |
| 62 |
return new ShapePoint( |
| 63 |
cell.x * cellWidth + cellWidth/2, |
| 64 |
cell.y * cellHeight + cellHeight/2, |
| 65 |
ShapePoint.TYPE_NORMAL |
| 66 |
); |
| 67 |
} |
| 68 |
throw new RuntimeException("Cannot make point for cell "+cell); |
| 69 |
} |
| 70 |
|
| 71 |
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight){ |
| 72 |
return createClosedFromBoundaryCells(grid, cells, cellWidth, cellHeight, false); |
| 73 |
} |
| 74 |
|
| 75 |
public static DiagramComponent createClosedFromBoundaryCells(TextGrid grid, CellSet cells, int cellWidth, int cellHeight, boolean allRound){ |
| 76 |
if(cells.getType(grid) == CellSet.TYPE_OPEN) throw new IllegalArgumentException("CellSet is closed and cannot be handled by this method"); |
| 77 |
if(cells.size() < 2) return null; |
| 78 |
|
| 79 |
DiagramShape shape = new DiagramShape(); |
| 80 |
shape.setClosed(true); |
| 81 |
if(grid.containsAtLeastOneDashedLine(cells)) shape.setStrokeDashed(true); |
| 82 |
|
| 83 |
TextGrid workGrid = new TextGrid(grid.getWidth(), grid.getHeight()); |
| 84 |
grid.copyCellsTo(cells, workGrid); |
| 85 |
|
| 86 |
if (DEBUG){ |
| 87 |
System.out.println("Making closed shape from buffer:"); |
| 88 |
workGrid.printDebug(); |
| 89 |
} |
| 90 |
|
| 91 |
TextGrid.Cell start = (TextGrid.Cell) cells.getFirst(); |
| 92 |
if(workGrid.isCorner(start)) shape.addToPoints(makePointForCell(start, workGrid, cellWidth, cellHeight, allRound)); |
| 93 |
TextGrid.Cell previous = start; |
| 94 |
TextGrid.Cell cell = null; |
| 95 |
CellSet nextCells = workGrid.followCell(previous); |
| 96 |
if(nextCells.size() == 0) return null; |
| 97 |
cell = (TextGrid.Cell) nextCells.getFirst(); |
| 98 |
if(workGrid.isCorner(cell)) shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound)); |
| 99 |
|
| 100 |
while(!cell.equals(start)){ |
| 101 |
nextCells = workGrid.followCell(cell, previous); |
| 102 |
if(nextCells.size() == 1) { |
| 103 |
previous = cell; |
| 104 |
cell = (TextGrid.Cell) nextCells.getFirst(); |
| 105 |
if(!cell.equals(start) && workGrid.isCorner(cell)) |
| 106 |
shape.addToPoints(makePointForCell(cell, workGrid, cellWidth, cellHeight, allRound)); |
| 107 |
} else if(nextCells.size() > 1) { |
| 108 |
return null; |
| 109 |
} else { |
| 110 |
throw new RuntimeException("cannot create closed shape from boundary cells, nowhere to go from " |
| 111 |
+ cell + " coming from " + previous + " in grid:\n" + grid |
| 112 |
+"\nmaybe you have an edge pointing nowhere?"); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
return shape; |
| 117 |
|
| 118 |
} |
| 119 |
} |