Calculating this integral along a Bezier curve in code

127 Views Asked by At

I'm doing some graphics code work and need to solve this integral in code:

$$ \int_{0}^{1} f(P(t)) dt $$

Where $P(t)$ is a quadratic or cubic Bezier curve, and $f(p)$ ($p$ is a point) is defined as:

$$ f(p) = \begin{cases} 1 & \text{if } 0 \le p_x \le 1, & 0 \le p_y \le 1 \\ 0 & \text{elsewhere} \end{cases} $$

So we're taking an integral along the path of the Bezier curve, but only within the square region $0 \le p_x \le 1, \text{ } 0 \le p_y \le 1$, since $f(p)$ is $0$ everywhere else. The code that needs to compute this can make no assumptions about the input Bezier curves.

I understand that what we're doing is basically "clipping" the curve to a square region, and we need to find the range of the parameter $t$ where the curve lies within that region (taking into account the case where the curve lies completely outside the region). But I cannot figure out a generalised way to solve this. If I can look at the curve visually then it becomes clear how to find the required range of $t$, but I cannot understand a general technique that can be implemented in code.

1

There are 1 best solutions below

4
On

Approximate the Bézier curve $P(t)$ by a polyline $L(t)$. Clipping the polyline to the square is easy. You will find several intervals, $[a,b]$ where $L(t)$ is inside the square for $t \in [a,b]$. Add up the lengths of these intervals.

You didn’t say anything about the degree of your Bézier curves. If they are always cubic, you can do the clipping directly, without the polyline approximation. Look up “Bézier clipping”.