ternary operator?

I would like to know what do the following lines mean?

1
2
3
4
5
6
7
 glVertex3d((aLines[i][0] & (1 << 0)) ? minPt[0] : maxPt[0],
                   (aLines[i][0] & (1 << 1)) ? minPt[1] : maxPt[1],
                   (aLines[i][0] & (1 << 2)) ? minPt[2] : maxPt[2]);

glVertex3d((aLines[i][1] & (1 << 0)) ? minPt[0] : maxPt[0],
                   (aLines[i][1] & (1 << 1)) ? minPt[1] : maxPt[1],
                   (aLines[i][1] & (1 << 2)) ? minPt[2] : maxPt[2]);
The operator works like so:

 
(condition) ? (use_if_true) : (use_if_false)


So this:
1
2
3
glVertex3d((aLines[i][0] & (1 << 0)) ? minPt[0] : maxPt[0],
                   (aLines[i][0] & (1 << 1)) ? minPt[1] : maxPt[1],
                   (aLines[i][0] & (1 << 2)) ? minPt[2] : maxPt[2]);


Is a shorthand way of saying this:
1
2
3
4
5
6
7
8
9
10
if( aLines[i][0] & (1 << 0) )  param0 = minPt[0];
else                           param0 = maxPt[0];

if( aLines[i][0] & (1 << 1) )  param1 = minPt[1];
else                           param1 = maxPt[1];

if( aLines[i][0] & (1 << 2) )  param2 = minPt[2];
else                           param2 = maxPt[2];

glVertex3d( param0, param1, param2 );
@Disch,
Thank you. Another question, the aLines is
1
2
3
4
5
6
 static const HDint aLines[12][2] = 
    {
        { 0, 1 }, { 1, 3 }, { 3, 2 }, { 2, 0 },
        { 4, 5 }, { 5, 7 }, { 7, 6 }, { 6, 4 },
        { 0, 4 }, { 1, 5 }, { 2, 6 }, { 3, 7 }
    };


In this case, what is the aLines[0][0] or you stated it param0?
It does look like that you do draw a cube. Your original two glVertex3d()-calls are in a loop, are they not?
@keskiverto,
Yes, you are right. I want to know the actual vertices to convert it to Quads. This is my ultimate goal.
Each value in aLines is a vertex. There are numbers [0..7], eight distinct values. Each value is x*1+y*2+z*4, where x, y, z are either 0 or 1. The binary AND expressions pick up one of x, y, or z.

As example, one face of the cube is { 0, 1, 3, 2 }.
Last edited on
@keskiverto,
Thanks. I solved the problem.
Topic archived. No new replies allowed.