Tuesday, 28 June 2016

Consider writing a program to draw a square 200 pixels by 200 pixels that looks like the square below with colors red (red value 255), green (green value 255), and blue (blue value 255).

11. Consider writing a program to draw a square 200 pixels by 200 pixels that looks like the square below with colors red (red value 255), green (green value 255), and blue (blue value 255).

All other RGB values are set to 0.
Consider that the square is black to start out with. Examine the following code to draw this image that has one MISSING CONDITION to draw the blue pixels.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for (var pixel of image.values()) {
x = pixel.getX();
y = pixel.getY();
if (y <= image.getHeight()/3) {
pixel.setRed(255);
}
else if (MISSING CONDITION) {
pixel.setBlue(255);
}
else if (x <= image.getWidth()/3) {
pixel.setRed(255);
}
else {
pixel.setGreen(255);
}
}

Which one of the following is the correct MISSING CONDITION to complete the program to draw the image above?



Solution


for (var p of image.values()) {
    p.setRed(0);
    p.setBlue(0);
    p.setGreen(0);
    x = p.getX();
    y = p.getY();
    if (y <= image.getHeight()/3) {
        p.setRed(255);
    }
    else if (y >= image.getHeight()/3*2) {    
        p.setBlue(255);
    }
    else if (x <= image.getWidth()/3) {
        p.setRed(255);
    }
    else {
        p.setGreen(255);
    }
}
print (image);

No comments:

Post a Comment