#include <stdio.h>
#include <string.h>

#define G	(-9.8)			/* Earth's gravity	*/


/*
 *  Print help message
 */
void
help( )
{
	fprintf( stderr, "Usage is:  gravity [options]\n" );
	fprintf( stderr, "  -s x  set start time to x\n" );
	fprintf( stderr, "  -e x  set end time to x\n" );
	fprintf( stderr, "  -n x  set number of time steps to x\n" );
}



/*
 *  Compute parabolic path for a bouncing ball
 */
main( argc, argv )
	int argc;
	char *argv[];
{
	float startTime = 0.0;		/* Starting time	*/
	float endTime   = 1.0;		/* Ending time		*/
	int nTimes      = 10;		/* # of desired times	*/
	float x0 = 0.0, y0 = 0.0;	/* Initial position	*/
	float vx = 0.0, vy = 0.0;	/* Initial velocity	*/
	float now = 0.0;		/* Current time		*/
	float deltaTime = 0.0;		/* Start to end time	*/
	float t;			/* Time since start	*/
	float x, y;			/* Current position	*/
	int i;				/* Counter		*/


	/*
	 *  Parse command-line arguments.
	 */
	while ( --argc > 0 )
	{
		++argv;
		if ( argv[0][0] != '-' )
		{
			fprintf( stderr, "Unknown argument:  %s\n", argv[0] );
			help( );
			exit( 1 );
		}
		switch ( argv[0][1] )
		{
		case 's':
			--argc;
			++argv;
			startTime = atof( argv[0] );
			break;

		case 'e':
			--argc;
			++argv;
			endTime = atof( argv[0] );
			break;

		case 'n':
			--argc;
			++argv;
			nTimes = atoi( argv[0] );
			break;

		default:
			fprintf( stderr, "Unknown flag:  %s\n", argv[0] );
			help( );
			exit( 1 );
		}
	}


	/*
	 *  Set up parameters
	 */
	vy = (-G) * (endTime - startTime);
	deltaTime = (endTime - startTime) / (float)(nTimes - 1);


	/*
	 *  Output times
	 */
	printf( "\t# key fractional times\n" );
	now = startTime;
	for ( i = 0; i < nTimes; i++ )
	{
		t = now - startTime;
		printf( "\t\t%5.2f,\n", now );
		now += deltaTime;
	}


	/*
	 *  Output positions
	 */
	printf( "\t# key position values\n" );
	now = startTime;
	for ( i = 0; i < nTimes; i++ )
	{
		t = now - startTime;
		x = x0 + vx * t;
		y = y0 + vy * t + G * t * t;
		printf( "\t\t%5.2f %5.2f %5.2f,\t# %5.2f\n", x, y, 0.0, now );
		now += deltaTime;
	}
}

