/*****************************************************************************************
* File: stars.cpp
* Directory: F:\CPP-Projects\Stars
* Written by Michael Main (January 8, 2026)
*
* A simple WinBGIm program to draw six stars with various number of points at fixed 
* locations on the screen.
*/


// Directives:
#include <cstdlib>
#include <cmath>
#include <graphics.h>
using namespace std;


// Named constants and function prototypes:
const double PI = 3.14159265358979323846264338327950288;
const int S = 600; // Width and height of the graphics window.


// The draw_chord function draws a chord of a circle where:
// + The circle's center is (px, py) in pixel coordinates.
// + The radius parameter is the radius of the circle in pixels. .
// + The starting and ending points of the chord are points on the circle defined
//   by the trigonometric angles (in radians) as measured clockwise from the circle's
//   rightmost point. For example, with theta0 = PI/2 and theta1 = PI, the function
//   draws a chord from the top of the circle to the leftmost point of the circle.
void draw_chord(int cx, int cy, int radius, double theta0, double theta1);


// The draw_star function draws a star that is centered at (cx, cy) in pixel coordinates
// with a radius given by the radius parameter (also in pixels). The many_points parameter
// determines the number of points of the star. 
// Prerequisite: (many_points is odd) and (many_points >= 5).
void draw_star(int cx, int cy, int radius, int many_points);


//----------------------------------------------------------------------------------------
int main( )
{
    // 1. Initialize the graphics window:
    initwindow(S, S, "Starlight, Star bright . . .");
    
    // 2. Draw six stars: 
    draw_star(S/5, S/3, S/17, 5);
    draw_star(S/5, 2*S/3, S/50, 7);
    draw_star(int(0.73*S), int(0.64*S), S/6, 9);
    draw_star(int(0.5*S), int(0.43*S), S/10, 11);
    draw_star(int(0.88*S), int(0.12*S), S/73, 13);
    draw_star(int(0.66*S), int(0.1*S), S/39, 15);

    // 3. Finish:
    delay(100000);
    return EXIT_SUCCESS;
}
// End of main


//----------------------------------------------------------------------------------------
void draw_chord(int cx, int cy, int radius, double theta0, double theta1)
{    
    int start_x = cx + radius*cos(theta0);
    int start_y = cy - radius*sin(theta0);
    int end_x = cx + radius*cos(theta1);
    int end_y = cy - radius*sin(theta1);
    
    line(start_x, start_y, end_x, end_y);
} // End of draw_chord


//----------------------------------------------------------------------------------------
void draw_star(int cx, int cy, int radius, int many_points)
{
    int i; // control variable for the loop that draws the chords of the star
    double radians_per_point = 2 * PI / many_points; // distance between star points   
    
    // Each line of the star spans a certain number of its points, including the end point 
    // but not the start. For example, a line in a five-pointed spans two of its points.
    // This computes that number of points and the equivalent radians in the span:
    int points_spanned = (many_points/2); // number of points spanned by one star line
    double radians_spanned = points_spanned * radians_per_point;
    
    // The angles of the endpoints of the first line that we'll draw, which starts at the
    // top of the encompassing circle:      
    double theta0 = PI/2;  
    double theta1 = theta0 + radians_spanned; 
     
    // Draw the lines of the star, shifting the endpoints as we go:  
    for (i = 0; i < many_points; ++i)
    {
        draw_chord(cx, cy, radius, theta0, theta1); 
        theta0 = theta1;           // The new start is the previous end.
        theta1 += radians_spanned; // The new end moves forward by radians_spanned.  
    }
}
// End of draw_star

