用c++写一个绘制贝塞尔曲面的cpp文件,用easyx库实现,在曲面上把贝塞尔曲线连接起来
时间: 2024-06-09 09:11:29 浏览: 24
以下是一个绘制贝塞尔曲面并连接成一个网格的C++代码,使用了EasyX图形库:
```c++
#include <graphics.h>
#include <cmath>
#define WIDTH 640
#define HEIGHT 480
// 计算贝塞尔曲线上的点
POINT BezierPoint(int t, POINT* points, int count)
{
POINT pt;
pt.x = pt.y = 0;
for (int i = 0; i < count; i++)
{
int k = 1;
for (int j = 0; j < i; j++)
{
k *= (count - j - 1) * t / (j + 1);
}
pt.x += k * points[i].x;
pt.y += k * points[i].y;
}
return pt;
}
// 绘制贝塞尔曲线
void BezierCurve(POINT* points, int count, COLORREF color)
{
setcolor(color);
for (int i = 0; i <= 100; i++)
{
int t = i * count / 100;
POINT pt = BezierPoint(t, points, count);
if (i == 0)
{
moveto(pt.x, pt.y);
}
else
{
lineto(pt.x, pt.y);
}
}
}
// 绘制贝塞尔曲面
void BezierSurface(POINT** points, int count, COLORREF color)
{
setcolor(color);
for (int i = 0; i < count; i++)
{
BezierCurve(points[i], count, color);
}
for (int i = 0; i < count; i++)
{
POINT curve[count];
for (int j = 0; j < count; j++)
{
curve[j] = points[j][i];
}
BezierCurve(curve, count, color);
}
}
int main()
{
initgraph(WIDTH, HEIGHT);
POINT points[4][4] = {
{ { 100, 100 },{ 200, 100 },{ 300, 100 },{ 400, 100 } },
{ { 100, 200 },{ 200, 200 },{ 300, 200 },{ 400, 200 } },
{ { 100, 300 },{ 200, 300 },{ 300, 300 },{ 400, 300 } },
{ { 100, 400 },{ 200, 400 },{ 300, 400 },{ 400, 400 } }
};
BezierSurface((POINT**)points, 4, RGB(255, 0, 0));
system("pause");
closegraph();
return 0;
}
```
这段代码使用了一个二维数组来存储曲面上的点,因此可以很容易地修改来绘制不同的曲面。注意需要在程序结束前调用 closegraph() 函数关闭图形窗口。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)