Пример #1
0
 @Override
 public void invoke(GObject target, Context context) {
   if (edges.mst != null) {
     edges.mst = null;
     if (settings.realtime) {
       doKruskal();
     }
   }
   NodeView view = (NodeView) target;
   Node<String> node = view.getNode();
   for (WeightedEdge<String, Double> edge : graph.getEdges()) {
     if (!(edge.getTail() == node) && !(edge.getHead() == node)) {
       continue;
     }
     Node<String> otherNode = (node == edge.getTail() ? edge.getHead() : edge.getTail());
     NodeView otherView = null;
     for (NodeView couldBeOther : nodes) {
       if (couldBeOther.getNode().equals(otherNode)) {
         otherView = couldBeOther;
         break;
       }
     }
     if (otherView != null) {
       edge.setWeight(calculateWeightByDistance(view, otherView));
     }
   }
 }
Пример #2
0
    @Override
    public void paint(Graphics2D g) {

      g.setColor(Color.BLACK);
      for (NodeView view : nodes) {
        Node<String> node = view.getNode();
        for (NodeView other : nodes) {
          Node<String> otherNode = other.getNode();
          if (graph.connected(node, otherNode)) {
            Line2D l2d = new Line2D.Double(view.getX(), view.getY(), other.getX(), other.getY());
            // stroke width asymptotically approaches 10
            // 10 (1 - r^(w/k))
            // w = weight
            // r = convergence time
            // k = scalar for weight

            // find edge
            Set<? extends WeightedEdge<String, Double>> neighboringEdges =
                graph.getNeighboringEdges(node);
            for (WeightedEdge<String, Double> edge : neighboringEdges) {
              if (edge.getTail() == otherNode) {
                Color headColor = mst != null && mst.contains(edge) ? Color.RED : Color.BLACK;

                Color tailColor =
                    (edge instanceof DirectedEdge)
                        ? new Color(headColor.getRGB() & 0x1FFFFFFF, true)
                        : headColor;
                g.setPaint(
                    new GradientPaint(
                        new Point2D.Double(view.getX(), view.getY()), headColor,
                        new Point2D.Double(other.getX(), other.getY()), tailColor));

                float width =
                    (float)
                        (10
                            * (1f
                                - Math.pow(
                                    0.8f,
                                    Math.abs(edge.getWeight())
                                        / (settings.useDistanceWeight ? 6f : 3f))));
                if (edge.getWeight() >= 0) {
                  g.setStroke(new BasicStroke(width));
                } else {
                  g.setStroke(
                      new BasicStroke(
                          width,
                          BasicStroke.CAP_BUTT,
                          BasicStroke.JOIN_ROUND,
                          10.0f,
                          new float[] {width * 4},
                          0));
                }
              }
            }
            g.draw(l2d);
          }
        }
        super.paint(g);
      }
    }
Пример #3
0
 @Override
 public void viewShown() {
   if (settings.useDistanceWeight) {
     // temporary map
     Map<Node<String>, NodeView> map = new HashMap<>();
     for (NodeView nv : nodes) {
       map.put(nv.getNode(), nv);
     }
     Set<? extends WeightedEdge<String, Double>> edges = graph.getEdges();
     for (WeightedEdge<String, Double> edge : edges) {
       edge.setWeight(calculateWeightByDistance(map.get(edge.getHead()), map.get(edge.getTail())));
     }
   }
 }