File: findquads.c

package info (click to toggle)
grass 6.0.2-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 40,044 kB
  • ctags: 31,303
  • sloc: ansic: 321,125; tcl: 25,676; sh: 11,176; cpp: 10,098; makefile: 5,025; fortran: 1,846; yacc: 493; lex: 462; perl: 133; sed: 1
file content (88 lines) | stat: -rw-r--r-- 2,099 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (C) 1994-1995. James Darrell McCauley. (darrell@mccauley-usa.com)
 * 	                          http://mccauley-usa.com/
 *
 * This program is free software under the GPL (>=v2)
 * Read the file GPL.TXT coming with GRASS for details.
 */

#include <sys/types.h> /* for getpid() */
#include <unistd.h>
#include <stdlib.h> 
#include <math.h>
#include "gis.h"
#include "Vect.h"
#include "quaddefs.h"

#ifndef RAND_MAX 
#define RAND_MAX (pow(2.0,31.0)-1) 
#endif

#define R_INIT srand
#define RANDOM(lo,hi) ((double) rand() / (double)RAND_MAX * (hi-lo) + lo)

/*
#define R_MAX 1.0
#define R_INIT srand48
#define RANDOM(lo,hi) (drand48()/R_MAX*(hi-lo)+lo)
*/

/*
 * returns Z struct filled with centers of n non-overlapping circles of
 * radius r contained completely within window
 */
COOR *find_quadrats (int n, double r, struct Cell_head window, int verbose)
{
  int i = 1, j, overlapped;
  unsigned k;
  double east, north, e_max, e_min, n_max, n_min;
  COOR *quads=NULL;

  quads = (COOR *) G_malloc ( n * sizeof(COOR) );
  if (quads == NULL)
    G_fatal_error ("cannot allocate memory for quadrats");

   srand ((unsigned int)getpid()); 
  /* R_INIT (1); */

  e_max = window.east - r;
  e_min = window.west + r;
  n_max = window.north - r;
  n_min = window.south + r;

  quads[0].x = RANDOM (e_min, e_max);
  quads[0].y = RANDOM (n_min, n_max);

  while (i < n)
  {
    k=0;
    if (verbose)
      G_percent (i, n, 1);
    overlapped = 1;
    while (overlapped)
    {
      east = RANDOM (e_min, e_max);
      north = RANDOM (n_min, n_max);
      k++;
      overlapped = 0;
      for (j = i; j >= 0; --j)
      {
	if (hypot (quads[j].x - east, quads[j].y - north) < 2 * r)
	{
	  overlapped = 1;
	  j = -1;
	}
      }
      if (k==n*n) 
        G_warning("Having difficulties fitting that many circles with that radius");
      if (k==2*n*n) 
        G_fatal_error("Maximum number of iterations exceeded\nTry smaller radius or smaller number of quads");
    }
    if (verbose)
      G_percent (i, n, 1);
    quads[i].x = east;
    quads[i].y = north;
    i++;
  }
  return quads;
}