> I understand that this should not be a very difficult problem to solve.
Most problems are not difficult to solve; we can always divide a big problem into a number of smaller problems, each of which is easy to solve.
1. start by defining a type which can represent a point in a three-dimensional cartesian coordinate system. For example:
1 2 3 4 5 6 7 8
|
struct point
{
double x ;
double y ;
double z ;
// member functions, etc ...
};
|
Verify that
point is working as expected.
2. Add a function to compute the linear distance between two points. For example:
double linear_distance( const point& a, const point& b ) ;
Test the function to make sure that it is correct.
3. Add another function to test if a point lies within a sphere. For example,
bool is_in_sphere( const point& centre, double radius, const point& apoint );
Do not proceed any further till you are confident that this is working as expected.
4. Now implement the stream insertion and extraction operators to facilitate reading and writing of points from/to streams.
1 2
|
std::istream& operator<< ( std::istream& stm, point& p ) ;
std::ostream& operator>> ( std::ostream& stm, const point& p ) ;
|
Stop, verify, test, modify, test again etc. till you are satisfied that it works.
5. Now add the functionality to read a bunch of points from a file. For example:
std::vector<point> sequence_from_file( const std::string& path2file ) ;
Stop, verify, test, modify, test again etc. till you are satisfied that this works.
6. Now on to the next step; write a function to count how many neighbours a point has.
1 2
|
std::size_t num_neighbours ( const point& apoint, double radius_of_sphere,
const std::vector<point>& point_collection ) ;
|
Stop, verify, test, modify, test again etc. till you are satisfied that this too works.
And so on, taking one small easy step at a time, till at the end you have arrived at a solution to the bigger problem.