How to tell if a 2d point is within a set of Bézier curves?

1.7k Views Asked by At

I have a set of Bézier curves defined like pictured:

Bezier curves

They make a closed shape like so:

Blob shape

Given a point $\left(x,y\right)$ I need to tell if it is inside this set of Bézier curves, each defined by four points.

Edit: pseudocode can be given too :)

1

There are 1 best solutions below

5
On

There are a few ways to do this:

  1. Approximate each Bézier curve by a polyline. Then your problem becomes a point-in-polygon problem, which has been studied to death. The Wikipedia page on the subject describes two techniques: one based on firing rays and counting intersections, and the other based on winding numbers. Of course, the results will only be as accurate as your polyline approximation. If you want to avoid errors due to this approximation, you can apply the ray-firing algorithm directly to the original Bézier curves. You'll have to write (or find) a function to intersect a line with a Bézier curve. This involves solving a cubic equation. You can find relevant stuff here and here and here.

  2. The fastest approach is to just render your shape into a bitmap (which you already know how to do, it appears), and see if the bitmap covers your point. So, specifically, you set the pixel at your test point to white, render the shape in black, and see if your point remains white. This way, you're using the GPU to do the bulk of the calculations, and that's what gives you the speed.

The above are general methods. Your particular case seems quite special, though -- the large yellow region appears to be bordered by four Bézier curves that have horizontal and vertical tangents. In testing the yellow region, you may be able to take advantage of this simplicity.