/**************************************************
* File: caranim.c
* Animate a moving car
* (1) Run this program in Ch.
* (2) Click "Go" to view the animation
***************************************************/
#include <stdio.h>
#include <math.h>

int main() {
    
    FILE *stream;
    stream = popen("qanimate", "w");
    if (stream == NULL) {
        fprintf(stderr, "Error: popen() failed\n");
    }
    
    /* initial corrdinates for car */
    double x_polygon[18] = {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};
    double y_polygon[18] = {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};
    double x_top_frame[12] = {1.5, 3, 7, 9, 8.5, 7, 5.2, 5.2, 4.8, 4.8, 3, 2.5};
    double y_top_frame[12] = {3, 5.5, 5.5, 3, 3, 5, 5, 3, 3, 5, 5, 3};
    double x_arc[2] = {2.0, 9.0}, y_arc[2] = {1.0, 1.0};
    double x_circle[2] = {2.0, 9.0}, y_circle[2] = {0.9, 0.9}, r = 0.8;
    double x_ground[2] = {-1, 40.0};
    
    /* A comment line starting with # */
    fprintf(stream, "# qanimate data for car animation\n");
    /* The title displayed on the animation */
    fprintf(stream, "title \"Car Animation\"\n");
    fprintf(stream, "fixture\n");
    /* The primitive following fixture */
    fprintf(stream, "ground -1 0 50.0 0.0\\\n");
    fprintf(stream, "animate\n");
    
    /* declare and intialize initial and final time, time increment, and velocity */
    double t0 = 0, tf = 15, t_incr = 0.1, v = 2;
    double t, d, theta;
    double omega = v / r;       // angular acceleration of the wheel
    for (t = t0; t < tf; t += t_incr) {
        d = t * v;          // distance travel in t seconds    
        theta = omega * t;  // angle travel in t second 
        
        fprintf(stream, "polygon %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f fill blue \\\n",
                x_polygon[0]+d, y_polygon[0], x_polygon[1]+d, y_polygon[1], x_polygon[2]+d, y_polygon[2], x_polygon[3]+d, y_polygon[3], 
                x_polygon[4]+d, y_polygon[4], x_polygon[5]+d, y_polygon[5], x_polygon[6]+d, y_polygon[6], x_polygon[7]+d, y_polygon[7], 
                x_polygon[8]+d, y_polygon[8], x_polygon[9]+d, y_polygon[9], x_polygon[10]+d, y_polygon[10], x_polygon[11]+d, y_polygon[11],
                x_polygon[12]+d, y_polygon[12], x_polygon[13]+d, y_polygon[13], x_polygon[14]+d, y_polygon[14], x_polygon[15]+d, y_polygon[15],
                x_polygon[16]+d, y_polygon[16], x_polygon[17]+d, y_polygon[17]);
        fprintf(stream, "polygon %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f  fill blue \\\n",
                x_top_frame[0]+d, y_top_frame[0], x_top_frame[1]+d, y_top_frame[1], x_top_frame[2]+d, y_top_frame[2],
                x_top_frame[3]+d, y_top_frame[3], x_top_frame[4]+d, y_top_frame[4], x_top_frame[5]+d, y_top_frame[5], 
                x_top_frame[6]+d, y_top_frame[6], x_top_frame[7]+d, y_top_frame[7], x_top_frame[8]+d, y_top_frame[8],
                x_top_frame[9]+d, y_top_frame[9], x_top_frame[10]+d, y_top_frame[10], x_top_frame[11]+d, y_top_frame[11]);
        fprintf(stream, "circle %f %f %f linewidth 3\\\n", x_circle[0]+d, y_circle[0], r);
        fprintf(stream, "line %f %f %f %f\\\n", x_circle[0]+d, y_circle[0], x_circle[0] + d + r * cos(theta)
                                                , y_circle[0] - r * sin(theta));
        fprintf(stream, "line %f %f %f %f\\\n", x_circle[0]+d, y_circle[0], x_circle[0] + d + r * cos(theta + 2 * M_PI / 3),
                                                y_circle[0] - r * sin(theta + 2 * M_PI / 3));
        fprintf(stream, "line %f %f %f %f\\\n", x_circle[0]+d, y_circle[0], x_circle[0] + d + r * cos(theta - 2 * M_PI / 3),
                                                y_circle[0] - r * sin(theta - 2 * M_PI / 3));
        fprintf(stream, "circle %f %f %f\\\n linewidth 3", x_circle[1]+d, y_circle[1], r);
        fprintf(stream, "line %f %f %f %f\\\n", x_circle[1]+d, y_circle[1], x_circle[1] + d + r * cos(theta)
                                                , y_circle[1] - r * sin(theta));
        fprintf(stream, "line %f %f %f %f\\\n", x_circle[1]+d, y_circle[1], x_circle[1] + d + r * cos(theta + 2 * M_PI / 3),
                                                y_circle[1] - r * sin(theta + 2 * M_PI / 3));
        fprintf(stream, "line %f %f %f %f\n", x_circle[1]+d, y_circle[1], x_circle[1] + d + r * cos(theta - 2 * M_PI / 3),
                                                y_circle[1] - r * sin(theta - 2 * M_PI / 3));
    
    }
    
    pclose(stream);
    return 0;
}
