| 1 | <pre style="text-align:left;color:#000000; background-color:#ffffff; border:solid black 1px; padding:0.5em 1em 0.5em 1em; overflow:auto;font-size:small; font-family:monospace; "><strong><span style="color:#881350;">int</span></strong> first = <span style="color:#0000ff;">0</span>; |
| 2 | |
| 3 | <em><span style="color:#236e25;">// Create space to store the convex hull. |
| 4 | // An alloca(), or a variable length array would be a better, but not always portable choice. |
| 5 | </span></em>cpVect *hullVerts = (cpVect *)<span style="color:#003369;">calloc</span>(vertCount, <strong><span style="color:#881350;">sizeof</span></strong>(cpVect)); |
| 6 | <strong><span style="color:#881350;">int</span></strong> hullCount = <span style="color:#003369;">cpConvexHull</span>(vertCount, verts, hullVerts, &first, <span style="color:#0000ff;">0.0</span>); |
| 7 | |
| 8 | <em><span style="color:#236e25;">// hullVerts[0] will be equal to verts[first] here. |
| 9 | // If you don't care, pass NULL instead of the 'first' pointer. |
| 10 | </span></em> |
| 11 | cpBody *body = <span style="color:#003369;">cpBodyNew</span>(mass, <span style="color:#003369;">cpMomentForPoly</span>(mass, hullCount, hullVerts, cpvzero)); |
| 12 | cpShape *shape = <span style="color:#003369;">cpPolyShapeNew</span>(body, hullCount, hullVerts, cpvzero); |
| 13 | |
| 14 | <span style="color:#003369;">free</span>(hullVerts); |
| 15 | |
| 16 | <em><span style="color:#236e25;">// ********* |
| 17 | // Altenatively you can use the CP_CONVEX_HULL() macro to save yourself a little work |
| 18 | </span></em> |
| 19 | <em><span style="color:#236e25;">// The macro will declare the hullCount and hullVerts variables. |
| 20 | // hullVerts is allocated on the stack and does not need to be freed. |
| 21 | </span></em><span style="color:#003369;">CP_CONVEX_HULL</span>(count, verts, hullCount, hullVerts) |
| 22 | |
| 23 | cpBody *body = <span style="color:#003369;">cpBodyNew</span>(mass, <span style="color:#003369;">cpMomentForPoly</span>(mass, hullCount, hullVerts, cpvzero)); |
| 24 | cpShape *shape = <span style="color:#003369;">cpPolyShapeNew</span>(body, hullCount, hullVerts, cpvzero); |
| 25 | </pre> |