132 uint32_t p1 = 0, p2 = 0;
134 uint32_t duplicate_count = 1;
135 double max_dst = -1, current_distance;
136 double dst_p1, dst_p2;
142 for (i = 1; i < n; i++)
145 if (!objs[i])
continue;
148 if (!objs[p1] && !objs[p2])
158 if ((dst_p1 > max_dst) || (dst_p2 > max_dst))
171 if ((dst_p1 == 0) || (dst_p2 == 0)) duplicate_count++;
173 if (duplicate_count > 1)
175 "%s: there are at least %u duplicate inputs, number of output clusters may be less than you requested",
180 assert(p1 != p2 && objs[p1] && objs[p2] && max_dst >= 0);
183 centers_raw[0] = *((
POINT2D *)objs[p1]);
184 centers[0] = &(centers_raw[0]);
185 centers_raw[1] = *((
POINT2D *)objs[p2]);
186 centers[1] = &(centers_raw[1]);
191 distances =
lwalloc(
sizeof(
double) * n);
194 for (j = 0; j < n; j++)
205 for (i = 2; i < k; i++)
207 uint32_t candidate_center = 0;
208 double max_distance = -DBL_MAX;
211 for (j = 0; j < n; j++)
214 if (distances[j] < 0)
continue;
218 if (current_distance < distances[j])
219 distances[j] = current_distance;
222 if (distances[j] > max_distance)
224 candidate_center = j;
225 max_distance = distances[j];
230 assert(max_distance >= 0);
233 distances[candidate_center] = -1;
236 centers_raw[i] = *((
POINT2D *)objs[candidate_center]);
237 centers[i] = &(centers_raw[i]);
void * lwalloc(size_t size)
void lwnotice(const char *fmt,...)
Write a notice out to the notice handler.
static double distance2d_sqr_pt_pt(const POINT2D *p1, const POINT2D *p2)