C语言编写绘制三次 B 样条曲线的通用程序,可以通过鼠标指定控制多边形的顶点。
时间: 2024-06-06 10:06:22 浏览: 11
很高兴为您提供帮助!以下是通用程序的代码:
代码:
#include <graphics.h>
#include <stdio.h>
#define MAX_POINTS 20
#define BEZIER_STEPS 1000
typedef struct point_t {
int x;
int y;
} point_t;
// 三次 B 样条曲线的通用程序
void draw_bezier(point_t* points, int num_points) {
int i, j;
double step = 1.0 / BEZIER_STEPS;
double t = 0.0;
for (i = 0; i < BEZIER_STEPS; i++) {
// 计算此时的参数 t 对应的曲线上的点坐标
double x = 0, y = 0;
for (j = 0; j < num_points; j++) {
double b = 1;
int k;
for (k = 0; k < num_points; k++) {
if (j == k) continue;
b *= (t - (double)k / (num_points - 1)) /
((double)j / (num_points - 1) - (double)k / (num_points - 1));
}
x += b * points[j].x;
y += b * points[j].y;
}
// 画出这条曲线
putpixel((int)x, (int)y, WHITE);
t += step;
}
}
int main(void) {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
point_t points[MAX_POINTS] = {0};
int num_points = 0;
// 鼠标指定控制多边形的顶点
while (num_points < MAX_POINTS) {
if (ismouseclick(WM_LBUTTONDOWN)) {
POINT mouse_pos;
getmouseclick(WM_LBUTTONDOWN, mouse_pos.x, mouse_pos.y);
points[num_points].x = mouse_pos.x;
points[num_points].y = mouse_pos.y;
num_points++;
if (num_points > 1) {
// 画出已经选择的点之间的线段
line(points[num_points - 2].x, points[num_points - 2].y, points[num_points - 1].x, points[num_points - 1].y);
}
}
if (ismouseclick(WM_RBUTTONUP)) {
cleardevice();
num_points = 0;
}
}
draw_bezier(points, num_points);
getch();
closegraph();
return 0;
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)