I have never used cairo, but I know about Bezier curves. First of all, if cairo comes with a Bezier curve function, it goes without saying. Bezier curves can be realized using the subpision method, such as continuous corner cutting. Specifically, the divide and conquer method can be used. Another commonly used method is the de casteljau algorithm. This is drawn by linear interpolation (sliding) in the control stick. It is relatively simple to implement. For details, please refer to the wiki
Okay, in order to answer the question, I wrote a drawing program using de casteljau algorithm:
Core code:
Point Interpolate(vector<Point> points, float u)
{
int n = points.size() - 1;
vector<Point> ps(n);
for (int i = 0; i < n; i++) {
ps[i] = u*points[i] + (1 - u)*points[i + 1];
}
if (n == 1)
return ps[0];
else
return Interpolate(ps, u);
}
vector<Point> Bezier()
{
vector<Point> points;
for (float u = 0.f; u <= 1; u += 0.01f) {
Point p = Interpolate(ctrl_points, u);
points.push_back(p);
}
return points;
}
Full code
If you want the primitives to be evenly distributed along the curve, you need to calculate the arc length of the curve as a function of u. However, the expression of the Bezier curve is more complicated, and integrating the arc length is even more complicated. Therefore, it is recommended to use numerical methods to determine the interval points, and the normal vector of each interval point on the curve can be easily obtained, so that it is easy to map the primitive to the curve.
I have never used cairo, but I know about Bezier curves.
First of all, if cairo comes with a Bezier curve function, it goes without saying.
Bezier curves can be realized using the subpision method, such as continuous corner cutting. Specifically, the divide and conquer method can be used.
Another commonly used method is the de casteljau algorithm. This is drawn by linear interpolation (sliding) in the control stick. It is relatively simple to implement. For details, please refer to the wiki
Okay, in order to answer the question, I wrote a drawing program using de casteljau algorithm:
Core code:
Full code
If you want the primitives to be evenly distributed along the curve, you need to calculate the arc length of the curve as a function of u. However, the expression of the Bezier curve is more complicated, and integrating the arc length is even more complicated. Therefore, it is recommended to use numerical methods to determine the interval points, and the normal vector of each interval point on the curve can be easily obtained, so that it is easy to map the primitive to the curve.