Package graphmatcher.helper

Source Code of graphmatcher.helper.GraphTools

package graphmatcher.helper;

import graphmatcher.graph.Edge;
import graphmatcher.graph.Graph;
import graphmatcher.graph.Vertex;

import java.awt.geom.AffineTransform;
import java.util.Random;

public class GraphTools {

  public static Graph createRandomGraph(int numberOfVertices, int numberOfEdges) {
    if (numberOfVertices < 2) {
      throw new RuntimeException("Graph mit nur einem Knoten ist nicht erlaubt!");
    }
    Random random = new Random();
    Vertex[] vertices = new Vertex[numberOfVertices];
    for (int i = 0; i < vertices.length; i++) {
      int x = random.nextInt(550);
      int y = random.nextInt(350);
      vertices[i] = new Vertex(x, y);
    }
    Edge[] edges = new Edge[numberOfEdges];
    for (int i = 0; i < edges.length; i++) {
      int vertex1 = random.nextInt(numberOfVertices);
      int vertex2 = vertex1;
      while (vertex1 == vertex2) {
        vertex2 = random.nextInt(numberOfVertices);
      }
      edges[i] = new Edge(vertex1, vertex2);
    }
    return new Graph(vertices, edges);
  }

  @Deprecated
  public static Graph randomTransform(Graph graph) {
    AffineTransform affineTransform = new AffineTransform();
    Random random = new Random();
    int angle = random.nextInt(360);

    angle = 0;

    affineTransform.rotate(Math.toRadians(angle));
    double scaleX = random.nextDouble() * 1 + 0.5;
    double scaley = random.nextDouble() * 1 + 0.5;
    affineTransform.scale(scaleX, scaley);
    int translateX = random.nextInt(100);
    int translateY = random.nextInt(100);
    affineTransform.translate(translateX, translateY);
    System.out.println("Zufallstranformation:");
    System.out.println("Angle  = " + angle);
    System.out.println("ScaleX = " + scaleX);
    System.out.println("ScaleY = " + scaley);
    System.out.println("TransX = " + translateX);
    System.out.println("TransY = " + translateY);
    transformGraph(graph, affineTransform);
    return graph;

  }

  public static void transformGraph(Graph graph, AffineTransform affineTransform) {
    for (int i = 0; i < graph.vertices().length; i++) {
      Vertex vertex = graph.vertices()[i];
      double[] point = new double[2];
      point[0] = vertex.x;
      point[1] = vertex.y;
      double[] newPoint = new double[2];
      affineTransform.transform(point, 0, newPoint, 0, 1);
      vertex.x = (int) newPoint[0];
      vertex.y = (int) newPoint[1];

      graph.reset();
    }
  }

  // public static Graph transformGraph(Graph graph, AffineTransform
  // affineTransform) {
  // Graph result = graph.clone();
  // // Graph result = graph;
  // for (int i = 0; i < result.vertices().length; i++) {
  // Vertex vertex = result.vertices()[i];
  // double[] point = new double[2];
  // point[0] = vertex.x;
  // point[1] = vertex.y;
  // double[] newPoint = new double[2];
  // affineTransform.transform(point, 0, newPoint, 0, 1);
  // vertex.x = (int) newPoint[0];
  // vertex.y = (int) newPoint[1];
  // }
  // result.setName(result.getName() + "(detransform)");
  // return result;
  // }

  public static AffineTransform createRotation(double angle) {
    AffineTransform transform = new AffineTransform();
    transform.rotate(Math.toRadians(angle));
    return transform;
  }
}
TOP

Related Classes of graphmatcher.helper.GraphTools

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.