How can I calculate the area of a bezier curve?

前端 未结 6 1611
孤独总比滥情好
孤独总比滥情好 2020-12-08 10:49

Given the following path (for example) which describes a SVG cubic bezier curve: \"M300,140C300,40,500,40,500,140\", and assuming a straight line connecting the end points 3

6条回答
  •  有刺的猬
    2020-12-08 11:06

    I hesitated to just make a comment or a full reply. But a simple Google search of "area bezier curve" results in the first three links (the first one being this same post), in :

    http://objectmix.com/graphics/133553-area-closed-bezier-curve.html (archived)

    that provides the closed form solution, using the divergence theorem. I am surprised that this link has not been found by the OP.

    Copying the text in case the website goes down, and crediting the author of the reply Kalle Rutanen:

    An interesting problem. For any piecewise differentiable curve in 2D, the following general procedure gives you the area inside the curve / series of curves. For polynomial curves (Bezier curves), you will get closed form solutions.

    Let g(t) be a piecewise differentiable curve, with 0 <= t <= 1. g(t) is oriented clockwise and g(1) = g(0).

    Let F(x, y) = [x, y] / 2

    Then div(F(x, y)) = 1 where div is for divergence.

    Now the divergence theorem gives you the area inside the closed curve g (t) as a line integral along the curve:

    int(dot(F(g(t)), perp(g'(t))) dt, t = 0..1) = (1 / 2) * int(dot(g(t), perp(g'(t))) dt, t = 0..1)

    perp(x, y) = (-y, x)

    where int is for integration, ' for differentiation and dot for dot product. The integration has to be pieced to the parts corresponding to the smooth curve segments.

    Now for examples. Take the Bezier degree 3 and one such curve with control points (x0, y0), (x1, y1), (x2, y2), (x3, y3). The integral over this curve is:

    I := 3 / 10 * y1 * x0 - 3 / 20 * y1 * x2 - 3 / 20 * y1 * x3 - 3 / 10 * y0 * x1 - 3 / 20 * y0 * x2 - 1 / 20 * y0 * x3 + 3 / 20 * y2 * x0 + 3 / 20 * y2 * x1 - 3 / 10 * y2 * x3 + 1 / 20 * y3 * x0 + 3 / 20 * y3 * x1 + 3 / 10 * y3 * x2

    Calculate this for each curve in the sequence and add them up. The sum is the area enclosed by the curves (assuming the curves form a loop).

    If the curve consists of just one Bezier curve, then it must be x3 = x0 and y3 = y0, and the area is:

    Area := 3 / 20 * y1 * x0 - 3 / 20 * y1 * x2 - 3 / 20 * y0 * x1 + 3 / 20 * y0 * x2 - 3 / 20 * y2 * x0 + 3 / 20 * y2 * x1

    Hope I did not do mistakes.

    --
    Kalle Rutanen
    http://kaba.hilvi.org

提交回复
热议问题