DEFINITION  
    output = "pix/piston";
    xleft = 0; xright = 323;
    ylow  = 0; yhigh  = 242;
    up = yaxis;

    startframe =  1.0;
    endframe   = 90.0;
    byframe    =  1.0;

                                   /* basic model definition */
	triple	CameraView( 0.0, 1.8,  0.0);
    scalar piston_length( 2.0 );
    scalar piston_radius( 0.3 );
    scalar rod_length( 2.5 );
    scalar rod_radius( 0.1 );
    scalar wheel_thickness( 0.3 );
    scalar wheel_radius( 1.3 );
    scalar crank_offset( 1.0 );

                    /* working variables */
    scalar x1(0.0);
    scalar y1(0.0); /* position of crank end of rod */
    scalar z1(0.0);

    scalar x2(0.0);
    scalar y2(0.0); /* position of piston end of rod */
    scalar z2(0.0);

                    /* angle of wheel (crank) */
    scalar Theta(0);                                        /* absolute    */
    scalar dTheta( (360.0 / (endframe - startframe + 1)) ); /* incremental */

    scalar Phi(0);  /* deviation of rod from plumb */

    shader Red     ( model = lambert, color = (255.0,   0.0,   0.0),
                     incandescence = (255, 0, 0) );
    shader Green   ( model = lambert, color = (  0.0, 255.0,   0.0),
                     incandescence = (0, 255, 0) );

    patch cylinder( shader = ( Red ),
                    subdivide = uniform, divisions = (3.0, 3.0),
                    udegree = 3, vdegree = 3,
                    doublesided = TRUE,
                    casts_shadow = TRUE,
                    active = TRUE,
                    uknots = ( 0.0,  0.0,  0.0,  1.0,  1.0,  1.0      ),
                    vknots = ( 0.0,  1.0,  2.0,  3.0,  4.0,
                               5.0,  6.0,  7.0,  8.0,  9.0,
                              10.0, 11.0, 12.0      ),
                    cvs = ( (cv((  0.3918057, -0.3918058,  0.5      ),  1.0 ),
                             cv((  0.554097 ,  0.0      ,  0.5      ),  1.0 ),
                             cv((  0.3918057,  0.3918056,  0.5      ),  1.0 ),
                             cv((  0.0      ,  0.5540967,  0.5      ),  1.0 ),
                             cv(( -0.3918056,  0.3918054,  0.5      ),  1.0 ),
                             cv(( -0.5540966,  0.0      ,  0.5      ),  1.0 ),
                             cv(( -0.3918053, -0.3918054,  0.5      ),  1.0 ),
                             cv((  0.0      , -0.5540963,  0.5      ),  1.0 ),
                             cv((  0.3918054, -0.391805 ,  0.5      ),  1.0 ),
                             cv((  0.5540962,  0.0      ,  0.5      ),  1.0 ),
                             cv((  0.3918048,  0.3918052,  0.5      ),  1.0 )),
                            (cv((  0.3918057, -0.3918058,  0.1666667),  1.0 ),
                             cv((  0.554097 ,  0.0      ,  0.1666667),  1.0 ),
                             cv((  0.3918057,  0.3918056,  0.1666667),  1.0 ),
                             cv((  0.0      ,  0.5540967,  0.1666667),  1.0 ),
                             cv(( -0.3918056,  0.3918054,  0.1666667),  1.0 ),
                             cv(( -0.5540966,  0.0      ,  0.1666667),  1.0 ),
                             cv(( -0.3918053, -0.3918054,  0.1666667),  1.0 ),
                             cv((  0.0      , -0.5540963,  0.1666667),  1.0 ),
                             cv((  0.3918054, -0.391805 ,  0.1666667),  1.0 ),
                             cv((  0.5540962,  0.0      ,  0.1666667),  1.0 ),
                             cv((  0.3918048,  0.3918052,  0.1666667),  1.0 )),
                            (cv((  0.3918057, -0.3918058, -0.1666667),  1.0 ),
                             cv((  0.554097 ,  0.0      , -0.1666667),  1.0 ),
                             cv((  0.3918057,  0.3918056, -0.1666667),  1.0 ),
                             cv((  0.0      ,  0.5540967, -0.1666667),  1.0 ),
                             cv(( -0.3918056,  0.3918054, -0.1666667),  1.0 ),
                             cv(( -0.5540966,  0.0      , -0.1666667),  1.0 ),
                             cv(( -0.3918053, -0.3918054, -0.1666667),  1.0 ),
                             cv((  0.0      , -0.5540963, -0.1666667),  1.0 ),
                             cv((  0.3918054, -0.391805 , -0.1666667),  1.0 ),
                             cv((  0.5540962,  0.0      , -0.1666667),  1.0 ),
                             cv((  0.3918048,  0.3918052, -0.1666667),  1.0 )),
                            (cv((  0.3918057, -0.3918058, -0.5      ),  1.0 ),
                             cv((  0.554097 ,  0.0      , -0.5      ),  1.0 ),
                             cv((  0.3918057,  0.3918056, -0.5      ),  1.0 ),
                             cv((  0.0      ,  0.5540967, -0.5      ),  1.0 ),
                             cv(( -0.3918056,  0.3918054, -0.5      ),  1.0 ),
                             cv(( -0.5540966,  0.0      , -0.5      ),  1.0 ),
                             cv(( -0.3918053, -0.3918054, -0.5      ),  1.0 ),
                             cv((  0.0      , -0.5540963, -0.5      ),  1.0 ),
                             cv((  0.3918054, -0.391805 , -0.5      ),  1.0 ),
                             cv((  0.5540962,  0.0      , -0.5      ),  1.0 ),
                             cv((  0.3918048,  0.3918052, -0.5      ),  1.0 ))
                          )
                  );

    patch cap( shader = ( Green ),
               subdivide = uniform, divisions = (3.000000, 3.000000),
               udegree = 3, vdegree = 3,
               doublesided = TRUE,
               casts_shadow = TRUE,
               active = TRUE,
               uknots = ( 0.0,  0.0,  0.0,  1.0,  1.0,   1.0      ),
               vknots = ( 0.0,  1.0,  2.0,  3.0,  4.0,
                          5.0,  6.0,  7.0,  8.0,  9.0,
                         10.0, 11.0, 12.0      ),
               cvs = ( (cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.0      ,  -0.5),   1.0  ) ),
                       (cv((  0.1306019,   0.130602 ,  -0.5),   1.0  ),
                        cv((  0.184699 ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.1306019,  -0.1306018,  -0.5),   1.0  ),
                        cv((  0.0      ,  -0.1846989,  -0.5),   1.0  ),
                        cv(( -0.1306018,  -0.1306018,  -0.5),   1.0  ),
                        cv(( -0.1846989,   0.0      ,  -0.5),   1.0  ),
                        cv(( -0.1306018,   0.1306018,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.1846988,  -0.5),   1.0  ),
                        cv((  0.1306018,   0.1306017,  -0.5),   1.0  ),
                        cv((  0.1846987,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.1306016,  -0.1306017,  -0.5),   1.0  ) ),
                       (cv((  0.2612038,   0.2612039,  -0.5),   1.0  ),
                        cv((  0.369398 ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.2612038,  -0.2612037,  -0.5),   1.0  ),
                        cv((  0.0      ,  -0.3693978,  -0.5),   1.0  ),
                        cv(( -0.2612037,  -0.2612036,  -0.5),   1.0  ),
                        cv(( -0.3693977,   0.0      ,  -0.5),   1.0  ),
                        cv(( -0.2612035,   0.2612036,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.3693975,  -0.5),   1.0  ),
                        cv((  0.2612036,   0.2612033,  -0.5),   1.0  ),
                        cv((  0.3693974,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.2612033,  -0.2612035,  -0.5),   1.0  ) ),
                       (cv((  0.3918057,   0.3918059,  -0.5),   1.0  ),
                        cv((  0.554097 ,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.3918057,  -0.3918055,  -0.5),   1.0  ),
                        cv((  0.0      ,  -0.5540967,  -0.5),   1.0  ),
                        cv(( -0.3918055,  -0.3918054,  -0.5),   1.0  ),
                        cv(( -0.5540966,   0.0      ,  -0.5),   1.0  ),
                        cv(( -0.3918053,   0.3918054,  -0.5),   1.0  ),
                        cv((  0.0      ,   0.5540963,  -0.5),   1.0  ),
                        cv((  0.3918054,   0.391805 ,  -0.5),   1.0  ),
                        cv((  0.5540962,   0.0      ,  -0.5),   1.0  ),
                        cv((  0.3918049,  -0.3918052,  -0.5),   1.0  ) )
                     )
             );


