defining neighbouring points

Hello,


I am trying to analyse my data and therefore need to write a program to do the following steps:

1) I produce a file with xyz (3D) coordinates for each point simply in this format:

x1 y1 z1
x2 y2 z2
...
...
xn yn zn

2) now what i want is to do is to analyse neighbouring point densities as follows: I want to create a sphere around each point such that the diameter of this sphere can vary

3) and would like to produce a file with output such that I collect the information about how many neighbours each point has.

I understand that this should not be a very difficult problem to solve, but I would like to ask whether anyone could possibly point me to the right direction please.

Thank you very much in advance!
The following is 2_D (not 3_D) but should get you started.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
  Name:findclosest1 
  Copyright: 
  Author: 
  Date: 15/03/08 13:08
  Description:loads a two dimensional array interactively 
  and finds the closest x,y coordinates to a given position m,n
  */
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
float coordinate [10][2];
float elements [10][2];
void print (float coordinate [][2]);
float find_closest_coordinate(float [][2],float,float);
float load_array(float elements[][2]);
int pairs;

int main()
{float m=0,n=0,c=0;
cout<<"This routine finds the coordinates in the array\n"
    <<"closest to the coordinates of a point to be entered\n";
load_array(elements );
cout<<"\nThe xy coordinate pairs are: \n\n";    
print (elements);    
cout<<"\nEnter an xy coordinate ";
cin>>m>>n;    
cout<<"The radii are: \n";
c=find_closest_coordinate(coordinate,m,n);
cout<<" with radius of: "<<c<<endl;
cout<<"\n";    
system("pause");
return 0;    
}
float load_array(float elements[][2])
{     float c;
      cout<<"\nHow many points (pairs): ";
      cin>>pairs;
      cout<<"\nEnter "<<pairs<<" pairs of coordinates\n ";
      cout<<"Separate each with a space\n";
      for(int i=0;i<pairs;i++)
      for(int j=0;j<2;j++)
      { cin>>c;
      elements [i][j]= c;
      
      }
}      
float find_closest_coordinate(float coordinate [][2],float m,float n)
{     
     int count = 0;
     float min=0,radius=0,x=0,y=0,c=0;
     float a[1]={100};
     for(int i=0;i<pairs;i++)//run through the array items
     for(int j=0;j<2;j++)      
        {x= (elements[i][j=0]-m);//subtract m from each x coordinate
        x=pow( x,2);               //and square it   
        y= (elements[i][j=1]-n); //ditto for n 
        y=pow(y,2);
        radius=sqrt(x+y);           //calculate hypotenuse
        cout<<setprecision(4)<<radius<<endl;//print each radius
        if(radius < a[0])//find which set of coordinates are closest to m,n
           { a[0]=radius;//a[0] holds the smallest radius
             count=i;}
           }
     cout<<"Closest element is: "<<elements [count][0]
         <<" "<<elements [count][1];
     c=a[0];
     return c;
}      

void print (float elements [][2])
 {
     int count=1;
     for(int i=0;i<pairs;i++)
     for(int j=0;j<2;j++)
       { cout<<" "<<elements[i][j];
         if(count%2==0 )cout<<", ";
         if(count++%10==0 )cout<<"\n";
       }  
}   
/*This routine finds the coordinates in the array
closest to the coordinates of a point to be entered

How many points (pairs): 10

Enter 10 pairs of coordinates
 Separate each with a space
2.3 3.4 2.4 3.8 4.6 7.2 6.1 5.9 8.5 7.4 3.5 5.4 8.7 9.1 4.6 6.1 8.3 3.1 1.2 2.7

The xy coordinate pairs are:

 2.3 3.4,  2.4 3.8,  4.6 7.2,  6.1 5.9,  8.5 7.4,
 3.5 5.4,  8.7 9.1,  4.6 6.1,  8.3 3.1,  1.2 2.7,

Enter an xy coordinate 4.5 6.2
The radii are:
3.561
3.189
1.005
1.628
4.176
1.281
5.104
0.1414
4.904
4.81
Closest element is: 4.6 6.1 with radius of: 0.1414
*/
> 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.


Hi JLBorges,

thanks a lot for your answer, could you please clarify points that you've made; I am a beginner, and it is quite difficult to read and be able to apply separate pieces of code, could you possibly refer me to something that explains why I should do the operations that you describe or comment on your previous post?

Thanks!

> I am a beginner, and it is quite difficult to read and be able to apply separate pieces of code

Take it one step at a time. Start with 1., then go on to 2. and so on up to step 6.

If you are stuck at a particular point, post a specific question here and someone would help you out.

Once you have completed step 6., we can talk about the next steps to be taken, and about putting it all together to get a solution to the complete problem.

Topic archived. No new replies allowed.