Home  >  Q&A  >  body text

gtk - c++调用cairo库如何实现图元沿曲线分布,即Drawing Lines with Image Patterns。

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

迷茫迷茫2715 days ago611

reply all(1)I'll reply

  • 怪我咯

    怪我咯2017-04-17 15:34:11

    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.

    reply
    0
  • Cancelreply