绘制贝塞尔曲线

简介明了直接上代码:

#define PI 3.14159265

struct point
{
	float x;
	float y;
};

void decas(int point_count, struct point *point_v, float t, struct point *point_dst)
{
	int r;
	float t1 = 1 - t;
	struct point point_1[10];
	int i;
	for (i = 0; i < point_count; i++) {
		point_1[i] = point_v[i];
	}
	for (r = 1; r < point_count; r++) {
		for (i = 0; i < point_count - r; i++) {
			point_1[i].x = t1 * point_1[i].x + t * point_1[i + 1].x;
			point_1[i].y = t1 * point_1[i].y + t * point_1[i + 1].y;
		}
	}
	point_dst->x = point_1[0].x;
	point_dst->y = point_1[0].y;
}

int bezier(struct state *state, int point_count, struct point *point_v)
{
	const int div_count = 80;
	float t = 0.0;
	int i;
	struct point point_dst, point_prev = {point_v[point_count - 1].x, point_v[point_count - 1].y};
	/* draw frame */
	for (i = 0; i < point_count; i++) 
	{
		point_dst.x = point_v[i].x;
		point_dst.y = point_v[i].y;
		line_dda(state, point_dst.x, point_dst.y, point_prev.x, point_prev.y, COLOR_GREEN(state->screen));
		point_prev = point_dst;
	}

	point_prev.x = point_prev.y = 0;
	/* draw bezier */
	for (i = 0; i < div_count; i++) {
		t += 1.0 / div_count;
		decas(point_count, point_v, t, &point_dst);
		printf("%.2f, (%d:%d)\n", t, (int)point_dst.x, (int)point_dst.y);
		if (!(point_prev.x == 0 && point_prev.y == 0))
			line_dda(state, point_dst.x, point_dst.y, point_prev.x, point_prev.y, COLOR_RED(state->screen));
		point_prev = point_dst;
	}
	return 0;
}

void On_Draw(struct state *state)
{
	struct point point_v[] = {
		{100, 20},
		{200, 70},
		{200, 200},
		{20,200},
	};
	bezier(state, 4, point_v);
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值