41223209 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • About
  • w5
  • w6
  • w7
  • w12
  • w13
  • w15
  • w16
  • ANSIC
  • c_ex
  • 教學影片
  • 倉儲與網站評分項目
  • Brython
w5 << Previous Next >> w7

w6

// 為了使用print
#include <stdio.h>
// 使用GD函式庫
#include <gd.h>
//為了計算三角函數
#include <math.h>

// void因不會回傳任何值而用於紀錄所需的各項指令及參數
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);

// main() { 為整個程式執行的開頭,加上int以回傳結果
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);

// 輸出名為roc_flag.png的檔案
FILE *outputFile = fopen("./roc_flag.png", "wb");
if (outputFile == NULL) {
fprintf(stderr, "Error opening the output file.\n");
return 1;
}
// 將檔案存為png格式
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);

// 設定顏色
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);
}

// 為了使用print
#include <stdio.h>
// 使用GD函式庫
#include <gd.h>
//為了計算三角函數
#include <math.h>

// void因不會回傳任何值而用於紀錄所需的各項指令及參數
void draw_usa_flag(gdImagePtr img);
void draw_star(gdImagePtr img, int x, int y, int size, int color, double rotation_angle);

// main() { 為整個程式執行的開頭,加上int以回傳結果
int main() {
int width = 800;
int height = (int)(width / 1.9);

// 創建一個新圖像
gdImagePtr img = gdImageCreateTrueColor(width, height);
// 不調整顏色透明度
gdImageAlphaBlending(img, 0);

draw_usa_flag(img);

// 在images資料夾輸出名為usa_flag.png的檔案
FILE *outputFile = fopen("./../images/usa_flag.png", "wb");
if (outputFile == NULL) {
fprintf(stderr, "打开输出文件时出错。\n");
return 1;
}
// 將檔案存為png格式
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;
// 國旗顏色
red = gdImageColorAllocate(img, 178, 34, 52); // 紅色條紋
white = gdImageColorAllocate(img, 255, 255, 255); // 白色條紋
blue = gdImageColorAllocate(img, 60, 59, 110); // 藍色矩形
int stripe_height = height / 13;
int stripe_width = width;
int star_size = (int)(0.0308 * height); // 星星大小
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.129 * height); // 横向星星之間的間距
int star_spacing_y = (int)(0.054 * height); // 縱向星星之間的間距
int star_start_x = (int)(0.125 * height); // 星星的起始X位置
int star_start_y = (int)(0.0485 * height); // 星星的起始Y位置
for (int row = 0; row < 9; row++) {
int starsPerRow = (row % 2 == 0) ? 6 : 5;
// 計算2、4、6和8排星星的偏移量
int offset_x = (row % 2 == 0) ? star_spacing_x / -2 : 0;
for (int star = 0; star < starsPerRow; star++) {
int x = star_start_x + star * star_spacing_x + offset_x;
// 旋轉角度(以弧度為單位)
double rotation_angle = M_PI / 5; 
int y = star_start_y + row * star_spacing_y;
draw_star(img, x, y, star_size, white, rotation_angle);
}
}
}
void draw_star(gdImagePtr img, int x, int y, int size, int color, double rotation_angle) {
gdPoint points[10];
for (int i = 0; i < 10; i++) {
double angle = M_PI / 2 + i * 2 * M_PI / 10 + rotation_angle;
int radius = (i % 2 == 0) ? size : size / 2;
points[i].x = x + radius * cos(angle);
points[i].y = y + radius * sin(angle);
}
// 用指定的顏色填充星星
gdImageFilledPolygon(img, points, 10, color);
}


w5 << Previous Next >> w7

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