3

I have a library that draws regular bezier path figures (complex paths formed of a lot of bezier points), using midpoint approximation.

I can draw them without problem, but I need to add support for advanced geometry operations: Nearest point of a curve, intersection, figure contains point, and more importantly, path combinations: difference, intersection, exclusive-or, union, ...

Is there any good source to get all this?

Thanks

xan
  • 7,511
  • 2
  • 32
  • 45
  • I realize this is old. Is it still relevant? What is the result of the different set operations? If you take take the union of two paths that share no points, do you just have two paths stored in a single data structure? – JCooper Mar 28 '11 at 18:07

1 Answers1

1

I've had to implement some of those operations on curves or closed paths. It mostly boils down to line and polygon operations. A few useful concepts:

  1. The control points form a convex hull around the Bezier path, which is useful to short circuit intersection-related operations.
  2. Your curve subdivision should be adaptive, stopping when the next subdivision won't be a significant difference, which means each "half" may divide to a different depth.
  3. You can subdivide a curve at any point, not just the midpoint, which is useful for creating a Bezier subcurve ending at a found interestion point.

Example code for arbitrary subdivision:

 static Point2D.Double[][] splitBezier(Point2D.Double[] p) {
     return splitBezier(p, 0.5);
 }

 static Point2D.Double[][] splitBezier(Point2D.Double[] p, double t) {
    Point2D.Double[][] parts = new Point2D.Double[2][4];
    Point2D.Double ab = interpolate(t, p[0], p[1]);
    Point2D.Double bc = interpolate(t, p[1], p[2]);
    Point2D.Double cd = interpolate(t, p[2], p[3]);
    Point2D.Double abc = interpolate(t, ab, bc);
    Point2D.Double bcd = interpolate(t, bc, cd);
    Point2D.Double abcd = interpolate(t, abc, bcd);
    parts[0][0] = p[0];
    parts[0][1] = ab;
    parts[0][2] = abc;
    parts[0][3] = abcd;
    parts[1][0] = abcd;
    parts[1][2] = bcd;
    parts[1][2] = cd;
    parts[1][3] = p[3];
    return parts;
 }

 static Point2D.Double interpolate(double t, Point2D.Double a, Point2D.Double b) {
    return new Point2D.Double((1 - t) * a.getX() + t * b.getX(),
                              (1 - t) * a.getY() + t * b.getY());
 }

Some useful sites:

xan
  • 7,511
  • 2
  • 32
  • 45