CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(HeartShape)
# 设置C标准
set(CMAKE_C_STANDARD 99)
# 设置EasyX路径
set(EasyX_INCLUDE_DIR "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/include")
set(EasyX_LIB_DIR "C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Auxiliary/VS/lib")
# 根据目标架构选择库路径
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
set(EasyX_LIB_PATH "${EasyX_LIB_DIR}/x64/EasyXa.lib")
else()
set(EasyX_LIB_PATH "${EasyX_LIB_DIR}/x86/EasyXa.lib")
endif()
# 添加可执行文件
add_executable(HeartShape main.cpp)
# 包含头文件目录
target_include_directories(HeartShape PRIVATE ${EasyX_INCLUDE_DIR})
# 链接EasyX库
target_link_libraries(HeartShape ${EasyX_LIB_PATH})
main.cpp 必须使用cpp文件,因为EasyX 只能被C++调用
#include <graphics.h>
#include <math.h>
#include <iostream>
// 手动定义 M_PI
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// 绘制心形函数
void drawHeart(int x0, int y0, int size) {
for (double t = 0; t <= 2 * M_PI; t += 0.01) {
// 心形的参数方程
double x = 16 * pow(sin(t), 3);
double y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t);
// 缩放和平移
int px = static_cast<int>(x * size + x0);
int py = static_cast<int>(-y * size + y0);
// 绘制点
putpixel(px, py, RED);
}
}
int main() {
// 初始化图形窗口
initgraph(800, 600);
// 设置背景颜色
setbkcolor(WHITE);
cleardevice();
// 计算心形的中心位置
int centerX = 400;
int centerY = 300;
int heartSize = 10;
// 绘制心形
drawHeart(centerX, centerY, heartSize);
// 等待用户按键关闭窗口
std::cin.get();
// 关闭图形窗口
closegraph();
return 0;
}