// find the cycles representing each polygon in the layer // this is a naive O(n^2) implementaion, it will benefit greatly from a scanline aproach void Layer::create_polys() { std::vector<line*> candidates (segments->begin(), segments->end()); while (!candidates.empty()) { line *cur_start = candidates.at(0); point *poly_start = &(cur_start->p1); point *look_for = &(cur_start->p2); // REMOVE the start poly from the candidates candidates.erase(candidates.begin()); std::vector<point*> *cur_poly = new std::vector<point*>; cur_poly->push_back(poly_start); // follow this polygon around while ( !point_compare(*look_for, *poly_start) ) { // If there are not exact match we have to find a best match int closest_idx = -42; double closest_distanceSQ = 42; int cur_idx=0; for(std::vector<line*>::iterator it = candidates.begin(); it != candidates.end(); it++) { line *l = *it; if (point_compare(l->p1, *look_for)) { // we found the other end cur_poly->push_back(look_for); // update what wer are looking look_for = &(l->p2); // never look at this again. candidates.erase(it); closest_distanceSQ = 0.0; closest_idx = cur_idx; break; } double cur_disSQ = point_distSQ(*look_for, l->p1); if (cur_disSQ < closest_distanceSQ ) { closest_idx = cur_idx; closest_distanceSQ = cur_disSQ; } cur_idx++; } if (closest_distanceSQ != 0.0) { // we did not find a match take the best.. printf("not exact match\n"); *look_for = candidates[closest_idx]->p2; candidates.erase(candidates.begin()+closest_idx); } } // there should be a cycle, insert look_for ? and poly->push_back(*cur_poly); } }
static int compare_point_to_tile(void const *key, void const *object) { struct point const *point = key; struct tile *const *tile = object; return point_compare(*point, (*tile)->point); }
static int compare_by_point(void const *object, void const *other_object) { struct tile *const *tile = object; struct tile *const *other = other_object; return point_compare((*tile)->point, (*other)->point); }
int main(int argc, char **argv) { struct point p1, *p2; point_set(&p1, 1.0, 1.0); p2 = malloc(sizeof(struct point)); assert(p2); point_set(p2, 1.0, 1.0); assert(point_distance(&p1, p2) == 0.0); point_translate(&p1, 1.0, 0.0); assert(point_distance(&p1, p2) == 1.0); point_set(&p1, 0.0, 0.0); point_set(p2, 3.0, 4.0); assert(point_distance(&p1, p2) == 5.0); assert(point_compare(&p1, p2) < 0); point_set(&p1, 1, 2); point_set(p2, 2, 1); assert(point_compare(&p1, p2) == 0); point_set(&p1, 4, 2); point_set(p2, 3, 3); assert(point_compare(&p1, p2) > 0); free(p2); p2 = NULL; printf("OK\n"); return 0; }