繪製貝塞爾曲線
阿新 • • 發佈:2019-02-10
簡介明瞭直接上程式碼:
#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); }