Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Algorithms;
- import Graph.Graph;
- import Graph.Vertex;
- import Visualization.GraphVisualization;
- import java.util.ArrayList;
- public class Calculation {
- private static final int START_INDEX = 0;
- private Graph graph;
- private int type;
- public static int WIDTH = GraphVisualization.WIDTH;
- public static int HEIGHT = GraphVisualization.HEIGHT;
- public Calculation(Graph graph) {
- this.graph = graph;
- }
- private void dfs(Vertex v) {
- v.setMark(1);
- for (Vertex u: v.getChild()) {
- if (u.getMark() == 0) {
- u.setParent(v); //устанавливаем родителя
- u.getChild().remove(v); //удаляем ссылку на родителя
- dfs(u);
- }
- }
- }
- private void makeTree(Vertex v) {
- v.setRoot(true);
- dfs(v);
- for (Vertex vert: graph.getVertices()) {
- ArrayList<Vertex> temp = new ArrayList<>();
- for (Vertex u: vert.getChild()) {
- if (u.getParent() == vert) {
- temp.add(u);
- }
- }
- vert.setChild(temp);
- }
- }
- public Graph calculateGraph(int type) {
- this.type = type;
- if (type == 3) {
- makeTree(graph.get(START_INDEX));
- Algorithm3.useAlgorithm(graph);
- }
- //printGraph();
- //System.out.println(graph);
- if (type == 5) {
- makeTree(graph.get(START_INDEX));
- Algorithm5.useAlgorithm(graph);
- }
- //Algorithm3.useAlgorithm(graph);
- if (type == 1) {
- makeTree(graph.get(START_INDEX));
- Algorithm5.useAlgorithm(graph);
- Algorithm1.useAlgorithm(graph);
- }
- for (Vertex v: graph.getVertices()) {
- System.out.println("COORDINATES " + v.getIndex() + " (" + v.getX() + "," + v.getY() + ") w = " + v.getWidth() + " h = " + v.getHeight());
- System.out.println(" " + "sign: (" + v.getSign().getX() + "," + v.getSign().getY() + ") w = " + v.getSign().getWidth() + " h = " + v.getSign().getHeight());
- }
- convertCoordinates(graph);
- return graph;
- }
- private void convertCoordinates(Graph graph) {
- calculateWidthAndHeight(graph);
- for (Vertex v: graph.getVertices()) {
- double sx = v.getSign().getX();
- double sy = v.getSign().getY();
- v.setX(v.getX() / WIDTH);
- v.setY(v.getY() / HEIGHT);
- v.setWidth(v.getWidth() / WIDTH);
- v.setHeight(v.getHeight() / HEIGHT);
- //System.out.println("old " + v.getIndex() + " sign y = " + v.getSign().getY() + " height = " + HEIGHT + " y / height = " + (v.getSign().getY() / HEIGHT));
- v.getSign().setX(sx / WIDTH);
- v.getSign().setY(sy / HEIGHT);
- v.getSign().setWidth(v.getSign().getWidth() / WIDTH);
- v.getSign().setHeight(v.getSign().getHeight() / HEIGHT);
- System.out.println("CONVERTED COORDINATES " + v.getIndex() + " (" + v.getX() + "," + v.getY() + ") w = " + v.getWidth() + " h = " + v.getHeight());
- System.out.println(" " + "sign: (" + v.getSign().getX() + "," + v.getSign().getY() + ") w = " + v.getSign().getWidth() + " h = " + v.getSign().getHeight());
- }
- for (int i = 0; i < graph.getRadials().size(); i++) {
- double r = graph.getRadials().get(i) / (WIDTH < HEIGHT? WIDTH : HEIGHT);
- graph.getRadials().set(i, r);
- }
- }
- private void calculateWidthAndHeight(Graph graph) {
- double up = Double.NEGATIVE_INFINITY;
- double down = Double.POSITIVE_INFINITY;
- double right = up;
- double left = down;
- for (Vertex v: graph.getVertices()) {
- up = Math.max(v.getY() + v.getHeight() / 2, up);
- down = Math.min(v.getSign().getY() - v.getSign().getHeight() / 2, down);
- right = Math.max(v.getSign().getX() + v.getSign().getWidth() / 2, right);
- left = Math.min(v.getSign().getX() - v.getSign().getWidth() / 2, left);
- }
- double width = right - left;
- double height = up - down;
- System.out.println("width = " + width + " height = " + height + " right = " + right + " left = " + left + " up = " + up + " down = " + down);
- if (type == 3 && width < WIDTH && height < HEIGHT) {
- if (right > WIDTH / 2)
- translateLeft(graph, right, left);
- else if (left < - WIDTH / 2)
- translateRight(graph, left, right);
- else if (up > HEIGHT / 2)
- translateDown(graph, up, down);
- else if (down < - HEIGHT / 2)
- translateUp(graph, down, up);
- }
- if (width > WIDTH || height > HEIGHT) {
- double side = width > height? width : height;
- WIDTH = HEIGHT = (int) side + 1;
- System.out.println("HERE HERE HERE");
- }
- }
- private void translateLeft(Graph graph, double right, double left) {
- double offset = 0.0;
- while (right > WIDTH / 2 || left > - WIDTH / 2) {
- right -= 5.0;
- left -= 5.0;
- offset -= 5.0;
- }
- for (Vertex v: graph.getVertices()) {
- v.setX(v.getX() + offset);
- }
- }
- private void translateRight(Graph graph, double left, double right) {
- double offset = 0.0;
- while (left < - WIDTH / 2 || right < WIDTH / 2) {
- left += 5.0;
- offset += 5.0;
- }
- for (Vertex v: graph.getVertices()) {
- v.setX(v.getX() + offset);
- }
- }
- private void translateDown(Graph graph, double up, double down) {
- double offset = 0.0;
- while (up > HEIGHT / 2 || down > - HEIGHT / 2) {
- up -= 5.0;
- offset -= 5.0;
- }
- for (Vertex v: graph.getVertices()) {
- v.setY(v.getY() + offset);
- }
- }
- private void translateUp(Graph graph, double down, double up) {
- double offset = 0.0;
- while (down < - HEIGHT / 2 || up < HEIGHT) {
- down += 5.0;
- offset += 5.0;
- }
- for (Vertex v: graph.getVertices()) {
- v.setY(v.getY() + offset);
- }
- }
- private void printGraph() {
- for (Vertex v: graph.getVertices()) {
- System.out.println("v = " + v.getIndex());
- for (Vertex u: v.getChild()) {
- System.out.println(" childs = " + u.getIndex() + " " + (u.getParent() != null ? u.getParent().getIndex() : "null"));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement