<<
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; (這些變數的目的可能是用來表示顏色的一些屬性)
im = gdImageCreateTrueColor(600, 400);
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;
gdImageFilledArc(im, centerX, centerY, radius, radius, 0, 180, blue, gdPie);
gdImageFilledArc(im, centerX, centerY - 1, radius, radius, 180, 360, red, gdPie);
我們可以使用相同的半徑(radius
)值來表示寬度和高度,從而畫出一個圓形的弧形或扇形。這樣的寬度和高度相等的情況下,就可以視 radius
為圓形的半徑。
int blueCircleRadius = 50;
int offsetX = 49;
gdImageFilledEllipse(im, centerX + offsetX, centerY, blueCircleRadius * 2, blueCircleRadius * 2, blue);
int redCircleRadius = 50;
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