139 double dst_p1, dst_p2;
145 for (i = 1; i < n; i++)
148 if (!objs[i])
continue;
151 if (!objs[p1] && !objs[p2])
161 if ((dst_p1 > max_dst) || (dst_p2 > max_dst))
163 max_dst = fmax(dst_p1, dst_p2);
169 if ((dst_p1 == 0) || (dst_p2 == 0)) duplicate_count++;
171 if (duplicate_count > 1)
173 "%s: there are at least %u duplicate inputs, number of output clusters may be less than you requested",
178 assert(p1 != p2 && objs[p1] && objs[p2] && max_dst >= 0);
181 centers_raw[0] = *((
POINT2D *)objs[p1]);
182 centers[0] = &(centers_raw[0]);
183 centers_raw[1] = *((
POINT2D *)objs[p2]);
184 centers[1] = &(centers_raw[1]);
189 distances =
lwalloc(
sizeof(
double) * n);
192 for (j = 0; j < n; j++)
203 for (i = 2; i < k; i++)
206 double max_distance = -DBL_MAX;
209 for (j = 0; j < n; j++)
212 if (distances[j] < 0)
continue;
218 if (distances[j] > max_distance)
220 candidate_center = j;
221 max_distance = distances[j];
226 assert(max_distance >= 0);
229 distances[candidate_center] = -1;
232 centers_raw[i] = *((
POINT2D *)objs[candidate_center]);
233 centers[i] = &(centers_raw[i]);
double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)
void * lwalloc(size_t size)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.