int ClipToPlane(polygon_t *pin, plane_t *pplane, polygon_t *pout)

{

    int     i, j, nextvert, curin, nextin;

    double  curdot, nextdot, scale;

    point_t *pinvert, *poutvert;



    pinvert = pin->verts;

    poutvert = pout->verts;



    curdot = DotProduct(pinvert, &pplane->normal);

    curin = (curdot >= pplane->distance);



    for (i=0 ; i<pin->numverts ; i++)

    {

        nextvert = (i + 1) % pin->numverts;



        // Keep the current vertex if it's inside the plane

        if (curin)

            *poutvert++ = *pinvert;



        nextdot = DotProduct(&pin->verts[nextvert], &pplane->normal);

        nextin = (nextdot >= pplane->distance);



        // Add a clipped vertex if one end of the current edge is

        // inside the plane and the other is outside

        if (curin != nextin)

        {

            scale = (pplane->distance - curdot) /

                    (nextdot - curdot);

            for (j=0 ; j<3 ; j++)

            {

                poutvert->v[j] = pinvert->v[j] +

                    ((pin->verts[nextvert].v[j] - pinvert->v[j]) *

                     scale);

            }

            poutvert++;

        }



        curdot = nextdot;

        curin = nextin;

        pinvert++;

    }



    pout->numverts = poutvert - pout->verts;

    if (pout->numverts < 3)

        return 0;



    pout->color = pin->color;

    return 1;

}

