Swapping parallel lines in a parallel coordinates visualization.

Hi, I am trying to write a parallel coordinates visualization and with some interaction. One of the interactions is swapping the parallel lines. I am able to achieve this but partially so, since it looks like my code has a bug. I can't share my entire code as it is part of my homework assignment, but I'll share the code snippet where the parallel line swapping is being carried out.

void mouseReleased() {
  float temp;
  int iTemp;

  for (int i = 0; i < parallelX.length; i++) {
    for (int j = 0; j < parallelX.length; j++) {
      if (i != j && (parallelX[i] >= parallelX[j] - mouseThreshold && parallelX[i] <= parallelX[j] + mouseThreshold)) {
        temp = parallelX[j];
        parallelX[j] = parallelXCopy[i];
        parallelX[i] = temp;

        iTemp = columnIndices[j];
        columnIndices[j] = columnIndices[i];
        columnIndices[i] = iTemp;
        for (int k = 0; k < columnIndices.length; k++) {
          println(columnIndices[k]);
        }
        println("");

        updateXHighlights(i);
        updateXHighlights(j);
        break;
      }
    }
  }
}

Here, parallelX is the variable containing the locations of the x-coordinates of the parallel lines. Since the parallel lines can be moved, the variable parallelXCopy contains the original locations of the parallel lines. mouseThreshold is the variable which depicts how close to another parallel line one parallel line has to be in order for them to be swapped. So according to my logic, the first loop iterates through each parallel line and the the second loop checks if the line in the first loop is close to any other line in the second loop except itself. If yes, it swaps them. The columnIndices variable stores the swapped indices so that the data need not be swapped in every subsequent swap, but just the indices. But I am getting the following bug in the output which I am unable to fix.

This is the original sketch.

image Original Sketch

On the first swap, everything is fine and dandy.

image First Swap

But after the second swap everything goes haywire.

image Second Swap

I feel that something is wrong with the way I am looping because these operations must only be performed once according to my understanding but they are being performed more than once. Therefore I put in a break statement. Can someone point out some anomaly here.

Thanks!

Answers

  • edited October 2013

    Keyword break only cancels the innermost loop! b-(
    If you wanna cancel all loops, you gotta use a label along w/ break, similar to goto!
    But since there's nothing else to do inside that function besides that double loop, a return would be much more straight forward! >:)

  • The return statement didn't make a difference. I printed out the values of parallelX and in each subsequent swap and columnIndices in every subsequent swap. I see that though the values of columnIndices are being swapped correctly, after the 2nd swap, the values of parallelX are still incorrect. Any idea why? The values are as follows:

    // After 1st swap
    // parallelX
    226.66667
    40.0
    413.33334
    600.0
    786.6667
    973.3334
    1160.0
    
    // columnIndices
    1
    0
    2
    3
    4
    5
    6
    
    // After 2nd swap
    // parallelX
    224.0
    40.0
    413.33334
    600.0
    786.6667
    973.3334
    1160.0
    
    // columnIndices    
    0
    1
    2
    3
    4
    5
    6
    
  • I could make some changes in the code and now, all the swaps from left to right work properly, but any swap from right to left goes haywire. The changed code is as follows:

    for (int i = 0; i < parallelX.length; i++) {
        for (int j = 0; j < parallelX.length; j++) {
          if (i != j && parallelX[i] >= parallelX[j] - mouseThreshold && parallelX[i] <= parallelX[j] + mouseThreshold) {
            if (columnIndices[i] < columnIndices[j]) {
              temp = parallelX[j];
              parallelX[j] = parallelXCopy[i];
              parallelX[i] = temp;
            } else {
              temp = parallelX[i];
              parallelX[i] = parallelXCopy[j];
              parallelX[j] = temp;
            }
    
            temp = parallelXCopy[j];
            parallelXCopy[j] = parallelXCopy[i];
            parallelXCopy[i] = temp;
    
            iTemp = columnIndices[j];
            columnIndices[j] = columnIndices[i];
            columnIndices[i] = iTemp;
            return;
          }
        }
      }
    

    Any ideas why that's the case?

Sign In or Register to comment.