25 lines
1.9 KiB
HTML
25 lines
1.9 KiB
HTML
<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>;
|
|
|
|
<em><span style="color:#236e25;">// Create space to store the convex hull.
|
|
// An alloca(), or a variable length array would be a better, but not always portable choice.
|
|
</span></em>cpVect *hullVerts = (cpVect *)<span style="color:#003369;">calloc</span>(vertCount, <strong><span style="color:#881350;">sizeof</span></strong>(cpVect));
|
|
<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>);
|
|
|
|
<em><span style="color:#236e25;">// hullVerts[0] will be equal to verts[first] here.
|
|
// If you don't care, pass NULL instead of the 'first' pointer.
|
|
</span></em>
|
|
cpBody *body = <span style="color:#003369;">cpBodyNew</span>(mass, <span style="color:#003369;">cpMomentForPoly</span>(mass, hullCount, hullVerts, cpvzero));
|
|
cpShape *shape = <span style="color:#003369;">cpPolyShapeNew</span>(body, hullCount, hullVerts, cpvzero);
|
|
|
|
<span style="color:#003369;">free</span>(hullVerts);
|
|
|
|
<em><span style="color:#236e25;">// *********
|
|
// Altenatively you can use the CP_CONVEX_HULL() macro to save yourself a little work
|
|
</span></em>
|
|
<em><span style="color:#236e25;">// The macro will declare the hullCount and hullVerts variables.
|
|
// hullVerts is allocated on the stack and does not need to be freed.
|
|
</span></em><span style="color:#003369;">CP_CONVEX_HULL</span>(count, verts, hullCount, hullVerts)
|
|
|
|
cpBody *body = <span style="color:#003369;">cpBodyNew</span>(mass, <span style="color:#003369;">cpMomentForPoly</span>(mass, hullCount, hullVerts, cpvzero));
|
|
cpShape *shape = <span style="color:#003369;">cpPolyShapeNew</span>(body, hullCount, hullVerts, cpvzero);
|
|
</pre> |