跳動的心形圖案,通過字符打印和延時效果模擬跳動,心形在兩種大小間交替跳動。
通過數學公式生成心形曲線
#include <stdio.h>
#include <windows.h> // Windows 系統頭文件(用于延時和清屏)
void printHeart(int size, int beat) {
? ? for (int y = size; y >= -size; y--) {
? ? ? ? for (int x = -size; x <= size; x++) {
? ? ? ? ? ? // 心形數學公式:(x2 + y2 - 1)3 - x2y3 ≤ 0(調整參數模擬跳動)
? ? ? ? ? ? float fx = (x * 0.4f * beat) * (x * 0.4f * beat) + (y * 0.4f) * (y * 0.4f) - 1;
? ? ? ? ? ? if (fx * fx * fx - (x * 0.4f * beat) * (y * 0.4f) * (y * 0.4f) * (y * 0.4f) <= 0) {
? ? ? ? ? ? ? ? printf("@");?
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? printf(" "); // 空格填充
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? printf("\n");
? ? }
}
int main() {
? ? int beat = 1; // 跳動幅度(1-2)
? ? while (1) {
? ? ? ? system("cls"); // 清屏(Linux/macOS 需改為 "clear")
? ? ? ? printHeart(15, beat); // 繪制心形(尺寸15,幅度beat)
? ? ? ? Sleep(100); // 延時100ms
? ? ? ? beat = (beat == 1) ? 2 : 1; // 切換跳動幅度
? ? }
? ? return 0;
}
?