/********************************************** 
 * File: projectiles.cpp                      *
 * Plot projectiles with the initial velocity * 
 * 35m/s and different projection angles      *
 **********************************************/
#include<math.h>
#include<chplot.h>

#define M_G 9.80  // gravitational constant g
#define N 100     // number of points for plotting

int main() {
    int i, j;
    double v0, theta0, xf, x[N], y[N];
    CPlot plot;
    char legend[64];

    v0 = 35.0;                          // initial velocity
    plot.title("Projectiles with different initial projection angles");
    /* initial projection angle 15 degrees increment */
    for(j=15; j<=90; j += 15) {
        theta0 = j*M_PI/180;            // projection angle in radian 
        xf = v0*v0*sin(2*theta0)/M_G;   // the distance
        for(i=0; i<N; i++) {
            x[i] = i*xf/(N-1);
            y[i] = tan(theta0)*x[i] - 
                   M_G*x[i]*x[i]/(2*v0*v0*cos(theta0)*cos(theta0));
        }
        plot.data2D(x, y);
        sprintf(legend, "projection angle %d degrees", j);
        plot.legend(legend, j/15-1);
    }
    plot.border(PLOT_BORDER_LEFT, PLOT_OFF);
    plot.plotting();
}
