41 #define EPSILON 1.0e-8
49 void nn_quit(
const char* format, ... );
60 fprintf( stderr,
"error: nn: " );
61 va_start( args, format );
62 vfprintf( stderr, format, args );
70 double x1sq = p1->
x * p1->
x;
71 double x2sq = p2->
x * p2->
x;
72 double x3sq = p3->
x * p3->
x;
73 double y1sq = p1->
y * p1->
y;
74 double y2sq = p2->
y * p2->
y;
75 double y3sq = p3->
y * p3->
y;
76 double t1 = x3sq - x2sq + y3sq - y2sq;
77 double t2 = x1sq - x3sq + y1sq - y3sq;
78 double t3 = x2sq - x1sq + y2sq - y1sq;
79 double D = ( p1->
x * ( p2->
y - p3->
y ) + p2->
x * ( p3->
y - p1->
y ) + p3->
x * ( p1->
y - p2->
y ) ) * 2.0;
84 c->
x = ( p1->
y * t1 + p2->
y * t2 + p3->
y * t3 ) / D;
85 c->
y = -( p1->
x * t1 + p2->
x * t2 + p3->
x * t3 ) / D;
86 c->
r = hypot( c->
x - p1->
x, c->
y - p1->
y );
100 return hypot( c->
x - p->
x, c->
y - p->
y ) <= c->
r * ( 1.0 +
EPSILON );
117 point * points = *ppoints;
118 double xmin = DBL_MAX;
119 double xmax = -DBL_MAX;
120 double ymin = DBL_MAX;
121 double ymax = -DBL_MAX;
123 double * sumx = calloc( (
size_t) nxy,
sizeof (
double ) );
124 double * sumy = calloc( (
size_t) nxy,
sizeof (
double ) );
125 double * sumz = calloc( (
size_t) nxy,
sizeof (
double ) );
126 int * count = calloc( (
size_t) nxy,
sizeof (
int ) );
130 point * pointsnew = NULL;
134 fprintf( stderr,
"thinned: %d points -> ", *pn );
136 if ( nx < 1 || ny < 1 )
142 fprintf( stderr,
"0 points" );
150 for ( ii = 0; ii < n; ++ii )
152 point* p = &points[ii];
164 stepx = ( nx > 1 ) ? ( xmax - xmin ) / nx : 0.0;
165 stepy = ( ny > 1 ) ? ( ymax - ymin ) / ny : 0.0;
167 for ( ii = 0; ii < n; ++ii )
169 point* p = &points[ii];
177 i = ( nx == 1 ) ? 0 : (
int) ( ( p->
x - xmin ) / stepx );
178 j = ( ny == 1 ) ? 0 : (
int) ( ( p->
y - ymin ) / stepy );
191 for ( j = 0; j < ny; ++j )
193 for ( i = 0; i < nx; ++i )
195 int index = i + j * nx;
197 if ( count[index] > 0 )
202 pointsnew = malloc( (
size_t) nnew *
sizeof (
point ) );
205 for ( j = 0; j < ny; ++j )
207 for ( i = 0; i < nx; ++i )
209 int index = i + j * nx;
210 int nn = count[index];
214 point* p = &pointsnew[ii];
216 p->
x = sumx[index] / nn;
217 p->
y = sumy[index] / nn;
218 p->
z = sumz[index] / nn;
225 fprintf( stderr,
"%d points\n", nnew );
233 *ppoints = pointsnew;
251 double xmin = DBL_MAX;
252 double xmax = -DBL_MAX;
253 double ymin = DBL_MAX;
254 double ymax = -DBL_MAX;
259 if ( nx < 1 || ny < 1 )
266 for ( ii = 0; ii < nin; ++ii )
280 if (
isnan( zoom ) || zoom <= 0.0 )
285 double xdiff2 = ( xmax - xmin ) / 2.0;
286 double ydiff2 = ( ymax - ymin ) / 2.0;
287 double xav = ( xmax + xmin ) / 2.0;
288 double yav = ( ymax + ymin ) / 2.0;
290 xmin = xav - xdiff2 * zoom;
291 xmax = xav + xdiff2 * zoom;
292 ymin = yav - ydiff2 * zoom;
293 ymax = yav + ydiff2 * zoom;
297 *pout = malloc( (
size_t) ( *nout ) *
sizeof (
point ) );
299 stepx = ( nx > 1 ) ? ( xmax - xmin ) / ( nx - 1 ) : 0.0;
300 stepy = ( ny > 1 ) ? ( ymax - ymin ) / ( ny - 1 ) : 0.0;
301 x0 = ( nx > 1 ) ? xmin : ( xmin + xmax ) / 2.0;
302 yy = ( ny > 1 ) ? ymin : ( ymin + ymax ) / 2.0;
305 for ( j = 0; j < ny; ++j )
308 for ( i = 0; i < nx; ++i )
310 point* p = &( *pout )[ii];
334 void points_generate2(
double xmin,
double xmax,
double ymin,
double ymax,
int nx,
int ny,
int* nout,
point** pout )
340 if ( nx < 1 || ny < 1 )
348 *pout = malloc( (
size_t) ( *nout ) *
sizeof (
point ) );
350 stepx = ( nx > 1 ) ? ( xmax - xmin ) / ( nx - 1 ) : 0.0;
351 stepy = ( ny > 1 ) ? ( ymax - ymin ) / ( ny - 1 ) : 0.0;
352 x0 = ( nx > 1 ) ? xmin : ( xmin + xmax ) / 2.0;
353 yy = ( ny > 1 ) ? ymin : ( ymin + ymax ) / 2.0;
356 for ( j = 0; j < ny; ++j )
359 for ( i = 0; i < nx; ++i )
361 point* p = &( *pout )[ii];
382 *value = strtod( token, &end );
393 #define NALLOCATED_START 1024
407 char seps[] =
" ,;\t";
410 if ( dim < 2 || dim > 3 )
421 if ( strcmp( fname,
"stdin" ) == 0 || strcmp( fname,
"-" ) == 0 )
425 f = fopen( fname,
"r" );
427 nn_quit(
"%s: %s\n", fname, strerror( errno ) );
431 *points = malloc( (
size_t) nallocated *
sizeof (
point ) );
433 while ( fgets( buf,
BUFSIZE, f ) != NULL )
437 if ( *n == nallocated )
440 *points = realloc( *points, (
size_t) nallocated *
sizeof (
point ) );
443 p = &( *points )[*n];
447 if ( ( token = strtok( buf, seps ) ) == NULL )
451 if ( ( token = strtok( NULL, seps ) ) == NULL )
459 if ( ( token = strtok( NULL, seps ) ) == NULL )
473 *points = realloc( *points, (
size_t) ( *n ) *
sizeof (
point ) );
476 if ( fclose( f ) != 0 )
477 nn_quit(
"%s: %s\n", fname, strerror( errno ) );
489 double xmin, ymin, xmax, ymax;
496 xmin = xmax = points[0].
x;
497 ymin = ymax = points[0].
y;
499 for ( i = 1; i < n; ++i )
501 point* p = &points[i];
505 else if ( p->
x > xmax )
509 else if ( p->
y > ymax )
513 if ( xmin == xmax || ymin == ymax )
516 k = ( ymax - ymin ) / ( xmax - xmin );
518 for ( i = 0; i < n; ++i )
534 for ( i = 0; i < n; ++i )
void points_read(char *fname, int dim, int *n, point **points)
double points_scaletosquare(int n, point *points)
int circle_contains(circle *c, point *p)
int circle_build(circle *c, point *p1, point *p2, point *p3)
static int str2double(char *token, double *value)
void points_generate2(double xmin, double xmax, double ymin, double ymax, int nx, int ny, int *nout, point **pout)
void nn_quit(const char *format,...)
static PLFLT value(double n1, double n2, double hue)
void points_scale(int n, point *points, double k)
void points_generate1(int nin, point pin[], int nx, int ny, double zoom, int *nout, point **pout)
void points_thin(int *pn, point **ppoints, int nx, int ny)