MODEL

Theta = (dTheta * frame);
x1    = (-crank_offset * sind( Theta ));
y1    = ( crank_offset * cosd( Theta ));
z1    = 0.0;
x2    = 0.0;
y2    = (y1 + sqrt( ((rod_length * rod_length) - (x1 * x1)) ));
z2    = 0.0;
Phi   = asind( ((x1 - x2) / rod_length) );

{  /******************************************  WHEEL   ********************/
    translate (0.0,  0.0, -rod_radius);
    rotate    ( zaxis, Theta);
    scale     ( (2.0*wheel_radius), (2.0*wheel_radius), wheel_thickness );
    instance cylinder();
    instance cap();
    translate (0.0, 0.0, 1.0);
    instance cap();
}
{  /******************************************   ROD    ********************/
    translate (x1, y1, z1);
    rotate    ( zaxis, Phi);
    scale     ((2.0 * rod_radius), rod_length, (2.0 * rod_radius) );
    translate (0.0, 0.5, 0.0);
    rotate    ( xaxis, 90.0);
    instance cylinder();
    instance cap();
    translate (0.0, 0.0, 1.0);
    instance cap();
}
{  /******************************************  PISTON  ********************/
    translate( x2, y2, z2 );
    scale     ((2.0* piston_radius), piston_length, (2.0* piston_radius));
    translate (0.0, 0.5, 0.0);
    rotate    ( xaxis, 90.0);
    instance cylinder();
    instance cap();
    translate (0.0, 0.0, 1.0);
    instance cap();
}
{  /******************************************  CAMERA  ********************/
    camera( eye      = ( 0.0, 1.8, 14.0),
            view     = CameraView,
            viewport = (xleft, xright, ylow, yhigh),
            aspect   = ((xright-xleft)+1)/((yhigh-ylow)+1)
          );
}
