/* File: treeanim.c 
* Animate Christmas tree 
* The output is for animation of Christmas tree.
* (1) Run this program in Ch as follows:
*      treeanim.c | qanimate
*                or
*      treeanim.c > tmp1.qnm
*      qanimate tmp1.qnm
* (2) Click "Go" to view the animation
* (3) Click "Slow" to slow down flashing
**********************************************************************/
#include <stdio.h>
#include <chplot.h>
#include <math.h>
#define NUM   2       /* number of points */
#define ELE   9       /* number of elements */
#define YDEL  0.3243  /* the height differences for each branch */
#define CNUM  20
#define CIRC  8
#define LINE  36

int main() {
    int i, j, num = 0, linewidth, linetype;
    double height = 2.0, width = 8.0, length = .75;
    char *color[] = {"yellow", "red", "green", "blue", "orange", "violet", "purple", "red", NULL} ;
    double r = 0.75, x0;
    int t, t0, tf;          // time
    
    
    /* Coordinate location for tree */
    x0 = 0.0;       //initial x
    double y01[ELE] = {8., 12., 16., 20., 24., 28., 32., 36., 40.};
    double y02[ELE] = {12., 16., 20., 24., 28., 32., 36., 40., 44.};
    double yf[ELE] = {5., 9.3243, 13.6486, 17.9730, 22.2973, 26.6216, 
                      30.9459, 35.2703, 39.5946};
    double xf[ELE] = {20., 17.8378, 15.6757, 13.5137, 11.3514, 9.1892, 
                      7.0270, 4.8649, 2.7027};

    /* coordinates lacation for tree stump */
    double stump[] = {8., 8., 0., 0., 8.0};
    double xs[] = { -1., 1., 1., -1., -1};

    /* coordinates for star */
    double star[] = {44., 43.1, 44.3405, 44.9721, 44.9721, 46.1, 44.9721, 
                     44.9721, 44.3405, 43.1, 44.};
    double xstar[] = {0, -1.5, -0.8077, -1.5, -.6, 0, .6, 1.5, 0.8077, 1.5, 0};

    /* coordinates for the center of tree ornaments */
    double xc[] = {-xf[0],xf[1],-xf[2],xf[2], -xf[4],xf[4],xf[6],-xf[7]};
    double yc[] = {3.5, 7.8243, 12.1486,12.1486,20.7973,20.7973, 
                   29.4459, 33.7703 };

    /* set tree line and color */
    linewidth = 4;
    linetype = 1;
    
    /* A comment line starting with # */
    printf("# qanimate data for animation of christmas tree\n");
    /* The title displayed on the animation */
    printf("title \"Christmas Tree Animation\"\n");
    printf("fixture\n");
    /* The primitives following fixture */
    /* Plot tree branches*/
    for ( i = 0 ;  i < ELE; i++){
        printf("line %f %f %f %f pen green linewidth %d\n", x0, y01[i], xf[i], yf[i], linewidth );
        printf("line %f %f %f %f pen green linewidth %d\n", x0, y02[i], xf[i], yf[i], linewidth );
        printf("line %f %f %f %f pen green linewidth %d\n", x0, y01[i], -xf[i], yf[i], linewidth );
        printf("line %f %f %f %f pen green linewidth %d\n", x0, y02[i], -xf[i], yf[i], linewidth );
    }
    
    /* plot tree stump */
    printf("rectangle %f %f %f %f pen brown fill brown\n", xs[0], x0, height, width);
    
    /* plot tree star */
    for ( i = 0 ; i< 10; i ++){
        printf("line %f %f %f %f pen yellow linewidth %d\n", xstar[i], star[i], xstar[i+1], star[i+1], linewidth ); 
    }
    /* plot blank circle */
    for ( i = 0 ; i < CIRC ; i++){
        printf("line %f %f %f %f pen black\n", xc[i], (yc[i]+length+r), xc[i], yc[i]+r);
        printf("circle %f %f %f\n", xc[i], yc[i], r);
    }
    
    printf("animate restart\n");
    t0 = 0;
    tf = 5;
    /* light circle */
    for(t = t0; t<tf; t += 1) {
        for ( i = 0 ; i < CIRC ; i++){
           if (t%2) {
                printf("circle %f %f %f fill %s\n", xc[i], yc[i], r, color[i]);
            }
       }
    }
    return 0;
}
