We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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.
On the first swap, everything is fine and dandy.
But after the second swap everything goes haywire.
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
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 ofparallelX
and in each subsequent swap andcolumnIndices
in every subsequent swap. I see that though the values ofcolumnIndices
are being swapped correctly, after the 2nd swap, the values ofparallelX
are still incorrect. Any idea why? The values are as follows: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:
Any ideas why that's the case?