[cairo] Finding minimum distance to a stroke or fill

mental at rydia.net mental at rydia.net
Fri Feb 11 16:58:48 PST 2005


Quoting Ashwin Bharambe <ashwinb at gmail.com>:

> Hi,
>
> I know that cairo_in_stroke (cairo_in_fill) would answer me if a
> point
> P was inside the stroke (fill), respectively. However, how would
> I go
> about finding the distance of the point in the stroke/fill which
> is
> closest to a certain point P?

Knowing roughly what you want this for, perhaps you would do better
to use a different approach...

For example, testing whether the stroke/fill overlaps a filled
circle with radius n centered on the given point?

Cairo does not do that for you any more than it does
closest-point-on-path, but it is a significantly more tractable
problem than trying to find the closest point on a bezier curve
(which is relatively difficult).

The overlapping-circle approach can be approximated using a regular
polygon (for a proof-of-concept a square with sides of length n
should be sufficient).

For testing against the fill, just use either cairo_current_path or
the locally cached pre-cairo path, and return true if:

  the center point falls within the path ||
  the first point of any subpath falls within the polygon ||
  any path segment intersects the polygon

(if you use cairo_current_path, I'm not sure whether using that and
subdividing yourself, or using cairo_current_path_flat would be
more/less efficient)

For testing against the stroke, increase the polygon's radius by the
stroke width and return true if:

  the first point of any subpath falls within the polygon ||
  any path segment intersects the polygon

(admittedly that will miss the miter part of miter-joined strokes)

You can probably combine both tests into a single pass.

Longer-term, perhaps cairo can add versions of cairo_in_stroke and
cairo_in_fill that take a radius and center coordinates?

Comments?  Other ideas?

-mental



More information about the cairo mailing list