/* Program CIRCLE_6.C
**
** Draws circle travleing along a line.
** Illustrates use of structures.
**
** Needs work in calculating angles of reflection.
**
** P. H. Anderson, 21 Nov, 94
*/

#include <stdio.h>
#include <graphics.h>
#include <conio.h>
#include <math.h>

#define PI 3.14159

struct Line
{
   int x;
   int y;
   float angle;
};

struct Line draw_circles_along_line(struct Line *line);
void glock(void);

void main(void)
{
	int driver, mode, n;
	struct Line line, new_line;

	line.x = 0; line.y=250; line.angle=0.0;

	driver = DETECT;  /* autotect*/
	mode = 0;
	initgraph(&driver, &mode, "c:\\turboc");

	line.x = 50; line.y=250; line.angle=30.0;
	while(1)
	{
	   new_line = draw_circles_along_line(&line);
	   glock();
	   line = new_line;
	}


	restorecrtmode(); /* restores the screen to the original screen */
}

struct Line draw_circles_along_line(struct Line *line)
{
   int n;
   float distance = 2.0, r=25, x, y;
   struct Line new_line;
   for (n=0;;n++)
   {
	 x = (*line).x + distance * n * cos(PI/180.0 * (*line).angle);
	 y = (*line).y - distance * n * sin(PI/180.0 * (*line).angle);
	 if(x<25)
	 {
	    new_line.angle = 180.0 - 2.0 * (*line).angle;;
	    new_line.x = x+1;
	    new_line.y = y;
	    return(new_line);
	 }
	 if(x>650)
	 {
	    new_line.angle = -180.0 + 2.0 * (*line).angle;
	    new_line.x = x-1;
	    new_line.y = y;
	    return(new_line);
	 }
	 if (y<25)
	 {
	    new_line.angle = - (*line).angle;
	    new_line.x = x;
	    new_line.y = y+1;
	    return(new_line);
	 }
	 if (y>450)
	 {
	    new_line.angle = - (*line).angle;;
	    new_line.x = x;
	    new_line.y = y-1;
	    return(new_line);
	 }
	 setcolor(BLUE);
	 circle(x, y, r);
/*
	 setfillstyle(1, RED);
	 floodfill(x, y, BLUE);
*/
	 delay(1);
/*
	 setfillstyle(1, BLACK);
	 floodfill(x, y, BLUE);
*/
	 setcolor(BLACK);
	 circle(x, y, r);
   }
}

void glock(void) /* wierd sound */
{
   int n;
   for (n=0; n<50; n++)
   {
	 sound(440+n);
	 delay(1);
   }
   nosound();
}