title: How to transform UIBezierPath/CGPath within a reference frame?
body: <p>I need to scale a path, but instead of using the path's bounds, the transform should be performed using a larger, enclosing, arbitrary rect. (i.e. a "canvas".)</p>
  
  <p>My problem is how to do that without drawing the canvas.</p>
  
  <p>For example, in the following image:</p>
  
  <ul>
  <li><p>the blue square indicates the native rect of the path (a rocket in this case).</p></li>
  <li><p>the purple rect indicates the bounds of the "canvas" or parent coordinate system.</p></li>
  <li><p>the purple rect is for positioning only; not to be drawn. The whitespace is similarly ignored.</p></li>
  <li><p>I need to scale the canvas, and keep the transformed rocket path (for clipping operations, caching etc.). i.e. I need to lose the purple rect after transformation.</p></li>
  </ul>
  
  <p><a href="https://i.stack.imgur.com/RE4dL.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RE4dL.png" alt="Rocket"></a></p>
  
  <p>Ideally, I'd use something like the following fiction:</p>
  
  <pre><code>var canvasPath = UIBezierPath(rect: canvasRect)
  canvasPath.append(rocketPath, withName: "rocket")
  canvasPath.doSomeTransformOrOther()
  let transformedRocketPath = canvasPath.path(forName: "rocket")
  </code></pre>
  
  <p>Sadly, the magical append(:withName:) and path(forName:) methods do not exist, even if <code>UIBezierPath</code> or <code>CGPath</code> treated subpaths like this. Alas!</p>
  
  <p>Another approach could be to temporarily add points to the path, one for each canvas corner. Then, transform the path. Then, walk the path with CGPath's (hideous) <code>apply(info:function:)</code> method, rebuild the path without the canvas points, then draw the result. (Yeah, pass the vodka.)</p>
  
  <p>Can anyone offer a more sane solution? Thanks for any helpi</p>
  
  <p>(For those interested: this is related to a problem of how to center irregularly-shaped paths. The purple rect represents the bounds of the minimum enclosing circle. Drawing the rocket with the MEC's coordinate system will make the rocket appear more visually centered, compared to using boundingBox.)</p>
  
  <p>More info: <a href="https://stackoverflow.com/questions/44239513/fitting-uibezierpath-cgpath-to-circle">Fitting UIBezierPath/CGPath to circle</a></p>