#include <array> #include <random> using namespace std; class Pixel { public: Pi
ID: 3903856 • Letter: #
Question
#include <array>
#include <random>
using namespace std;
class Pixel {
public:
Pixel()
{
random_device device;
mt19937 engine(device());
uniform_int_distribution<uint16_t> dist{ 0, UINT8_MAX };
_color = { static_cast<uint8_t>(dist(engine)), static_cast<uint8_t>(dist(engine)), static_cast<uint8_t>(dist(engine)) };
}
// TODO: add a public member function with signature 'void Fade()'
// this method should divide the color values in half
// i.e. if _color = {120, 230, 56}, then final _color = {60, 115, 28}
private:
array<uint8_t, 3> _color;
};
// A pixel holds color values as a three-component array where Red, Green, and Blue are stored
// as individual components. The above class generates a randomly colored Pixel when it is
// instantiated by randomly setting the Red, Green, and Blue values. In our class, we are using
// a uint8_t to store the pixel value, so it will be an int between 0 and 255.
// In some applications, it is useful to manipulate a pixel, for example, we may want to fade the
// color by reducing the strength of each of the component values
// OBJECTIVE: Add a public member function to the Pixel class with the signature: 'void Fade()'.
// This function should reduce each of the individual components of _color by half. Then, in main
// below, add code to call "Fade" on every pixel in the screen.
int main()
{
// create an array of 1000 Pixel objects
array<Pixel, 1000> screen;
// TODO - fade all of the pixels
}
Explanation / Answer
Given below is the completed code.
#include <array>
#include <random>
using namespace std;
class Pixel {
public:
Pixel()
{
random_device device;
mt19937 engine(device());
uniform_int_distribution<uint16_t> dist{ 0, UINT8_MAX };
_color = { static_cast<uint8_t>(dist(engine)), static_cast<uint8_t>(dist(engine)), static_cast<uint8_t>(dist(engine)) };
}
// TODO: add a public member function with signature 'void Fade()'
// this method should divide the color values in half
// i.e. if _color = {120, 230, 56}, then final _color = {60, 115, 28}
void Fade()
{
for(int i = 0; i < 3; i++)
_color[i] = _color[i] / 2;
}
private:
array<uint8_t, 3> _color;
};
// A pixel holds color values as a three-component array where Red, Green, and Blue are stored
// as individual components. The above class generates a randomly colored Pixel when it is
// instantiated by randomly setting the Red, Green, and Blue values. In our class, we are using
// a uint8_t to store the pixel value, so it will be an int between 0 and 255.
// In some applications, it is useful to manipulate a pixel, for example, we may want to fade the
// color by reducing the strength of each of the component values
// OBJECTIVE: Add a public member function to the Pixel class with the signature: 'void Fade()'.
// This function should reduce each of the individual components of _color by half. Then, in main
// below, add code to call "Fade" on every pixel in the screen.
int main()
{
// create an array of 1000 Pixel objects
array<Pixel, 1000> screen;
// TODO - fade all of the pixels
for(int i = 0; i < screen.size(); i++)
screen[i].Fade();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.