/*
* JFLAP - Formal Languages and Automata Package
*
*
* Susan H. Rodger
* Computer Science Department
* Duke University
* August 27, 2009
* Copyright (c) 2002-2009
* All rights reserved.
* JFLAP is open source software. Please see the LICENSE for terms.
*
*/
package automata.graph;
import java.awt.geom.Point2D;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Set;
/**
* This class defines an algorithm that lays out a graph.
*
* @author Thomas Finley & Chris Morgan
*/
public abstract class LayoutAlgorithm {
/**
* The size of the canvas on which the LayoutAlgorithm
will be implemented on.
*/
protected Dimension size;
/**
* The value for how much space in the graph a vertex is assumed to take.
*/
protected Dimension vertexDim;
/**
* The minimum space between vertices.
*/
protected double vertexBuffer;
public LayoutAlgorithm() {
size = new Dimension(900, 900);
vertexDim = new Dimension(30, 30);
vertexBuffer = 30;
}
/**
* Constructor allowing the user to customize certain values.
*
* @param pSize - value for size
.
* @param vDim - value for vertexDim
.
* @param vBuffer - value for vertexBuffer
.
*/
public LayoutAlgorithm(Dimension pSize, Dimension vDim, double vBuffer)
{
size = pSize;
vertexDim = vDim;
vertexBuffer = vBuffer;
}
/**
* Moves the vertices of the states of the graph so that they are in some pleasing position.
*
* @param graph
* the graph whose states this method shall move.
* @param notMoving
* the set of vertices that will not move at all.
*/
public abstract void layout(Graph graph, Set notMoving);
/**
* Method that can make sure that all vertices are visible in the screen. The
* leftmost vertex is shifted to an x-coordinate of buffer.width
, the highest
* vertex is shifted to a y-coordinate of buffer.height
, the rightmost vertex
* is shifted to a coordinate of size.width
- buffer.width
, and
* the bottom-most vertex is shifted to a coordinate of size.width
-
* buffer.height
. All other vertices are adjusted accordingly. Additionally,
* if the graph is too large to fit onto the current screen, the distance between the
* vertices is proportionally shrunk so that they are..
*
* @param graph
* The graph whose states this method shall move.
* @param size
* The assumed size of the screen
* @param buffer
* The minimum space from the side of the screen that a vertex can be placed at. There
* can be different values for the width and height.
* @param scaleOnlyOverflow
* If true, will only fill the screen with a graph if the points are outside
* the given size.
*/
public static void shiftOntoScreen(Graph graph, Dimension size, Dimension buffer, boolean scaleOnlyOverflow)
{
if (size==null || size.getHeight() == 0 || size.getWidth() == 0)
return;
Object[] vertices = graph.vertices();
double currentX, currentY, minX, minY, maxX, maxY, heightRatio, widthRatio;
//First, find the extreme values of x & y
minX=Integer.MAX_VALUE; minY=Integer.MAX_VALUE;
maxX=Integer.MIN_VALUE; maxY=Integer.MIN_VALUE;
for (int i=0; i maxX)
maxX = currentX;
if (currentY < minY)
minY = currentY;
if (currentY > maxY)
maxY = currentY;
}
//Then, set all points so that their coordinates range from (0...maxX-minX, 0...maxY-minY)
for (int i=0; i 1.0 || !scaleOnlyOverflow) {
for (int i=0; i 1.0 || !scaleOnlyOverflow) {
for (int i=0; igraph but are not in notMoving
*
. If called by layout()
, it should return a list of vertices that can be
* moved.
*
* @return the list of vertices
*/
public static ArrayList getMovableVertices(Graph graph, Set notMoving) {
Object[] vArray = graph.vertices();
ArrayList vertices = new ArrayList();
for (int i=0; ir and y = θ. The center is assumed to be the origin.
*
* @param graph - the graph the points are listed in
* @param vertices - a list of objects whose points need to be changed
*/
public static void cartesianToPolar(Graph graph, ArrayList vertices) {
double theta, r;
Point2D cartesian;
for (int i=0; ir and y =
* θ. The center is assumed to be the origin
*
* @param graph - the graph the points are listed in
* @param vertices - a list of objects whose points need to be changed
*/
public static void polarToCartesian(Graph graph, ArrayList vertices) {
Point2D polar, cartesian;
for (int i=0; i