Task: Draw the standard national flag (outline diagram).The Flag Law of the People’s Republic of China: The national flag of the People’s Republic of China is the Five-Star Red Flag. The national flag is a symbol and sign of the People’s Republic of China. Every citizen and organization should respect and cherish the national flag.In this task, it is required to accurately calculate the size, position, rotation angle, and other elements of the national flag, and draw the outline diagram.Difficulties: It is necessary to accurately calculate the rotation angle of each small pentagram based on the grid. At the same time, encapsulate the code for drawing the pentagram into a function for easy repeated calls.Source Code
#include <graphics.h>
#include <math.h>
#include <conio.h>
#define PI acos(-1.0)
// Pentagram drawing function
void DrawStar(int centerX, int centerY, float radius, float rotateAngle, COLORREF color) {
const float innerRadius = radius * sin(18 * PI / 180) / sin(126 * PI / 180); // Calculate the radius of the inscribed circle
POINT points[10]; // Store the 10 vertices of the pentagram
// Calculate the coordinates of each vertex
for (int i = 0; i < 10; ++i) {
float angle = rotateAngle + i * 36 * PI / 180;
float r = (i % 2 == 0) ? radius : innerRadius;
points[i].x = centerX + r * cos(angle);
points[i].y = centerY + r * sin(angle);
}
setlinecolor(color);
circle(centerX,centerY, radius);
polygon(points, 10); // Draw the filled pentagram
}
int main() {
// Standard flag ratio parameters
const int flagWidth = 900; // Flag width
const int flagHeight = 600; // Flag height (3:2 ratio)
const int gridSize = 30; // Grid size (15x10 grid)
initgraph(flagWidth, flagHeight);
setbkcolor(WHITE);
cleardevice();
setlinestyle(PS_SOLID,1);
//// Draw the red flag surface
//setfillcolor(RED);
//solidrectangle(0, 0, flagWidth, flagHeight);
// Large pentagram parameters
const float mainStarRadius = flagHeight * 3.0 / 20; // Diameter 3/10 height
const int mainStarX = flagHeight / 10 + mainStarRadius;
const int mainStarY = flagHeight / 10 + mainStarRadius;
// Draw the large pentagram (no rotation)
DrawStar(mainStarX, mainStarY, mainStarRadius, -PI / 2, BLACK);
// Small pentagram parameters
const float smallStarRadius = mainStarRadius / 3;
const POINT smallStarCenters[4] = {
{ // Center of the first small star
static_cast<int>(mainStarX + 5 * gridSize),
static_cast<int>(mainStarY - 3 * gridSize)
},
{ // Center of the second small star
static_cast<int>(mainStarX + 7 * gridSize),
static_cast<int>(mainStarY - gridSize)
},
{ // Center of the third small star
static_cast<int>(mainStarX + 7 * gridSize),
static_cast<int>(mainStarY + 2 * gridSize)
},
{ // Center of the fourth small star
static_cast<int>(mainStarX + 5 * gridSize),
static_cast<int>(mainStarY + 4 * gridSize)
}
};
// Calculate the rotation angle of each small star (pointing to the center of the large star)
for (int i = 0; i < 4; ++i) {
// Calculate the vector from the small star to the large star (note the Y-axis direction)
double dx = mainStarX - smallStarCenters[i].x;
double dy = smallStarCenters[i].y - mainStarY; // Y-axis inverted
// Calculate the angle in standard mathematical coordinates (in radians)
double angle = 2 * PI - atan2(dy, dx);
DrawStar(smallStarCenters[i].x, smallStarCenters[i].y,
smallStarRadius, static_cast<float>(angle), BLACK);
// Add debug lines to observe the pointing relationship
setlinecolor(BLACK);
line(smallStarCenters[i].x, smallStarCenters[i].y,
mainStarX, mainStarY);
}
// Draw auxiliary grid lines (for debugging)
setlinecolor(BLACK);
for (int x = 0; x <= flagWidth; x += gridSize)
line(x, 0, x, flagHeight);
for (int y = 0; y <= flagHeight; y += gridSize)
line(0, y, flagWidth, y);
_getch();
closegraph();
return 0;
}