#include // to use realloc #include // to use NAN #include // to use printf double* getpoint(double* points, unsigned dim, unsigned id) { double* out = malloc(dim * sizeof(double)); for(unsigned i = 0; i < dim; i++) { out[i] = points[id*dim+i]; } return out; } /* dot product of two vectors */ double dotproduct(double* p1, double* p2, unsigned dim) { double out = 0; for(unsigned i = 0; i < dim; i++) { out += p1[i] * p2[i]; } return out; } /* middle of segment [p1,p2] */ double* middle(double* p1, double* p2, unsigned dim) { double* out = malloc(dim * sizeof(double)); for(unsigned i = 0; i < dim; i++) { out[i] = (p1[i] + p2[i]) / 2.0; } return out; } /* vector of NANs */ double* nanvector(int dim) { double* out = malloc(dim * sizeof(double)); for(unsigned i = 0; i < dim; i++) { out[i] = NAN; } return out; } // to use the qsort function int cmpfunc(const void *a, const void *b) { return *(int*)a - *(int*)b; } int cmpfuncdbl(const void *a, const void *b) { return *(double*)a - *(double*)b > 0 ? 1 : -1; } void qsortu(unsigned* vector, unsigned length) { qsort(vector, length, sizeof(unsigned), cmpfunc); } double square(double x) { return x * x; } /* append to a vector of unsigned */ void appendu(unsigned x, unsigned** array, unsigned length, unsigned* flag) { *flag = 1; for(unsigned i = 0; i