package infovis.embed;

import static infovis.VecUtil.*;
import infovis.embed.pol.Interpolator;

import java.awt.geom.Point2D;

/**
 * A simple circular embedder.
 * 
 * @author Joschi <josua.krause@googlemail.com>
 */
public class CircularEmbedder extends AbstractEmbedder {

  /**
   * The weighter.
   */
  private final Weighter weighter;

  /**
   * Creates a circular embedder.
   * 
   * @param weighter The weighter.
   * @param drawer The drawer.
   */
  public CircularEmbedder(final Weighter weighter, final NodeDrawer drawer) {
    super(drawer);
    this.weighter = weighter;
  }

  @Override
  protected void step() {
    final SpringNode ref = weighter.getReferenceNode();
    if(weighter.hasChanged()) {
      Point2D refP;
      if(ref != null) {
        refP = ref.getPos();
      } else {
        refP = null;
      }
      for(final SpringNode n : weighter.nodes()) {
        final Point2D pos = weighter.getDefaultPosition(n);
        if(n == ref) {
          continue;
        }
        Point2D dest;
        if(refP == null) {
          dest = pos;
        } else {
          if(!weighter.hasWeight(n, ref)) {
            dest = new Point2D.Double();
          } else {
            final double w = weighter.weight(n, ref);
            dest = addVec(setLength(subVec(pos, refP), w), refP);
          }
        }
        n.startAnimationTo(dest, Interpolator.INTERPOLATOR, Interpolator.DURATION);
      }
    }
    for(final SpringNode n : weighter.nodes()) {
      n.animate();
    }
  }

  @Override
  protected boolean doesDrag() {
    return false;
  }

}

@@ -709,24 +709,44 @@
 hanged()) %7B%0A
+      Point2D diff;%0A
       Point2
@@ -789,56 +789,175 @@
     
-refP = ref.getPos();%0A      %7D else %7B%0A        refP
+final Point2D orig = weighter.getDefaultPosition(ref);%0A        refP = ref.getPos();%0A        diff = subVec(refP, orig);%0A      %7D else %7B%0A        refP = null;%0A        diff
  = n
@@ -1422,19 +1422,33 @@
 (subVec(
-pos
+addVec(pos, diff)
 , refP),

correct node position after multiple selections solves #16