public void SliceQuery(cpShape shape, float t, cpVect n, SliceContext context) { cpVect a = context.a; cpVect b = context.b; // Check that the slice was complete by checking that the endpoints aren't in the sliced shape. cpPointQueryInfo inf1 = null; cpPointQueryInfo inf2 = null; if (shape.PointQuery(a, ref inf1) > 0 && shape.PointQuery(b, ref inf2) > 0) { // Can't modify the space during a query. // Must make a post-step callback to do the actual slicing. context.space.AddPostStepCallback( (s, o1, o2) => SliceShapePostStep(s, (cpShape)o1, (SliceContext)o2), shape, context); } }
int ClipCell(cpShape shape, cpVect center, int i, int j, WorleyContex context, cpVect[] verts, cpVect[] clipped, int count) { cpVect other = WorleyPoint(i, j, ref context); // printf(" other %dx%d: (% 5.2f, % 5.2f) ", i, j, other.x, other.y); cpPointQueryInfo queryInfo = null; if (shape.PointQuery(other, ref queryInfo) > 0.0f) { for (int x = 0; x < count; x++) { clipped[x] = new cpVect(verts[x]); } return(count); } else { // printf("clipped\n"); } cpVect n = cpVect.cpvsub(other, center); float dist = cpVect.cpvdot(n, cpVect.cpvlerp(center, other, 0.5f)); int clipped_count = 0; for (j = 0, i = count - 1; j < count; i = j, j++) { cpVect a = verts[i]; float a_dist = cpVect.cpvdot(a, n) - dist; if (a_dist <= 0.0f) { clipped[clipped_count] = a; clipped_count++; } cpVect b = verts[j]; float b_dist = cpVect.cpvdot(b, n) - dist; if (a_dist * b_dist < 0.0f) { float t = cp.cpfabs(a_dist) / (cp.cpfabs(a_dist) + cp.cpfabs(b_dist)); clipped[clipped_count] = cpVect.cpvlerp(a, b, t); clipped_count++; } } return(clipped_count); }