基础Bezier曲线

版权声明:欢迎转载,请注明出处噢,谢谢 https://blog.csdn.net/DT2131/article/details/78948149

N 为点数

DIV 为曲线细分程度

使用了 EasyX 图形库,感谢 EasyX 团队

#include <bits/stdc++.h>
#include <graphics.h>
#include <conio.h>
using namespace std;
typedef struct Node {
	int x, y;
}Node;
const int MAXN = 1007;
const int N = 8;
const int W = N - 1;
const int DIV = 50000;
Node nodes[N];
int a[MAXN][MAXN];
//int cnt[640][480], maxx;
void ini() {
	memset(a, 0, sizeof(a));
	//memset(cnt, 0, sizeof(cnt)); maxx = 0;
	for (int i = 0; i <= 1e2; i++) {
		a[i][0] = 1;
		for (int j = 1; j <= i; j++) {
			a[i][j] = a[i - 1][j - 1] + a[i - 1][j];
		}
	}
}
void input() {
	nodes[0] = { 100,100 };
	nodes[1] = { 140,160 };
	nodes[2] = { 180,200 };
	nodes[3] = { 220,220 };
	nodes[4] = { 260,270 };
	nodes[5] = { 300,300 };
	nodes[6] = { 340,320 };
	nodes[7] = { 370,370 };
	for (int i = 0; i < N; i++) putpixel(nodes[i].x, nodes[i].y, RED);
}
void Bezier() {
	double t, dt = 1.0 / DIV, x, y;
	for (t = 0; t <= 1; t += dt) {
		x = 0, y = 0;
		for (int j = 0; j <= W; j++) {
			x += a[W][j] * nodes[j].x*pow(t, j)*pow((1 - t), W - j);
			y += a[W][j] * nodes[j].y*pow(t, j)*pow((1 - t), W - j);
		}
		putpixel((int)x, (int)y, WHITE);
		//cnt[(int)x][(int)y]++;
		//maxx = max(maxx, cnt[(int)x][(int)y]);
	}
}
int main()
{
	initgraph(640, 480);
	ini();
	input();
	Bezier();
	system("pause");
    return 0;
}



猜你喜欢

转载自blog.csdn.net/DT2131/article/details/78948149