iospdfcore-graphicscgpdfcontext

Fit Polygons Together


How to eliminate the boundary between triangles?

black triangles with white boundaries

I'm implementing export-to-pdf in my triangle drawing app. The image above shows what the pdf output looks like. There are white boundaries everywhere, less than 1 pixel wide.

The triangles can have any color.


I draw each triangle, like this:

CGContextBeginPath(context);
CGContextMoveToPoint(context, x0, y0);
CGContextAddLineToPoint(context, x1, y1);
CGContextAddLineToPoint(context, x2, y2);
CGContextClosePath(context);
CGContextFillPath(context);

It's important that black and white triangles have the same sizes.


Approaches

Approach 1 - Stroke

Draw a 1 pixel thick stroke around all triangles.

Approach 2 - Extrude

Extrude all triangles by 2 pixels so the triangles overlaps.

Approach 3 - Combine

Combine touching triangles into a single polygon.

Approach 4 - PDF overlap settings

Perhaps PDF has settings for eliminating boundaries. Dunno.

Approach 5 - Post processing

Create a filter that detects boundary pixels and eliminates them. This will not work for me, it needs to be saved to a PDF. Shader code is not supported in PDF on iOS, AFAIK.

Approach X - A smarter way

Is there a better way of snapping triangles together?


Solution

  • Approach 3 - Combine

    I'm using GPC – General Polygon Clipper library and it almost works as desired.

    I run a UNION operation one triangle at a time. Until all triangles have been UNION'ed into the result.

    Below is output from GPC. No white edges can be seen.

    Output when using GPC.


    I also tried using Angus Johnson's Clipper library, but was unable to built polygons by UNIONing one triangle at a time. It only removed a few of the white edges between triangles. Although Clipper seems more powerful than GPC.

    Below is output from Clipper, it shows white edges. Output when using Clipper, has white edges