/* Program CIRCLE_3.C
**
** Illustrates how to draw a line which turns in a circle
**
** Draws a circle in the center of the screen and a line moves
** around the circle.
**
** P. H. Anderson, MSU, 10 Nov, '94
**
*/

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

#define PI 3.14159

void draw_polar_line (int x_1, int y_1, int l, float theta);
/* draws a line of length l at angle theta using the specified
** line style and color from graphics coords x, y. angle theta is
** in degrees.
*/

float degrees_to_rads (float x);

void main(void)
{
	int driver, mode, n;
	float angle;
	driver = DETECT;  /* autotect*/
	mode = 0;
	clrscr();
	initgraph(&driver, &mode, "c:\\turboc");

	setcolor(BLUE); /*sets the color of the circle */
	circle (300, 250, 200); /* x=300, y=250, r=200 */

	setfillstyle (SOLID_FILL, YELLOW);
	floodfill (300, 250, BLUE);
	/* fills area in which x, y are located bounded by BLUE */

	setlinestyle(SOLID_LINE, 0, NORM_WIDTH);

	while(1)
	{
	   for (n=0; n<90; n++)
	   {
		 angle = 360.0 * n/90;
		 setcolor(RED);
		 draw_polar_line (300, 250, 200, angle);
		 delay(200);

		 setcolor(YELLOW);  /* erase the line */
		 draw_polar_line (300, 250, 200, angle);
	   }
	}
	restorecrtmode(); /* restores the screen to the original screen */
}

void draw_polar_line (int x_1, int y_1, int l, float theta)
{
   int diff_x, diff_y, x_2, y_2;
   float angle_rads;
   angle_rads = degrees_to_rads (theta);
   diff_x = (int) l * cos (angle_rads);
   diff_y = (int) l * sin (angle_rads);

   x_2 = x_1 + diff_x;
   y_2 = y_1 - diff_y;  /* note that up is minus */

   line (x_1, y_1, x_2, y_2);
}

float degrees_to_rads (float x)
{
   return(PI/180.0*x);
}