Overview of Programming Concepts
Recall the function addBorder you wrote in a programming exercise that has a parameter image and another parameter thickness. This function returns image with an added black border around each side of the image that is thickness pixels wide. https://imgur.com/HVTslRs.png Which two of the following are correct implementations of addBorder?
✅function addBorder(image, thickness){ for (var px of image.values()){ var x = px.getX(); var y = px.getY(); if (x < thickness){ px = setBlack(px); } if (x >= image.getWidth()-thickness){ px = setBlack(px); } if (y < thickness){ px = setBlack(px); } if (y >= image.getHeight()-thickness){ px = setBlack(px); } } return image; } ✅function addBorder(image, thickness){ for (var pixel of image.values()){ if (pixel.getX() < thickness){ pixel = setBlack(pixel); } if (pixel.getX() >= image.getWidth()-thickness){ pixel = setBlack(pixel); } if (pixel.getY() < thickness){ pixel = setBlack(pixel); } if (pixel.getY() >= image.getHeight()-thickness){ pixel = setBlack(pixel); } } return image; }
Consider the following program that uses the setBlack function you wrote in the Advanced Modifying Images programming exercise: function pixelOnEdge(image,pixel,horizontalThick, verticalThick){ var x = pixel.getX(); var y = pixel.getY(); if (x < verticalThick || x > image.getWidth() - verticalThick){ return true; } if (y < horizontalThick || y > image.getHeight() - horizontalThick){ return true; } return false; } function addBorders(image,horizontalThick, verticalThick){ for (var px of image.values()){ if (pixelOnEdge(image,px,horizontalThick,verticalThick)){ px = setBlack(px); } } return image; } var img = new SimpleImage("skyline.png"); img = addBorders(img,40,20); print(img);
To identify pixels that are within the borders by returning true
Consider the following image on the left, which has been modified into the image on the right with green by changing the red and blue values of some pixels to 0. https://imgur.com/undefined.png Which one of the following is most likely the code that modifies the first image to look like the second image? Hint: be sure to review how image x and y coordinates work. You can review this on our documentation page.
for (var pixel of image.values()) { x = pixel.getX(); y = pixel.getY(); if (x > y) { pixel.setRed(0); pixel.setBlue(0); } }
Which of the following could not be the output of running the program written in the previous question? Select all that apply.
https://imgur.com/RvuuquW.png
Consider the following code in which the starting image named image is all red (each pixel has red value 255, green value 0 and blue value 0) as shown below on the left and the resulting image shown on the right below is supposed to be all green, but is all yellow. The image is a 200 pixel by 200 pixel image. for (var pixel of image.values()) { if (pixel.getRed() > 250) { pixel.setGreen(255); } } https://imgur.com/IoogMwO.png
pixel.setRed(0);