41223227 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • ANSIC
  • c_ex
    • jsliu_c_programming:
    • introduce to c:
  • W16
  • W15
  • 線上繪圖
  • w13
  • w12
  • w7
  • w6
  • w5
  • 倉儲與網站評分項目
  • 期末總結
  • Brython
w7 << Previous Next >> w5

w6

// 使用標準輸入/輸出函數
#include <stdio.h>
// 繪製gd函式庫
#include <gd.h>
// 繪製數學函數庫
#include <math.h>

// 聲明 draw_usa_flag 函數
// 聲明 draw_star 函數
void draw_usa_flag(gdImagePtr img);
void draw_star(gdImagePtr img, int x, int y, int size, int color);

int main() {
    int width = 800;
    int height = (int)(width / 1.9);

    gdImagePtr img = gdImageCreateTrueColor(width, height);
    gdImageAlphaBlending(img, 0);

    draw_usa_flag(img);

    FILE *outputFile = fopen("./../images/usa_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_usa_flag(gdImagePtr img) {
    int width = gdImageSX(img);
    int height = gdImageSY(img);
    int red, white, blue;
    // Colors for the flag
    red = gdImageColorAllocate(img, 178, 34, 52); // Red stripes
    white = gdImageColorAllocate(img, 255, 255, 255); // White stripes
    blue = gdImageColorAllocate(img, 60, 59, 110); // Blue field

    int stripe_height = height / 13;
    int stripe_width = width;
    int star_size = (int)(0.0308 * height); // Corrected star size (half the original size)

    for (int y = 0; y < height; y += stripe_height) {
        if (y / stripe_height % 2 == 0) {
            gdImageFilledRectangle(img, 0, y, stripe_width, y + stripe_height, red);
        } else {
            gdImageFilledRectangle(img, 0, y, stripe_width, y + stripe_height, white);
        }
    }

    gdImageFilledRectangle(img, 0, 0, width * 2 / 5, stripe_height * 7, blue);

    int star_spacing_x = (int)(0.063 * height); // Horizontal spacing between stars
    int star_spacing_y = (int)(0.054 * height); // Vertical spacing between stars
    int star_start_x = (int)(0.0616 * height); // Starting X position for stars
    int star_start_y = (int)(0.0485 * height); // Starting Y position for stars

    for (int row = 0; row < 9; row++) {
        int starsPerRow = (row % 2 == 0) ? 6 : 5;

        for (int star = 0; star < starsPerRow; star++) {
            int x = star_start_x + star * star_spacing_x;
            int y = star_start_y + row * star_spacing_y;
            draw_star(img, x, y, star_size, white);
        }
    }
}

void draw_star(gdImagePtr img, int x, int y, int size, int color) {
    gdPoint points[10];

    for (int i = 0; i < 10; i++) {
        double angle = M_PI / 2 + i * 2 * M_PI / 10;
        int radius = (i % 2 == 0) ? size : size / 2;
        points[i].x = x + radius * cos(angle);
        points[i].y = y + radius * sin(angle);
    }

    // Fill the star with white color
    gdImageFilledPolygon(img, points, 10, color);
}

// 使用標準輸入/輸出函數
#include <stdio.h>
// 繪製gd函式庫
#include <gd.h>
// 繪製數學函數庫
#include <math.h>

// 聲明 draw_roc_flag 函數
// 聲明 draw_white_sun 函數
void draw_roc_flag(gdImagePtr img);
void draw_white_sun(gdImagePtr img, int x, int y, int size, int color);

int main() {
    // width 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("./../images/roc_flag1.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
    // red rectangle area
    gdImageFilledRectangle(img, 0, 0, width, height, red);
    // blue rectangle area
    gdImageFilledRectangle(img, 0, 0, (int)(width/2.0), (int)(height/2.0), blue);
    // 目前僅畫出青天白日的輪廓直線, 請嘗試計算所需的點座標完成國旗繪圖
    draw_white_sun(img, center_x, center_y, sun_radius, white);
}

void draw_white_sun(gdImagePtr img, int center_x, int center_y, int sun_radius, int color) {
    float angle = 0;
    int fromX, fromY;
    int toX, toY;
    for (int i=0; i<24; i++){
        angle += 5*M_PI*2/12;
        //printf("%.3f", angle);
        toX = center_x + cos(angle)*sun_radius;
        toY = center_y + sin(angle)*sun_radius;
        // 只有 i 為 0 時移動到 toX, toY, 其餘都進行直線繪圖
        if (i!=0){
            gdImageLine(img, fromX, fromY, toX, toY, color);
        }
        fromX = toX;
        fromY = toY;
   }
}




w7 << Previous Next >> w5

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