2

I'm doing some drawing relative to a scaled image so I end up with fractional CGPoints. I am scaling the results from the CoreImage face detection routine.

Do I want to round these myself or leave it to iOS to do it when I use these points in CGPathAddLineToPoint calls? If it is better to round, should I round up or down?

I've read about pixel boundaries, etc. but I'm not sure how to apply that here. I am drawing to a CALayer

CGPoint leftEye = CGPointMake((leftEyePosition.x * xScale), 
                              (leftEyePosition.y * yScale));

// result

features {
    faceRect = "{{92, 144.469}, {166.667, 179.688}}";
    hasLeftEyePosition = 1;
    hasMouthPosition = 1;
    hasRightEyePosition = 1;
    leftEyePosition = "{142.667, 268.812}";
    mouthPosition = "{176, 189.75}";
    rightEyePosition = "{207.333, 269.531}";
}
spring
  • 18,009
  • 15
  • 80
  • 160
  • CoreGraphics will antialias the lines and draw 'third' pixels by tweaking the opacity/saturation. –  Jan 21 '13 at 23:36

1 Answers1

12

Whether or not you round, and in what direction, depends on the effect you are trying to accomplish.

CoreGraphics itself has absolutely no problem with fractional coordinates. However, drawing anything using fractional coordinates is going to end up antialiasing the drawn objects. This typically causes them to look fuzzy. Rounding your coordinates appropriately is a good idea to avoid this.

Be warned, however. Depending on what you're drawing and how, you may want coordinates that are 0.5 pixels off instead of integral coordinates. For example, if you're drawing a line, the line is centered on the coordinate you give. So a 1-pixel line drawn on integral coordinates will actually end up being a fuzzy line 2 pixels wide (with each pixel accounting for half of the line). The simplest thing to remember is that strokes are centered on the coordinates, but fills are bounded by them. So when filling a rectangle, integral coordinates is best. When stroking a rectangle, inset your coordinates by 0.5 pixels (or, rather, by half of the stroke width you want to use).

Also, don't forget that when drawing an image that's meant to be displayed on a retina screen with scale=2, coordinates that are 0.5 units off are actually still on pixel boundaries. So if you know it's retina, you can avoid rounding to fully integral coordinates when the nearest half-unit coordinate is fine.

Lily Ballard
  • 182,031
  • 33
  • 381
  • 347