gtk - c++调用cairo库如何实现图元沿曲线分布,即Drawing Lines with Image Patterns。
迷茫
迷茫 2017-04-17 15:32:11
0
1
667

这种沿贝塞尔曲线分布图像的方式用cairo库怎么实现,希望有做图形方面的来交流一下
,这个是图元

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
刘奇

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template