41223133

  • Home
    • SMap
    • reveal
    • blog
  • About
  • W5
    • 曲線圖
  • W6
  • W12
    • GD圖
  • W13
  • homework
    • 台灣國旗
    • 美國國旗
    • 日本國旗
    • 中國國旗
    • 英國國旗
    • 韓國國旗
  • Brython
homework << Previous Next >> 美國國旗

台灣國旗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// https://en.wikipedia.org/wiki/Flag_of_the_Republic_of_China
// cc roc_flag.c -lgd -lm to link with gd and math library
// https://www.rapidtables.com/web/color/RGB_Color.html
// 幾何形狀著色與繪圖練習
// 以下 gd 繪圖程式嘗試畫出 ROC 國旗, 請根據下列程式內容完成後續的國旗繪圖
#include <stdio.h>
#include <gd.h>
#include <math.h>
  
void draw_roc_flag(gdImagePtr img);
void draw_white_sun(gdImagePtr img, int center_x, int center_y, int sun_radius, int white, int red, int blue);
  
int main() {
    // wdth 3: height 2
    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("./roc_flag.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);
  
    // Colors for the flag
    red = gdImageColorAllocate(img, 242, 0, 0); // Red color
    white = gdImageColorAllocate(img, 255, 255, 255); // White stripes
    blue = gdImageColorAllocate(img, 0, 41, 204); // Blue
  
    // 繪製紅色矩形區域
    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, red, blue);
}
void draw_white_sun(gdImagePtr img, int center_x, int center_y, int sun_radius, int white, int red, int blue) {
    float angle = 0;
    int numRays = 12; // 光芒的數量
  
    gdPoint points[3]; // 三個頂點的陣列
  
    for (int i = 0; i < numRays; i++) {
        angle = i * (2 * M_PI / numRays);
        float x1 = center_x + cos(angle) * sun_radius;
        float y1 = center_y + sin(angle) * sun_radius;
  
        // 調整兩個底邊頂點的位置
      float x2 = center_x + cos(angle + 0.35) * (sun_radius * 0.5);
      float y2 = center_y + sin(angle + 0.35) * (sun_radius * 0.5);
      float x3 = center_x + cos(angle - 0.35) * (sun_radius * 0.5);
      float y3 = center_y + sin(angle - 0.35) * (sun_radius * 0.5);
  
        // 設定多邊形的三個頂點
        points[0].x = (int)x1;
        points[0].y = (int)y1;
        points[1].x = (int)x2;
        points[1].y = (int)y2;
        points[2].x = (int)x3;
        points[2].y = (int)y3;
  
        gdImageFilledPolygon(img, points, 3, white);
    }
  //外圈
  gdImageFilledEllipse(img, center_x, center_y, sun_radius * 1.2, sun_radius * 1.2, blue);
  
    // 繪製太陽內部
    gdImageFilledEllipse(img, center_x, center_y, sun_radius * 1.1, sun_radius * 1.1, white);
}




homework << Previous Next >> 美國國旗

Copyright © All rights reserved | This template is made with by Colorlib