/* File: car.cpp 
   Draw a car using CPlot 
*/
#include <stdio.h>
#include <math.h>
#include <chplot.h>
#define N 361

int main(){
    
    /* initial corrdinates for car */
    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};
    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};
    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};
    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;
    double x_wheel_1[N], y_wheel_1[N], x_wheel_2[N], y_wheel_2[N];
    double angle, 
           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);
    }
    
    /* drawing the car */
    CPlot plot;
    
    /* 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.data2DCurve(x_polygon, y_polygon, 19);
    plot.lineType(0, 1, 1, "blue");
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 0, "closed");
    plot.data2DCurve(x_top_frame, y_top_frame, 13);
    plot.lineType(1, 1, 1, "blue");
    plot.plotType(PLOT_PLOTTYPE_FILLEDCURVES, 1, "closed");
    /* plotting the wheel */
    plot.data2DCurve(x_wheel_1, y_wheel_1, N);
    plot.lineType(2, 1, 15, "black");
    plot.data2DCurve(x_wheel_2, y_wheel_2, N);
    plot.lineType(3, 1, 15, "black");
    /*plotting the wheel spoke */
    plot.line(2, 1, 0, x_wheel_1[90], y_wheel_1[90], 0);
    plot.line(2, 1, 0, x_wheel_1[210], y_wheel_1[210], 0);
    plot.line(2, 1, 0, x_wheel_1[330], y_wheel_1[330], 0);
    plot.line(9, 1, 0, x_wheel_2[90], y_wheel_2[90], 0);
    plot.line(9, 1, 0, x_wheel_2[210], y_wheel_2[210], 0);
    plot.line(9, 1, 0, x_wheel_2[330], y_wheel_2[330], 0);
    for(i = 4; i < 10; i++){
        plot.lineType(i, 1, 10, "black");
    }
    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;
}
