/* File: car_gifs.cpp
   Create gif files for each snapshot. Then combine them into an animated gif fle */
#include <stdio.h>
#include <math.h>
#include <chplot.h>
#include <array.h>
#define N 361
#define N1 20

int main(){
    
    /* initial corrdinates for car */
    array double x_polygon[19] = {0, 0, 12, 12, 10, 9.866, 9.5, 9, 8.5, 8.134, 8, 3, 2.866, 2.5, 2, 1.5, 1.134, 1, 0};
    array double y_polygon[19] = {1, 3, 3, 1, 1, 1.5, 1.866, 2, 1.866, 1.5, 1, 1, 1.5, 1.866, 2, 1.866, 1.5, 1, 1};
    array double x_top_frame[13] = {1.5, 3, 7, 9, 8.5, 7, 5.2, 5.2, 4.8, 4.8, 3, 2.5, 1.5};
    array double y_top_frame[13] = {3, 5.5, 5.5, 3, 3, 5, 5, 3, 3, 5, 5, 3, 3};
    
    /* define coordinates for the wheel */
    int i, j, k, a1 = 90, a2 = 210, a3 = 330 ;
    array double x_wheel_1[N], y_wheel_1[N], x_wheel_2[N], y_wheel_2[N];
    double angle,
           x_wheel_center_1 = 2,
           x_wheel_center_2 = 9,
           angle_incr = (360-0)/(N-1)*(M_PI/180),
           r = 0.8;
    for (i = 0; i < N; i++){
        angle = angle_incr * i;
        x_wheel_1[i] = 2 + r * cos(angle);
        y_wheel_1[i] = 1 + r * sin(angle);
        x_wheel_2[i] = 9 + r * cos(angle);
        y_wheel_2[i] = 1 + r * sin(angle);
    }
    
    y_polygon = y_polygon + 3;
    y_top_frame = y_top_frame +3;
    y_wheel_1 = y_wheel_1 + 3;
    y_wheel_2 = y_wheel_2 + 3;
    double x1, x2;
    
    
    /* declare and intialize initial and final time, time increment, and velocity */
    double t0 = 0, tf = 20, t_incr = 20/N1, v = 2;
    double t, d;
    double omega = v / r;       // angular acceleration of the wheel
    int theta;
    CPlot plot[N1];
    for (i = 0; i < N1; i++){
    
        if (i == 0){
            d = 0;
            theta = 0;
        }
        else{
            d = v * t_incr;
            theta = 20/2; // angle travel in t second
        }
        //t = t_incr*i;
        //d = t * v;          // distance travel in t seconds
        x_wheel_center_1 = x_wheel_center_1 + d;   // x position of the center of the back wheel at time t
        x_wheel_center_2 = x_wheel_center_2 + d;   // x position of the center of the front wheel at time t
        x_polygon = x_polygon + d;
        x_top_frame = x_top_frame + d;
        x_wheel_1 = x_wheel_1 + d;
        x_wheel_2 = x_wheel_2 + d; 
        
        /* Turn off border and axis display */
        //plot.axis(PLOT_AXIS_XY, PLOT_OFF);
        //plot.border(PLOT_BORDER_ALL, PLOT_OFF);
        //plot.tics(PLOT_AXIS_XY, PLOT_OFF);
        /* drawing the ground */
        plot[i].axisRange(PLOT_AXIS_X, 0, 50);
        plot[i].ticsRange(PLOT_AXIS_X, 5);
        plot[i].axisRange(PLOT_AXIS_Y, -5, 20);
        plot[i].ticsRange(PLOT_AXIS_Y, 5);
        plot[i].line(0,3,0,50,3,0);
        plot[i].lineType(0,1,1,"black");
        for(j=1; j<27; j++){
            x1 = 2*(j-1);
            x2 = 2*(j-1) - 0.5;
            plot[i].line(x1,3,0,x2,2.7,0);
            plot[i].lineType(j, 1, 1, "black");
        }
        /* drawing the car */
        /* Turn off border and axis display */
        /*plot.axis(PLOT_AXIS_XY, PLOT_OFF);
        plot.border(PLOT_BORDER_ALL, PLOT_OFF);
        plot.tics(PLOT_AXIS_XY, PLOT_OFF);*/
        /* plotting the frame of the car */
        plot[i].data2DCurve(x_polygon, y_polygon, 19);
        plot[i].lineType(27, 1, 1, "blue");
        plot[i].plotType(PLOT_PLOTTYPE_FILLEDCURVES, 27, "closed");
        plot[i].data2DCurve(x_top_frame, y_top_frame, 13);
        plot[i].lineType(28, 1, 1, "blue");
        plot[i].plotType(PLOT_PLOTTYPE_FILLEDCURVES, 28, "closed");
        /* plotting the wheel */
        plot[i].data2DCurve(x_wheel_1, y_wheel_1, N);
        plot[i].lineType(29, 1, 3, "black");
        plot[i].data2DCurve(x_wheel_2, y_wheel_2, N);
        plot[i].lineType(30, 1, 3, "black");
        /*plotting the wheel spoke */
        a1 = a1 - theta;
        a2 = a2 - theta;
        a3 = a3 - theta;
        if (a1 < 0)
            a1 = a1 + 360;
        if (a2 < 0)
            a2 = a2 + 360;
        if (a3 < 0)
            a3 = a3 + 360;
        plot[i].line(x_wheel_center_1, 4, 0, x_wheel_1[a1], y_wheel_1[a1], 0);
        plot[i].line(x_wheel_center_1, 4, 0, x_wheel_1[a2], y_wheel_1[a2], 0);
        plot[i].line(x_wheel_center_1, 4, 0, x_wheel_1[a3], y_wheel_1[a3], 0);
        plot[i].line(x_wheel_center_2, 4, 0, x_wheel_2[a1], y_wheel_2[a1], 0);
        plot[i].line(x_wheel_center_2, 4, 0, x_wheel_2[a2], y_wheel_2[a2], 0);
        plot[i].line(x_wheel_center_2, 4, 0, x_wheel_2[a3], y_wheel_2[a3], 0);
        for(j = 31; j < 37; j++){
            plot[i].lineType(j, 1, 2, "black");
        }
        if (i == 0)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car.gif");
        else if (i == 1)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car1.gif");
        else if (i == 2)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car2.gif");
        else if (i == 3)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car3.gif");
        else if (i == 4)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car4.gif");
        else if (i == 5)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car5.gif");
        else if (i == 6)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car6.gif");
        else if (i == 7)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car7.gif");
        else if (i == 8)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car8.gif");
        else if (i == 9)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car9.gif");
        else if (i == 10)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car10.gif");
        else if (i == 11)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car11.gif");
        else if (i == 12)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car12.gif");
        else if (i == 13)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car13.gif");
        else if (i == 14)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car14.gif");
        else if (i == 15)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car15.gif");
        else if (i == 16)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car16.gif");
        else if (i == 17)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car17.gif");
        else if (i == 18)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car18.gif");
        else if (i == 19)
            plot[i].outputType(PLOT_OUTPUTTYPE_FILE, "gif", "car19.gif");
        
        plot[i].plotting();
    }
    
    return 0;
}
