41223221 cp2023

  • Home
    • SMap
    • reveal
    • blog
  • 關於
  • 韓國國旗解說
    • 國旗程式詳細解說
  • w2-3
  • w4-5
  • w6
  • w7
  • w8
  • W11-12
  • w13
  • w15
  • w16
  • cEX
    • 課程1
    • 練習1
  • ANSIC
    • 課程2
    • 練習2
  • 期末總結
<< Previous Next >> w2-3

國旗程式詳細解說

include <gd.h>

GD 是一個用於圖形處理的函式庫,它提供了一些基本的繪圖功能

#include <stdio.h>

它用於導入標準輸入輸出(Standard Input/Output)函式庫的標頭文件。

include <math.h>

用於導入數學函式庫的標頭文件。這樣你的程式就能夠使用像 sin、cos、sqrt 等數學函式了。

gdImagePtr im; (gdImagePtr 是 GD 函式庫中表示圖片的指標類型)
FILE *output; (
是標準 C 語言中用於文件輸出的指標)

int white, red, blue, black; (這些變數的目的可能是用來表示顏色的一些屬性)

// 創建一個真彩色的圖像,寬度為600,高度為400

im = gdImageCreateTrueColor(600, 400);

// 分配顏色,這裡的數字表示RGB值,white為白色

white = gdImageColorAllocate(im, 255, 255, 255);

red = gdImageColorAllocate(im, 225, 0, 0);

blue = gdImageColorAllocate(im, 0, 0, 225);

black = gdImageColorAllocate(im, 0, 0, 0);

// 填充白色背景,將整個圖像填充為白色

gdImageFilledRectangle(im, 0, 0, 599, 399, white);

gdImageFilledArc 是 GD(Graphics Draw)函式庫中的一個函式,用於在圖像中填充弧形。

// 在中間添加紅藍各一半的圓,將紅色移到上方,藍色移到下方

int centerX = 300; int centerY = 200; int radius = 200;

// 這兩個函式分別繪製一半的圓,使用gdPie表示只繪製半圓

gdImageFilledArc(im, centerX, centerY, radius, radius, 0, 180, blue, gdPie);

gdImageFilledArc(im, centerX, centerY - 1, radius, radius, 180, 360, red, gdPie);

我們可以使用相同的半徑(radius)值來表示寬度和高度,從而畫出一個圓形的弧形或扇形。這樣的寬度和高度相等的情況下,就可以視 radius 為圓形的半徑。

// 在中間添加向右偏移49的直徑為100的藍色圓

int blueCircleRadius = 50; // 半徑為50

int offsetX = 49;

// 繪製橢圓,以形成一個圓

gdImageFilledEllipse(im, centerX + offsetX, centerY, blueCircleRadius * 2, blueCircleRadius * 2, blue);

// 在中間添加向左偏移50的直徑為100的紅色圓

int redCircleRadius = 50; // 半徑為50

// 繪製橢圓,以形成一個圓

gdImageFilledEllipse(im, centerX - 49, centerY, redCircleRadius * 2, redCircleRadius * 2, red);

gdImageFilledEllipse 是 GD(Graphics Draw)函式庫中的一個函式,用於在圖像中填充橢圓。

// 創建一個新的圖像,用於保存旋轉後的圖像

gdImagePtr rotated_im = gdImageCreateTrueColor(600, 400);

rotated_im。這個指標用於表示一個新的圖像,通常用來保存旋轉或變換後的圖像。

// 填充白色背景

gdImageFilledRectangle(rotated_im, 0, 0, 599, 399, white);

// 複製原始圖像到旋轉後的圖像中

gdImageCopy(rotated_im, im, 0, 0, 0, 0, 600, 400);

// 進行逆時針旋轉,角度為25度

double rotationAngle = 25.0;

double rotationAngle 是一個雙精度浮點數變數,它用來儲存旋轉的角度。

gdImageRotate(rotated_im, im, 0, 0, 600, 400, gdTrueColorAlpha(255, 255, 255, 127));

gdImageRotate 函式,該函式用於對圖像進行旋轉。

// 繪製斜直的長方形
int rectWidth = 15; // 長方形寬度
int rectHeight = 120; // 長方形高度
int rectSpacing = 10; // 長方形間隔

