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:

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
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
Get step-by-step solutions from verified subject matter experts
