Could someone explain this snippet of code i found?

Please explain what this code does(mainly the ‘pixels(i) = …’ parts):


if (height < 0) {
     underWater++;
     pixels[i] = 20 << 16 | 50 << 8 | (height / 2 + 225);
} else if (height < 2945) {
     pixels[i] = 0 << 16 | (100 + height / 19) << 8 | 0;
} else {
    pixels[i] = ((height - 2945) / 19) << 16 | 255 << 8 | ((height - 2945) / 19);
}

Thanks

Just show us the place where you found it, because it’s just basic Java you need to learn. I guess you don’t understand bitwise and bit shift operators - http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html

It is from some code mike posted that generates terrain. I like how it the color would gradually go up/down so i was taking look and found that snippet.

I understand bitshifting a little bit.

Here: http://www.java-gaming.org/topics/what-i-did-today/33622/msg/327009/view/topicseen.html#msg327003

(Snippet comes from “MapTest.java”)

From what I can tell, the author of the code is setting the pixel color components (R, G, B) at a given location i based on the value of the “height” variable.

pixels is an array where each entry depicts a specific color value. Because it is 1-dimensional, you need to specify your own “width” in order to correctly draw the array.

-Any height less than 0 has a more “blue” color as that represents the water.
-Any height less than 2945 has a more “green” color as that represents grass
-Any height that is greater or equal to 2945 has (I think) a greenish-white color.

Thanks, thought that might was what it would have been. But wasn’t sure because of the ‘bitshifts’