I assume you mean dash as in NSBezierPath's setLineDash:count:phase:.  Also, 
does the dash have just two elements (one segment and one gap), or is it more 
complex.

Does it have to be the same exact pattern all the way around?  If it's okay to 
differ a teeny bit (I bet imperceptibly), you could solve the problem 
separately for the horizontal edges and the vertical edges, and draw four lines 
instead of one rect.

Offhand, here's what comes to mind, just solving for one edge and assuming the 
pattern is simply { segmentLength, gapLength }.

In the final solution, how many iterations of the pattern will the line have?  
If both ends are in the middle of a segment, the answer is the same as if one 
end were at the beginning of a segment and the other at the end of a gap.  In 
other words, the line length will be an exact multiple of segment length.

How far off are we from that?  Let's figure out how many iterations fit now, 
and how much is left over.  (Excuse my sloppy floating point coding.)

CGFloat iterationLength = segmentLength + gapLength;
CGFloat numIterations = floor(lineLength / iterationLength);
CGFloat leftOver = lineLength - (numIterations * iterationLength);

We want to spread the leftover amount evenly across all iterations, so let's 
figure out how much to add to each iteration.  We could split that fudge factor 
equally between the segment and the gap, and I bet no one would notice, or to 
be fussy we could distribute it proportionally:

CGFloat iterationFudge = leftOver / numIterations;  // Assume numIterations > 0.
CGFloat segmentFudge = iterationFudge * (segmentLength / iterationLength);
CGFloat gapFudge = iterationFudge * (gapLength / iterationLength);

CGFloat myDashPattern[2] = { segmentLength + segmentFudge, gapLength + gapFudge 
};

// Assume myBezierPath contains the line.
[myBezierPath setLineDash:myDashPattern
                    count:2
                    phase:(myDashPattern[0] / 2.0)];

You could probably tweak some things to minimize rounding issues, but I don't 
know if that would make a noticeable difference.

--Andy

On Dec 12, 2013, at 7:54 AM, Graham Cox <graham....@bigpond.com> wrote:

> Does anyone have code or at least an outline of how to adjust a dash so that 
> it fits exactly to the corners of a rectangle?
> 
> The dash itself is supplied, but may be minimally adjusted in both phase and 
> length of any of its elements to fit, such that the rect corners lie exactly 
> in the centre of a solid part of the dash. It only needs to work for rects.
> 
> 
> —Graham



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to