1 | /*** MAIN.C ***/ |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> /* realloc(), qsort() */ |
---|
5 | |
---|
6 | #include "vdefs.h" |
---|
7 | |
---|
8 | Site * readone(void), * nextone(void) ; |
---|
9 | void readsites(void) ; |
---|
10 | |
---|
11 | int sorted, triangulate, plot, debug, nsites, siteidx ; |
---|
12 | float xmin, xmax, ymin, ymax ; |
---|
13 | Site * sites ; |
---|
14 | Freelist sfl ; |
---|
15 | |
---|
16 | int |
---|
17 | main(int argc, char *argv[]) |
---|
18 | { |
---|
19 | int c ; |
---|
20 | Site *(*next)() ; |
---|
21 | |
---|
22 | sorted = triangulate = plot = debug = 0 ; |
---|
23 | while ((c = getopt(argc, argv, "dpst")) != EOF) |
---|
24 | { |
---|
25 | switch(c) |
---|
26 | { |
---|
27 | case 'd': |
---|
28 | debug = 1 ; |
---|
29 | break ; |
---|
30 | case 's': |
---|
31 | sorted = 1 ; |
---|
32 | break ; |
---|
33 | case 't': |
---|
34 | triangulate = 1 ; |
---|
35 | break ; |
---|
36 | case 'p': |
---|
37 | plot = 1 ; |
---|
38 | break ; |
---|
39 | } |
---|
40 | } |
---|
41 | |
---|
42 | freeinit(&sfl, sizeof(Site)) ; |
---|
43 | if (sorted) |
---|
44 | { |
---|
45 | scanf("%d %f %f %f %f", &nsites, &xmin, &xmax, &ymin, &ymax) ; |
---|
46 | next = readone ; |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | readsites() ; |
---|
51 | next = nextone ; |
---|
52 | } |
---|
53 | siteidx = 0 ; |
---|
54 | geominit() ; |
---|
55 | if (plot) |
---|
56 | { |
---|
57 | plotinit() ; |
---|
58 | } |
---|
59 | voronoi(next) ; |
---|
60 | return (0) ; |
---|
61 | } |
---|
62 | |
---|
63 | /*** sort sites on y, then x, coord ***/ |
---|
64 | |
---|
65 | int |
---|
66 | scomp(const void * vs1, const void * vs2) |
---|
67 | { |
---|
68 | Point * s1 = (Point *)vs1 ; |
---|
69 | Point * s2 = (Point *)vs2 ; |
---|
70 | |
---|
71 | if (s1->y < s2->y) |
---|
72 | { |
---|
73 | return (-1) ; |
---|
74 | } |
---|
75 | if (s1->y > s2->y) |
---|
76 | { |
---|
77 | return (1) ; |
---|
78 | } |
---|
79 | if (s1->x < s2->x) |
---|
80 | { |
---|
81 | return (-1) ; |
---|
82 | } |
---|
83 | if (s1->x > s2->x) |
---|
84 | { |
---|
85 | return (1) ; |
---|
86 | } |
---|
87 | return (0) ; |
---|
88 | } |
---|
89 | |
---|
90 | /*** return a single in-storage site ***/ |
---|
91 | |
---|
92 | Site * |
---|
93 | nextone(void) |
---|
94 | { |
---|
95 | Site * s ; |
---|
96 | |
---|
97 | if (siteidx < nsites) |
---|
98 | { |
---|
99 | s = &sites[siteidx++]; |
---|
100 | return (s) ; |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | return ((Site *)NULL) ; |
---|
105 | } |
---|
106 | } |
---|
107 | |
---|
108 | /*** read all sites, sort, and compute xmin, xmax, ymin, ymax ***/ |
---|
109 | |
---|
110 | void |
---|
111 | readsites(void) |
---|
112 | { |
---|
113 | int i ; |
---|
114 | |
---|
115 | nsites = 0 ; |
---|
116 | sites = (Site *) myalloc(4000 * sizeof(Site)); |
---|
117 | while (scanf("%f %f", &sites[nsites].coord.x, |
---|
118 | &sites[nsites].coord.y) !=EOF) |
---|
119 | { |
---|
120 | sites[nsites].sitenbr = nsites ; |
---|
121 | sites[nsites++].refcnt = 0 ; |
---|
122 | if (nsites % 4000 == 0) |
---|
123 | sites = (Site *) |
---|
124 | realloc(sites,(nsites+4000)*sizeof(Site)); |
---|
125 | } |
---|
126 | |
---|
127 | qsort((void *)sites, nsites, sizeof(Site), scomp) ; |
---|
128 | xmin = sites[0].coord.x ; |
---|
129 | xmax = sites[0].coord.x ; |
---|
130 | for (i = 1 ; i < nsites ; ++i) |
---|
131 | { |
---|
132 | if(sites[i].coord.x < xmin) |
---|
133 | { |
---|
134 | xmin = sites[i].coord.x ; |
---|
135 | } |
---|
136 | if (sites[i].coord.x > xmax) |
---|
137 | { |
---|
138 | xmax = sites[i].coord.x ; |
---|
139 | } |
---|
140 | } |
---|
141 | ymin = sites[0].coord.y ; |
---|
142 | ymax = sites[nsites-1].coord.y ; |
---|
143 | } |
---|
144 | |
---|
145 | /*** read one site ***/ |
---|
146 | |
---|
147 | Site * |
---|
148 | readone(void) |
---|
149 | { |
---|
150 | Site * s ; |
---|
151 | |
---|
152 | s = (Site *)getfree(&sfl) ; |
---|
153 | s->refcnt = 0 ; |
---|
154 | s->sitenbr = siteidx++ ; |
---|
155 | if (scanf("%f %f", &(s->coord.x), &(s->coord.y)) == EOF) |
---|
156 | { |
---|
157 | return ((Site *)NULL ) ; |
---|
158 | } |
---|
159 | return (s) ; |
---|
160 | } |
---|
161 | |
---|