简介
动态演示贝塞尔曲线的绘制过程。
支持任意点数、任意阶次。
点击鼠标右键,在鼠标位置将创建新的点。
按住鼠标左键拖动控制点。
空格键屏幕将清空。
一起来玩一玩吧!
运行效果
源码
// 程序名称:贝塞尔曲线
//
#include <math.h> // sin
#include <stdint.h> // uint64_t
#include <vector> // array
#include <graphics.h> // window
using std::vector;
#define WIDTH 800 // 宽
#define HEIGHT 600 // 高
struct Point { double x, y; };
// 初始化控制点
vector<Point> controlPoints;
Point operator+(const Point& a, const Point& b)
{
return Point({ a.x + b.x, a.y + b.y });
}
Point operator*(double f, const Point& p)
{
return Point({f*p.x, f*p.y});
}
// 计算二项式系数:C(n, k) = n! / (k!(n-k)!)
// 这里我们不用公式,使用 Pascal's Triangle
// [1], n