跳动的爱心

跳动的心形图案,通过字符打印和延时效果模拟跳动,心形在两种大小间交替跳动。

通过数学公式生成心形曲线

#include

#include // Windows 系统头文件(用于延时和清屏)

void printHeart(int size, int beat) {

    for (int y = size; y >= -size; y--) {

        for (int x = -size; x <= size; x++) {

            // 心形数学公式:(x² + y² - 1)³ - x²y³ ≤ 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;

}
 

你可能感兴趣的:(c语言)