void TestPolygon() { Point point[7] = { { 100, 100 }, { 100, 600 }, { 200, 600 }, //{ 200, 350 }, { 500, 350 }, { 500, 600 }, { 600, 600 }, { 600, 100 }, }; Polygon* polygon = CreatePolygon(7, point); PrintPolygon(polygon); /* for (int i = 0; i < 50; ++i) { Point point = { (rand() % (800 * 10000) / 10000.f), (rand() % (640 * 10000) / 10000.f) }; printf("circle(%f, %f, %f, \"%s\");\n", point.x, point.y, 5.f, PointInPolygon(&polygon, &point) ? "red" : "yellow"); }*/ Circle circle[200] = { 0 }; for (int i = 0; i < 200; ++i) { float x = (rand() % (800 * 10000) / 10000.f); float y = (rand() % (640 * 10000) / 10000.f); float r = 20.f; circle[i].c.x = x; circle[i].c.y = y; circle[i].r = r; } for (int i = 0; i < 200; ++i) { PrintCircle(circle[i], IsCircleIntersectPolygon(&circle[i], polygon)); } DeletePolygon(&polygon); }
// takes a polygon as input an returns a list of face fragments and the leafs that they belong to static leafface_t *PushFaceIntoTree(bspnode_t *n, polygon_t *p) { bspnode_t *nstack[256]; polygon_t *pstack[256]; int sp = 0; nstack[sp] = n; pstack[sp] = p; sp++; printf("processing face...\n"); while (sp) { sp--; n = nstack[sp]; p = pstack[sp]; printf("n=%p, child0=%p, child1=%p\n", n, n->children[0], n->children[1]); if (!n->children[0] && !n->children[1]) { // only emit polygons into empty leafs if (!n->empty) continue; printf("face in leaf %p\n", n); PrintPolygon(p); continue; } int side = Polygon_OnPlaneSide(p, n->plane, CLIP_EPSILON); if (side == PLANE_SIDE_FRONT) { nstack[sp] = n->children[0]; pstack[sp] = p; sp++; } else if (side == PLANE_SIDE_BACK) { nstack[sp] = n->children[1]; pstack[sp] = p; sp++; } else if (side == PLANE_SIDE_ON) { float dot = Dot(n->plane.GetNormal(), Polygon_Normal(p)); // map 0 to the front child and 1 to the back child int facing = (dot > 0.0f ? 0 : 1); nstack[sp] = n->children[facing]; pstack[sp] = p; sp++; } else if (side == PLANE_SIDE_CROSS) { polygon_t *f, *b; Polygon_SplitWithPlane(p, n->plane, CLIP_EPSILON, &f, &b); nstack[sp] = n->children[0]; pstack[sp] = f; sp++; nstack[sp] = n->children[1]; pstack[sp] = b; sp++; } } return NULL; }