/* File: treeplot.cpp 
* Plot Christmas tree */
#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


int main() {
    int i, j, num=0,linewidth, linetype;
    double r = 0.75;
    double x0, t[CNUM], xcircle[CNUM], ycircle[CNUM], yll[NUM],
           ylr[NUM],xll[NUM],xlr[NUM];
    double x_r[NUM], x_l[NUM], lt_branch[NUM], rt_branch[NUM], 
           lb_branch[NUM], rb_branch[NUM];

    /* 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;
    
    x_r[0] = x0;
    x_l[1] = x0;
    
    /* plot tree Lines */
    CPlot plot;
    for (i = 0 ; i < ELE ; i++) {
        /* values of x for tree branches */
        x_r[1] = xf[i];
        x_l[0] = -xf[i];
        /* values of y for tree branches */
        rb_branch[0] = y01[i];
        rb_branch[1] = yf[i];
        lb_branch[0] = yf[i];
        lb_branch[1] = y01[i];
        rt_branch[0] = y02[i];
        rt_branch[1] = yf[i];
        lt_branch[0] = yf[i];
        lt_branch[1] = y02[i];

        plot.data2DCurve(x_r, rb_branch, NUM);   //right bottom
        plot.data2DCurve(x_l, lb_branch, NUM);   //left bottom
        plot.data2DCurve(x_r, rt_branch, NUM);   //right top
        plot.data2DCurve(x_l, lt_branch, NUM);   //left top
        
    }
    for (i=0 ; i<(ELE*4);i++){
        plot.lineType(i, linetype,linewidth, "green");
        num++;
    }

    /* plot tree ornaments lines left side */
    for (i=0; i<ELE-1 ; i++) {
        if (i ==1 || i==3 || i==5 || i==6){
            continue;
        }
         xll[0] = -xf[i];
         xll[1] = -xf[i];
         yll[0] = yf[i];
         yll[1] = yf[i] - r;
         plot.data2DCurve(xll,yll,NUM);
         plot.lineType(num, linetype,1, "black");
         num++;
    }
    /* plot tree ornaments lines right side */ 
    for (i=0; i<ELE-1 ; i++) {
        if (i ==0 || i==3 || i==5 || i==7){
            continue;
        }
         xlr[0] = xf[i];
         xlr[1] = xf[i];
         ylr[0] = yf[i];
         ylr[1] = yf[i] - r;
         plot.data2DCurve(xlr,ylr,NUM);
         plot.lineType(num, linetype,1, "black");
         num++;
    }
    /* plot tree ornaments */  
    lindata(0, 2*M_PI,t);
    linewidth = 2;
    for (i = 0; i < CIRC; i++) {
        for (j = 0; j < CNUM; j++){
            xcircle[j] = xc[i] + r * cos(t[j]);
            ycircle[j] = yc[i] + r * sin(t[j]);
        }
        plot.data2DCurve(xcircle,ycircle, CNUM);
        linetype = i;
        plot.lineType(num,linetype, linewidth);
        num++;
    }
    
    /* plot tree stump */
    plot.data2DCurve(xs, stump, 5);
    linetype = 1;
    linewidth = 6;
    plot.lineType(num, linetype, linewidth, "brown");
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, num, "closed");
    num++;
      
    /* plot star */  
    plot.label(PLOT_AXIS_XY, NULL);
    plot.data2DCurve(xstar, star, 11);
    linewidth = 1;
    plot.lineType(num, linetype, linewidth, "gold");
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, num, "closed");
    plot.sizeRatio(1);
    plot.axis(PLOT_AXIS_Y, PLOT_OFF);
    plot.plotting();

    /* plot another one without labels, border, tics, and axes */
    plot.label(PLOT_AXIS_XY, NULL);
    plot.border(PLOT_BORDER_ALL, PLOT_OFF);
    plot.tics(PLOT_AXIS_XY, PLOT_OFF);
    plot.axis(PLOT_AXIS_XY, PLOT_OFF);
    plot.plotting();
    return 0;
}
