Projets

Speed Dating


PFont f;

int[][] quizzesA = {
  {
    0, 1, 2, 3
  }
  , 
  {
    3, 2, 1, 0
  }
  , 
  {
    0, 1, 0, 0
  }
};

int[][] quizzesB = {
  {
    0, 1, 2, 3
  }
  , 
  {
    0, 1, 2, 3
  }
  , 
  {
    0, 1, 0, 0
  }
  , 
  {
    1, 0, 1, 0
  }
};

int[][] speeddating;

void setup() {

  int[][] memo;
  int[][] calc;
  int[][] result;
  int[] target;
  int step=0;
  int meet=0;

  printMatrix(quizzesA);
  println();
  printMatrix(transposeMatrix(quizzesB));
  println();

  step=min(quizzesA.length, quizzesB.length);
  //meet=max(quizzesA.length, quizzesB.length);
  memo=(multiMatrix(quizzesA, transposeMatrix(quizzesB)));
  calc=memo;


  result = initMatrix(calc, 0);
  while (!endMatrix (calc))
  {
    meet++;
    printMatrix(calc);
    println();
    for (int i=0 ; i< step ; i=i+1)
    {
      target=maxMatrix(calc);
      if (memo[target[0]][target[1]]>-1)
      {
        memo[target[0]][target[1]]=-1;
        result[target[0]][target[1]]=result[target[0]][target[1]]+meet;
        calc= markMatrix(calc, target, -1);
      }
    }
    printMatrix(result);
    println();
    calc=memo;
  }
  speeddating=dragMatrix(result);
  printMatrix(speeddating);
  println();

  size(3*50*speeddating.length, 50*speeddating[0].length);
  background(255);
  f = createFont("Arial", 24);
  textFont(f);
  textAlign(CENTER, CENTER);
  smooth();
  noLoop();

}

void draw() {
 
  
  int[][] quizzesAD=dragMatrix(quizzesA);
  int[][] quizzesBD=dragMatrix(quizzesB);
  
  for (int i=0 ; i< quizzesAD.length ; i=i+1)
  {
   for (int j=0 ; j< quizzesAD[0].length ; j=j+1)
  {
    fill(0, 128,255);
    if ((j==0))fill(0, 0,255);
    if ((i==0))fill(0);
    if (!((i==0)&&(j==0))) text(quizzesAD[i][j], 40+40*i, 40+40*j);
  }
  }
  
  for (int i=0 ; i< quizzesBD.length ; i=i+1)
  {
   for (int j=0 ; j< quizzesBD[0].length ; j=j+1)
  {
    fill(255, 128,255);
    if ((j==0))fill(255, 0,255);
    if ((i==0))fill(0);
     if (!((i==0)&&(j==0))) text(quizzesBD[i][j], 220+40*i, 40+40*j);
  }
  }
  
  for (int i=0 ; i< speeddating.length ; i=i+1)
  {
   for (int j=0 ; j< speeddating[0].length ; j=j+1)
  {
    
    fill(255, 128,0);
    if ((j==0))fill(255, 0,255);
    if ((i==0))fill(0, 0,255);
     if (!((i==0)&&(j==0))) text(speeddating[i][j], 440+40*i, 40+40*j);
  }
  }
  
}

void printMatrix(int[][] matrix) {
  for (int i=0 ; i< matrix.length ; i=i+1)
  {
    for (int j=0 ; j< matrix[0].length ; j=j+1)
    {
      print("["+matrix[i][j]+"]");
    }
    println();
  }
}

boolean endMatrix(int[][] matrix) {
  boolean result = true;
  for (int i=0 ; i< matrix.length ; i=i+1)
  {
    for (int j=0 ; j< matrix[0].length ; j=j+1)
    {
      if (matrix[i][j]>-1) result = false;
    }
  }
  return result;
}

int[][] transposeMatrix(int[][] matrix) {
  int[][] result;
  result = new int[matrix[0].length][matrix.length];
  for (int i=0 ; i< result.length ; i=i+1)
  {
    for (int j=0 ; j< result[0].length ; j=j+1)
    {
      result[i][j] =matrix[j][i];
    }
  }
  return result;
}

int[][] multiMatrix(int[][] matrixA, int[][] matrixB) {
  int[][] result;
  result = new int[matrixA.length][matrixB[0].length];
  for (int i=0 ; i< result.length ; i=i+1)
  {
    for (int j=0 ; j< result[0].length ; j=j+1)
    {
      result[i][j] = 0;
      for (int k=0 ; k< matrixB.length ; k=k+1)
      {
        if (matrixA[i][k]==matrixB[k][j]) result[i][j] =result[i][j]+1;
      }
    }
  }
  return result;
}

int[] maxMatrix(int[][] matrix) {
  int[] result = {
    0, 0
  };
  int max=-1;
  for (int i=0 ; i< matrix.length ; i=i+1)
  {
    for (int j=0 ; j< matrix[0].length ; j=j+1)
    {
      if (matrix[i][j] > max) {
        max=matrix[i][j];
        result[0]=i;
        result[1]=j;
      }
    }
  }
  return result;
}


int[][] dragMatrix(int[][] matrix) {
  int[][] result;
  result = new int[matrix.length+1][matrix[0].length+1];
  for (int i=0 ; i< result.length ; i=i+1)
  {
    for (int j=0 ; j< result[0].length ; j=j+1)
    {
      if (i==0)result[i][j] =j;
      else 
      {
        if (j==0)result[i][j] =i;
        else result[i][j]=matrix[i-1][j-1];
      }
    }
  }
  return result;
}

int[][] initMatrix(int[][] matrix, int val) {
  int[][] result;
  result = new int[matrix.length][matrix[0].length];
  for (int i=0 ; i< result.length ; i=i+1)
  {
    for (int j=0 ; j< result[0].length ; j=j+1)
    {
      result[i][j] =val;
    }
  }
  return result;
}


int[][] markMatrix(int[][] matrix, int[] target, int val) {
  int[][] result;
  result = new int[matrix.length][matrix[0].length];
  for (int i=0 ; i< result.length ; i=i+1)
  {
    for (int j=0 ; j< result[0].length ; j=j+1)
    {
      if (i==target[0])result[i][j] = val;
      else 
      {
        if (j==target[1])result[i][j] = val;
        else result[i][j]=matrix[i][j];
      }
    }
  }
  return result;
}