-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathcount_neighbors.c
41 lines (38 loc) · 1.04 KB
/
count_neighbors.c
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
#include <R.h>
#include <Rdefines.h>
#include <R_ext/Error.h>
void R_init_ggpointdensity(DllInfo* info) {
R_registerRoutines(info, NULL, NULL, NULL, NULL);
R_useDynamicSymbols(info, TRUE);
}
SEXP count_neighbors_( SEXP x, SEXP y, SEXP r2, SEXP xy ) {
double r2p = REAL(r2)[0];
double xyp = REAL(xy)[0];
double yxp = 1.0 / xyp;
int l = Rf_length(x);
if( Rf_length(y) != l )
error( "Vectors x and y differ in length." );
SEXP res = Rf_allocVector( INTSXP, l );
int *resp = INTEGER(res);
double *xp = REAL(x);
double *yp = REAL(y);
for( int i = 0; i < l; i++ ) {
int s = 0;
double xi = xp[i];
double yi = yp[i];
for( int j = 0; j < l; j++ ) {\
double xj = xp[j];
double yj = yp[j];
double dx = xi - xj;
double dy = yi - yj;
if((xi == xj && yi == yj) ||
(xi == xj && xyp * dy * dy <= r2p) ||
(yi == yj && xyp * dx * dx <= r2p) ||
(yxp*dx*dx + xyp*dy*dy <= r2p) ){
s++;
}
}
resp[i] = s;
}
return res;
}