vendredi 24 juin 2016

For ANY neighbouring Element do c++ random

I'm trying to implement an Algorithm, that depends on choosing any Element of the neighbouring six Elements, that is stored in an 8bit uint. At the Moment I compare

if ((temp & 16) != 0){ //do something }

for 1, 2, 4, 8, 16 and 32 to compare the corresponding Bits. If this Element is in the list it executes the code, but at the Moment it always chooses the same Element from the List (the last one overwrites the results of the previous ones).

Is there an elegant way to choose a random Element from the Elements in the list? So it doesn't take the same Element in every Iteration. The Code is in C++. Thanks in advance.

Edit:

        if ((temp & 1) != 0){
            if (lHeight[sX-1][sY][sZ] < lHeight[sX][sY][sZ]){
                lNf = 1;
                lDist[sX][sY][sZ] = lDist[sX - 1][sY][sZ] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX - 1][sY][sZ];
                lHeight[sX][sY][sZ] = lHeight[sX - 1][sY][sZ];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX - 1][sY][sZ]){
                    lLabel[sX][sY][sZ] = lLabel[sX - 1][sY][sZ];
                }
            }
        }
        if ((temp & 2) != 0){
            if (lHeight[sX +1][sY][sZ] < lHeight[sX][sY][sZ]){
                lNf = 2;
                lDist[sX][sY][sZ] = lDist[sX + 1][sY][sZ] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX + 1][sY][sZ];
                lHeight[sX][sY][sZ] = lHeight[sX + 1][sY][sZ];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX + 1][sY][sZ]){
                    lLabel[sX][sY][sZ] = lLabel[sX + 1][sY][sZ];
                }
            }
        }
        if ((temp & 4) != 0){
            if (lHeight[sX][sY-1][sZ] < lHeight[sX][sY][sZ]){
                lNf = 4;
                lDist[sX][sY][sZ] = lDist[sX][sY-1][sZ] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX][sY-1][sZ];
                lHeight[sX][sY][sZ] = lHeight[sX][sY-1][sZ];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX][sY-1][sZ]){
                    lLabel[sX][sY][sZ] = lLabel[sX][sY-1][sZ];
                }
            }
        }
        if ((temp & 8) != 0){
            if (lHeight[sX][sY+1][sZ] < lHeight[sX][sY][sZ]){
                lNf = 8;
                lDist[sX][sY][sZ] = lDist[sX][sY+1][sZ] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX][sY+1][sZ];
                lHeight[sX][sY][sZ] = lHeight[sX][sY+1][sZ];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX][sY+1][sZ]){
                    lLabel[sX][sY][sZ] = lLabel[sX][sY+1][sZ];
                }
            }
        }
        if ((temp & 16) != 0){
            if (lHeight[sX][sY][sZ-1] < lHeight[sX][sY][sZ]){
                lNf = 16;
                lDist[sX][sY][sZ] = lDist[sX][sY][sZ-1] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX][sY][sZ-1];
                lHeight[sX][sY][sZ] = lHeight[sX][sY][sZ-1];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX][sY][sZ-1]){
                    lLabel[sX][sY][sZ] = lLabel[sX][sY][sZ-1];
                }
            }
        }
        if ((temp & 32) != 0){
            if (lHeight[sX][sY][sZ+1] < lHeight[sX][sY][sZ]){
                lNf = 32;
                lDist[sX][sY][sZ] = lDist[sX][sY][sZ+1] + 1;
                lLabel[sX][sY][sZ] = lLabel[sX][sY][sZ+1];
                lHeight[sX][sY][sZ] = lHeight[sX][sY][sZ+1];
                state[k] = false;
            }
            else{
                if (lLabel[sX][sY][sZ] > lLabel[sX][sY][sZ+1]){
                    lLabel[sX][sY][sZ] = lLabel[sX][sY][sZ+1];
                }
            }
        }

I set the Bits if the Grayvalues of the neighbouring Pixels are the same. But now I want to choose any of the Elements and not the last one.




Aucun commentaire:

Enregistrer un commentaire