Consider the following program from the lesson, shown below with missing code to add a border of 30 to the image. Consider the following sections of code that call the pixelOnEdge function in different ways. Select the three of them that will still run correctly and put a border of 30 around the image.

function setBlack(pixel) {
     pixel.setRed(0);
     pixel.setGreen(0);
     pixel.setBlue(0);
     return pixel;
}

function pixelOnEdge (pixel, image, borderWidth) {
     var x = pixel.getX();
     var y = pixel.getY();
     if (x < borderWidth) return true;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var pixel of image.values()) {
    if (pixelOnEdge(pixel, image, 30)) {
      pixel = setBlack(pixel);
     }
}

print(image);




OR


function setBlack(pixel) {
     pixel.setRed(0);
     pixel.setGreen(0);
     pixel.setBlue(0);
     return pixel;
}

function pixelOnEdge (pixel, image, borderWidth) {
     var x = pixel.getX();
     var y = pixel.getY();
     if (x < borderWidth) return true;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var px of image.values()) {
     if (pixelOnEdge(px, image, 30)) {      
          px = setBlack(px);
     }
}

print(image);

or


function setBlack(pixel) {
     pixel.setRed(0);
     pixel.setGreen(0);
     pixel.setBlue(0);
     return pixel;
}

function pixelOnEdge (pixel, image, borderWidth) {
     var x = pixel.getX();
     var y = pixel.getY();
     if (x < borderWidth) return true;
     if (y < borderWidth) return true;
     if (x >= image.getWidth() - borderWidth) return true;
     if (y >= image.getHeight() - borderWidth) return true;
     return false;
}

var image = new SimpleImage("usain.jpg");
for (var px of image.values()) {
     if (pixelOnEdge(px, image, 30)) {      
          px = setBlack(px);
     }
}

print(image);

Comments

Popular posts from this blog

Choose Your Social Media Channels Week 5 Quiz