// 計算三個長方形的左上角座標
int rect1X = 139 - rectWidth - rectSpacing; // 第一個長方形
int rect1Y = 206 - rectHeight / 2;
int rect2X = rect1X - rectWidth - rectSpacing; // 第二個長方形
int rect2Y = rect1Y;
int rect3X = rect2X - rectWidth - rectSpacing; // 第三個長方形
int rect3Y = rect1Y;

gdPoint points1[4];
    points1[0].x = (int)(rect1X - centerX) * cos(rotationAngle * M_PI / 180) - (rect1Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points1[0].y = (int)(rect1X - centerX) * sin(rotationAngle * M_PI / 180) + (rect1Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points1[1].x = (int)((rect1X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - (rect1Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points1[1].y = (int)((rect1X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + (rect1Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points1[2].x = (int)((rect1X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - ((rect1Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points1[2].y = (int)((rect1X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + ((rect1Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points1[3].x = (int)(rect1X - centerX) * cos(rotationAngle * M_PI / 180) - ((rect1Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points1[3].y = (int)(rect1X - centerX) * sin(rotationAngle * M_PI / 180) + ((rect1Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;

    gdPoint points2[4];
    points2[0].x = (int)(rect2X - centerX) * cos(rotationAngle * M_PI / 180) - (rect2Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points2[0].y = (int)(rect2X - centerX) * sin(rotationAngle * M_PI / 180) + (rect2Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points2[1].x = (int)((rect2X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - (rect2Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points2[1].y = (int)((rect2X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + (rect2Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points2[2].x = (int)((rect2X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - ((rect2Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points2[2].y = (int)((rect2X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + ((rect2Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points2[3].x = (int)(rect2X - centerX) * cos(rotationAngle * M_PI / 180) - ((rect2Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points2[3].y = (int)(rect2X - centerX) * sin(rotationAngle * M_PI / 180) + ((rect2Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;

    gdPoint points3[4];
    points3[0].x = (int)(rect3X - centerX) * cos(rotationAngle * M_PI / 180) - (rect3Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points3[0].y = (int)(rect3X - centerX) * sin(rotationAngle * M_PI / 180) + (rect3Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points3[1].x = (int)((rect3X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - (rect3Y - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points3[1].y = (int)((rect3X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + (rect3Y - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points3[2].x = (int)((rect3X + rectWidth) - centerX) * cos(rotationAngle * M_PI / 180) - ((rect3Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points3[2].y = (int)((rect3X + rectWidth) - centerX) * sin(rotationAngle * M_PI / 180) + ((rect3Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;
    points3[3].x = (int)(rect3X - centerX) * cos(rotationAngle * M_PI / 180) - ((rect3Y + rectHeight) - centerY) * sin(rotationAngle * M_PI / 180) + centerX;
    points3[3].y = (int)(rect3X - centerX) * sin(rotationAngle * M_PI / 180) + ((rect3Y + rectHeight) - centerY) * cos(rotationAngle * M_PI / 180) + centerY;

    gdImageFilledPolygon(rotated_im, points1, 4, black);
    gdImageFilledPolygon(rotated_im, points2, 4, black);
    gdImageFilledPolygon(rotated_im, points3, 4, black);

首先,程式碼使用了 gdPoint 這個結構體定義,表示一個二維平面上的點,具有 x 和 y 座標。

程式碼創建了三個陣列 points1、points2、points3,每個陣列包含四個點,用來表示三個矩形的頂點座標。

cos sin是由include <math.h>標題檔來的

M_PI 是在 C 語言中提供的數學常數,表示圓周率 π(pi)

rect1X - centerX 是一個數學表達式,表示矩形的一個頂點的 x 座標相對於圖像的中心 x 座標的偏移量。

cos(rotationAngle * M_PI / 180) 是一個表達式,用來計算角度 rotationAngle 對應的弧度值的餘弦值。

rect1Y - centerY 是一個數學表達式,表示矩形的一個頂點的 y 座標相對於圖像的中心 y 座標的偏移量。

sin(rotationAngle * M_PI / 180) 是一個表達式,用來計算角度 rotationAngle 對應的弧度值的正弦值。

rotationAngle 是以度為單位的角度,而 M_PI / 180 是用來將角度轉換為弧度的比例因子。

其餘的都跟上面一樣只是運算的位置不一樣,還有運算的長方形也不一樣


<< Previous Next >> w2-3

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