由于代码较长,我将分步骤提供完整的解决方案。 首先,我们需要包含必要的头文件并初始化图形窗口: ```cpp #include <graphics.h> #include <vector> #include <cmath> using namespace std; struct Point { int x, y; }; vector<Point> controlPoints; void drawBezierCurve() { if (controlPoints.size() < 2) return; for (double t = 0.0; t <= 1.0; t += 0.001) { double x = 0, y = 0; int n = controlPoints.size() - 1; for (int i = 0; i <= n; i++) { double binomialCoeff = tgamma(n + 1) / (tgamma(i + 1) * tgamma(n - i + 1)); double term = binomialCoeff * pow(t, i) * pow(1 - t, n - i); x += term * controlPoints[i].x; y += term * controlPoints[i].y; } lineto(x, y); } } void initGraph() { initgraph(800, 600); setbkcolor(WHITE); cleardevice(); setcolor(BLACK); } ``` 接下来,我们添加鼠标事件处理函数来允许用户通过点击添加控制点,并拖动这些点: ```cpp void mouseHandler() { MOUSEMSG m; while (true) { m = GetMouseMsg(); switch (m.uMsg) { case WM_LBUTTONDOWN: controlPoints.push_back({m.x, m.y}); break; case WM_MOUSEMOVE: if (m.uMsg == WM_LBUTTONDOWN) { controlPoints.back().x = m.x; controlPoints.back().y = m.y; } break; } cleardevice(); drawBezierCurve(); for (auto& p : controlPoints) { circle(p.x, p.y, 5); } } } ``` 在主函数中调用初始化和事件处理函数: ```cpp int main() { initGraph(); mouseHandler(); closegraph(); return 0; } ``` 这段代码提供了一个基本的框架,你可以在此基础上增加更多的功能,如计算曲线上任意点的坐标、优化性能等。注意,这个程序需要EasyX库支持,确保你的开发环境已经配置了该库。 [2024-12-09 14:36:47 | AI写代码神器 | 659点数解答]