title: Inner shadow with Core Graphics
body: <p>Saw the generated code for drawing inner shadow. Everything is pretty clear and understood except for the portion whereby the shadow is created with <em><strong>copysign</strong></em>.</p>
  <p>I understand what <em><strong>copysign</strong></em> does but why and how is it actually being used in the code below.</p>
  <p>The 0.1 value seem insignificant to the <em><strong>xOffset</strong></em> value.</p>
  <pre><code>  CGContextRef context = UIGraphicsGetCurrentContext();
    
    UIColor *shadow = [UIColor redColor];
    CGSize shadowOffset = CGSizeMake(-2, -0);
    CGFloat shadowBlurRadius = 2;
  
    UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRect: CGRectMake(50, 50, 50, 50)];
    [[UIColor grayColor] setFill];
    [rectanglePath fill];
    
    CGRect rectangleBorderRect = CGRectInset([rectanglePath bounds], -shadowBlurRadius, -shadowBlurRadius);
    rectangleBorderRect = CGRectOffset(rectangleBorderRect, -shadowOffset.width, -shadowOffset.height);
    rectangleBorderRect = CGRectInset(CGRectUnion(rectangleBorderRect, [rectanglePath bounds]), -1, -1);
    
    UIBezierPath *rectangleNegativePath = [UIBezierPath bezierPathWithRect: rectangleBorderRect];
    [rectangleNegativePath appendPath: rectanglePath];
    rectangleNegativePath.usesEvenOddFillRule = YES;
    
    CGContextSaveGState(context);
    {
      CGFloat xOffset = shadowOffset.width + round(rectangleBorderRect.size.width);
      CGFloat yOffset = shadowOffset.height;
      CGContextSetShadowWithColor(context,
                                  CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)),
                                  shadowBlurRadius,
                                  shadow.CGColor);
  
      
      [rectanglePath addClip];
      CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rectangleBorderRect.size.width), 0);
      [rectangleNegativePath applyTransform: transform];
      [[UIColor grayColor] setFill];
      [rectangleNegativePath fill];
    }
    CGContextRestoreGState(context);
  </code></pre>