Question: Need help writing this distance function in C++ below: This distance() function computes the distance between two colours, a and b. If you look at

Need help writing this distance function in C++ below:

This distance() function computes the distance between two colours, a and b. If you look at these two colours as a three-dimensional vector, computing the distance between the two colours is the same as computing the distance between two points:

Need help writing this distance function in C++ below: This distance() function

It is easiest to compute the corresponding subtractions in three distinct variables first, then square those values, sum them, and finally compute the sqrt. You will need to use a floating-point type (e.g., double or float) for these values to properly compute the distance.

When done writing this function, test it to be sure it works properly.

//////////////////////////////////////////////////////////////////////////Code Below/////////////////////////////////////////////////////////////////

#include // for std::sqrt #include // for std::array #include // for std::vector #include // for std::numeric_limits #include // for std::string #include // for std::istream #include // for std::ostream #include // for std::cin, std::cout #include // for std::transform

using namespace std; // place this after the #includes

struct rgb { unsigned char red; unsigned char green; unsigned char blue; };

istream& operator >>(istream& is, rgb& colour) { unsigned r, g, b; is >> r >> g >> b; if(r

ostream& operator (colour.red) (colour.green) (colour.blue); return os; } double distance(rgb const& a, rgb const& b) { //CODE HERE }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!