Combining Cross CGPaths in iOS

I have a problem in the application I'm working on. Let's say I have two CGPaths that are quite complex, and I add them as to CGMutablePath (thus, unites them). Well, where two paths intersect, there will be points inside each other. I want to eliminate these internal points and essentially draw an external or path outline. It’s hard for me to understand how I will do this.

Edit: Here is an example of what I'm talking about. Blue and red rectangles represent points along CGPaths. Red rectangles are points that are inside both paths. I would like to somehow eliminate the red dots and redraw only the path contour.

enter image description here

+5
source share
4 answers

, , .

, .

, , , .

. , " ". - Objective-C - . , C, ++. ++, .m .mm. ++, :

, CGPathApply , .

+3

. , 1, :

int pnpoly(int npol, float *xp, float *yp, float x, float y)
{
  int i, j, c = 0;
  for (i = 0, j = npol-1; i < npol; j = i++) {
    if ((((yp[i] <= y) && (y < yp[j])) ||
         ((yp[j] <= y) && (y < yp[i]))) &&
        (x < (xp[j] - xp[i]) * (y - yp[i]) / (yp[j] - yp[i]) + xp[i]))
      c = !c;
  }
  return c;
}

.

:

define starPoly with 10 points
define simplePoly with 7 points

for each point in starPoly
    if ( pnpoly( 7, simplePoly.Xs[], simplePoly.Ys[], point.x, point.y ) == 0 )
        clipedStarPoly += point;

for each point in simplePoly
    if ( pnpoly( 10, starPoly.Xs[], starPoly.Ys[], point.x, point.y ) == 0 )
        clipedSimplePoly += point;

for each point in clipedStarPoly
    solutionPoly += point;

for each point in clipedSimplePoly
    solutionPoly += point;

solutionPoly += solutionPoly.point[0]

, , poly .

poly test,

+1

. , . , , .

A B. , .

  • A , B.
  • .
  • , .
  • , B, .
  • , B .
  • .
  • , , , B, A .
  • , B , , , .
  • 3, B A
  • , , , .

Please note that this solution is acceptable only for one-sided polygons. When it comes to the bezier path, it becomes much more difficult to calculate the intersection points, not to mention the difficulties of combining smooth angles with sharp angles or curves with straight segments.

0
source

All Articles