41223108 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • 倉儲維護
    • s.cycu.org維護
    • windows維護
  • weekly progress
    • w1~4
    • w5
    • w6
    • w7
    • w9
    • w12
      • helloworld
      • GD繪圖程式1
    • W13
    • w15
  • ANSIC
    • 1.
    • 2.
    • 3.
    • 4.
    • 5.
    • 6.
    • 7.
    • 8.
    • 9.
    • 10.
  • NOTE
    • ssh
    • puttygen
  • 國旗練習
    • Thai
    • Laos
    • Bangladesh
    • vietnam
    • Italian
    • Russia
    • Belgium
    • Ukraine
    • Korea
  • 課程評分
  • Brython
  • setup
  • C_lib
  • replit
  • MSD
  • Final
國旗練習 << Previous Next >> Laos

Thai

#include <stdio.h>
#include <gd.h>
#include <math.h>

#define WIDTH 600
#define HEIGHT 400

void drawThaiFlag(gdImagePtr im) {
    int white = gdImageColorAllocate(im, 255, 255, 255);
    int indigo = gdImageColorAllocate(im, 46, 49, 146);  // 較深的靛藍色
    int red = gdImageColorAllocate(im, 255, 0, 0);

    // Draw white background
    gdImageFilledRectangle(im, 0, 0, WIDTH, HEIGHT, white);

    // Calculate stripe heights based on the specified ratio
    int totalStripes = 5;
    int stripeHeights[5] = {1, 1, 2, 1, 1};
    int totalHeight = 0;
    for (int i = 0; i < totalStripes; ++i) {
        totalHeight += stripeHeights[i];
    }

    // Draw stripes
    int startY = 0;
    for (int i = 0; i < totalStripes; ++i) {
        int stripeHeight = stripeHeights[i] * HEIGHT / totalHeight;
        switch (i % 5) {
            case 0: // Red stripe
                gdImageFilledRectangle(im, 0, startY, WIDTH, startY + stripeHeight, red);
                break;
            case 1: // White stripe
                gdImageFilledRectangle(im, 0, startY, WIDTH, startY + stripeHeight, white);
                break;
            case 2: // Indigo stripe (靛藍色)
                gdImageFilledRectangle(im, 0, startY, WIDTH, startY + stripeHeight, indigo);
                break;
            case 3: // White stripe
                gdImageFilledRectangle(im, 0, startY, WIDTH, startY + stripeHeight, white);
                break;
            case 4: // Red stripe
                gdImageFilledRectangle(im, 0, startY, WIDTH, startY + stripeHeight, red);
                break;
        }
        startY += stripeHeight;
    }
}

int main() {
    gdImagePtr im;
    FILE *outputFile;

    // Create a new image with specified width and height
    im = gdImageCreate(WIDTH, HEIGHT);

    // Draw the Thai flag on the image
    drawThaiFlag(im);

    // Open a file for writing
    outputFile = fopen("thai_flag.png", "wb");

    // Write the image to the file in PNG format
    gdImagePng(im, outputFile);

    // Close the file
    fclose(outputFile);

    // Free the memory used for the image
    gdImageDestroy(im);

    return 0;
}


國旗練習 << Previous Next >> Laos

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