#include <stdio.h>
#include <gd.h>
#include <math.h>
void draw_roc_flag(gdImagePtr img);
void draw_white_sun(gdImagePtr img, int x, int y, int size, int color);
void connectPoints(gdImagePtr img, int x1, int y1, int x2, int y2, int color);
int main() {
int width = 1200;
int height = (int)(width * 2.0 / 3.0);
gdImagePtr img = gdImageCreateTrueColor(width, height);
gdImageAlphaBlending(img, 0);
draw_roc_flag(img);
FILE *outputFile = fopen(
"Y:/tmp/c_ex/roc_flag_in_gd.png"
,
"wb"
);
if
(outputFile == NULL) {
fprintf(stderr,
"Error opening the output file.\n"
);
return
1;
}
gdImagePngEx(img, outputFile, 9);
fclose(outputFile);
gdImageDestroy(img);
return
0;
}
void draw_roc_flag(gdImagePtr img) {
int width = gdImageSX(img);
int height = gdImageSY(img);
int red, white, blue;
int center_x = (int)(width / 4);
int center_y = (int)(height / 4);
int sun_radius = (int)(width / 8);
int white_circle_dia = sun_radius;
int blue_circle_dia = white_circle_dia + white_circle_dia * 2 / 15;
red = gdImageColorAllocate(img, 255, 0, 0);
white = gdImageColorAllocate(img, 255, 255, 255);
blue = gdImageColorAllocate(img, 0, 0, 149);
gdImageFilledRectangle(img, 0, 0, width, height, red);
gdImageFilledRectangle(img, 0, 0, (int)(width / 2.0), (int)(height / 2.0), blue);
draw_white_sun(img, center_x, center_y, sun_radius, white);
gdImageFilledEllipse(img, center_x, center_y, blue_circle_dia, blue_circle_dia, blue);
gdImageFilledEllipse(img, center_x, center_y, white_circle_dia, white_circle_dia, white);
connectPoints(img, 429, 125, 279, 165, white);
connectPoints(img, 279, 165, 170, 274, white);
connectPoints(img, 170, 274, 319, 234, white);
connectPoints(img, 319, 234, 429, 125, white);
}
void draw_white_sun(gdImagePtr img, int center_x, int center_y, int sun_radius, int color) {
float deg = M_PI / 180;
float sr = sun_radius / tan(75 * deg);
int ax, ay, bx, by, dx, dy, ex, ey;
ax = center_x;
ay = center_y - sun_radius;
bx = center_x - sun_radius * tan(15 * deg);
by = center_y;
ex = center_x;
ey = center_y + sun_radius;
dx = center_x + sun_radius * tan(15 * deg);
dy = center_y;
for
(int i = 1; i <= 6; i++) {
int ax1 = ax + sun_radius * sin(30 * deg * i);
int ay1 = ay + sun_radius - sun_radius * cos(30 * deg * i);
int bx1 = bx + sr - sr * cos(30 * deg * i);
int by1 = by - sr * sin(30 * deg * i);
int ex1 = ex - sun_radius * sin(30 * deg * i);
int ey1 = ey - (sun_radius - sun_radius * cos(30 * deg * i));
int dx1 = dx - (sr - sr * cos(30 * deg * i));
int dy1 = dy + sr * sin(30 * deg * i);
gdImageFilledPolygon(img, (gdPoint[4]){{ax1, ay1}, {bx1, by1}, {ex1, ey1}, {dx1, dy1}}, 4, color);
gdImageLine(img, ax1, ay1, bx1, by1, color);
gdImageLine(img, bx1, by1, ex1, ey1, color);
gdImageLine(img, ex1, ey1, dx1, dy1, color);
gdImageLine(img, dx1, dy1, ax1, ay1, color);
}
}
void connectPoints(gdImagePtr img, int x1, int y1, int x2, int y2, int color) {
gdImageLine(img, x1, y1, x2, y2, color);