BeadsKnot_Dowker/sketch_dowker_sphere/sketch_dowker.pde8355938226334858339.tmp
//int dowker[]={6, 10, 12, 2, 4, 8};
//int dowker[]={4,8,14,16,2,18,20,22,10,12,6};
//int dowker[]={4,10,12,14,22,2,18,20,8,6,16};
//int dowker[]={6, 8, 16, 14, 4, 18, 20, 2, 22, 12, 10};
int dowker[] = {6, 10, 16, 18, 14, 2, 20, 4, 22, 12, 8};
//int dowker[] = {6, 12, 16, 18, 14, 4, 20, 22, 2, 8, 10};
ArrayList<Node> nodes;
ArrayList<Edge>edges;


void setup() {
  size(800, 800);
  nodes = new ArrayList<Node>();
  edges = new ArrayList<Edge>();
  int len = dowker.length;
  for (int i=0; i<len; i++) {
    nodes.add(new Node(400+200*cos(PI*2*i/len), 400-200*sin(PI*2*i/len), 2*i+1, dowker[i]));
  }
  for (int i=0; i<len; i++) {
    for (int j=i+1; j<len; j++) {
      Node n1 = nodes.get(i);
      Node n2 = nodes.get(j);
      if (abs(n1.a-abs(n2.b))==1 || abs(n1.b-abs(n2.a))==1) {
        edges.add(new Edge(i,j));
      }
      if (abs(n1.a-abs(n2.b))==2*len-1 || abs(n1.b-abs(n2.a))==2*len-1) {
        edges.add(new Edge(i,j));
      }
    }
  }
}

void draw() {
  background(200);
  for (int e=0; e<edges.size(); e++) {
    Edge ee = edges.get(e);
    Node ees = nodes.get(ee.s);
    Node eet = nodes.get(ee.t);
    line (ees.x, ees.y, eet.x, eet.y);
  }
  for (int n=0; n<nodes.size(); n++) {
    Node nn = nodes.get(n);
    if (nn == draggedNode) {
      fill(255, 0, 255);
    } else {
      fill(0, 0, 255);
    }
    stroke(0);
    ellipse(nn.x, nn.y, 10, 10);
    text(""+nn.a+","+nn.b, nn.x+10, nn.y+10);
  }
}

class Node {
  int a, b;
  float x, y;
  Node(float _x, float _y, int _i, int _j) {
    x=_x;
    y=_y;
    a=_i;
    b=_j;
  }
}

class Edge {
  int s, t;
  Edge(int _s, int _t) {
    s=_s;
    t=_t;
  }
}

Node draggedNode=null;

void mousePressed() {
  for (int n=0; n<nodes.size(); n++) {
    Node nn = nodes.get(n);
    if (dist(nn.x, nn.y, mouseX, mouseY)<10) {
      draggedNode = nn;
      return ;
    }
  }
}

void mouseDragged() {
  if (draggedNode != null) {
    draggedNode.x = mouseX;
    draggedNode.y = mouseY;
  }
}

void mouseReleased() {
  draggedNode = null;
}

void keyPressed() {
  modify1();
}

void modify1() {
  int len = nodes.size();
  //三角形を探す
  for(int a=0; a<len; a++){
    for(int b=a+1; b<len; b++){
      for(int c=b+1; c<len; c++){
        boolean eab=false; 
        boolean ebc=false; 
        boolean eca=false; 
        for(int e=0; e<edges.size(); e++){
          Edge ee = edges.get(e);
          if(ee.s == a && ee.t == b ) eab = true; 
          else if(ee.s == b && ee.t == a ) eab = true; 
          else if(ee.s == b && ee.t == c ) ebc = true; 
          else if(ee.s == c && ee.t == b ) ebc = true; 
          else if(ee.s == c && ee.t == a ) eca = true; 
          else if(ee.s == a && ee.t == c ) eca = true; 
        }
      }
    }
  }
  float dx[] = new float [len];
  float dy[] = new float [len];
  for (int n=0; n<len; n++) {
    dx[n] = dy[n] = 0f;
  }
  for (int n=0; n<len; n++) {
    Node nn = nodes.get(n);
    float zx=0;
    float zy=0;
    for (int e=0; e<edges.size(); e++) {
      Edge ee = edges.get(e);
      if (ee.s == nn) {
        zx += ee.t.x;
        zy += ee.t.y;
      } else if (ee.t == nn) {
        zx += ee.s.x;
        zy += ee.s.y;
      }
    }
    zx /= 4;
    zy /= 4;
    float ax = zx - nn.x;
    float ay = zy - nn.y;
    ax *= 0.1;
    ay *= 0.1;
    dx[n] = ax;
    dy[n] = ay;
  }
  for (int n=0; n<len; n++) {
    Node nn = nodes.get(n);
    nn.x += dx[n];
    nn.y += dy[n];
  }
}

int countAIntersection() {
  return 0;
}

//参考文献
//https://www2.cs.arizona.edu/~kpavlou/Tutte_Embedding.pdf
//これはかなりわかりやすい！！
void TutteEmbedding(){
  int len = nodes.size();
  int mat[][] = new int[len][len];
  //行列を作る
  
  
  //三角形を探す
  for(int a=0; a<len; a++){
    for(int b=a+1; b<len; b++){
      for(int c=b+1; c<len; c++){
        
      }
    }
  }
}
