[Drawkit] Zoom to bounding box
Graham Cox
graham.cox at bigpond.com
Fri Apr 3 16:28:02 PDT 2009
On 04/04/2009, at 1:53 AM, Mike Caron wrote:
> However, it seems that I'm still doing something incorrectly here.
> Suppose, I have a drawing that is mostly whitespace, except for a
> few drawable objects in the lower right corner. Let's say the
> bounding box on these objects is at (35,35) and that it's height and
> width are each 25. Let's say the drawing itself is 60w x 80h. I want
> to crop the drawing now to be 30 x 30 with the bounding box contents
> centered in the middle. It seems that the way to do this is:
>
> 1. Obtain the bounding box of the drawable objects
> 2. Compute the X and Y translation factor (to move the objects)
> 2.1. Xoffset = -(boundingbox.origin.x)
> 2.2. Yoffset = -(boundingbox.origin.y)
> 3. Resize the document (so margins are created properly)
> 4. Iterate through the drawable objects and move them using the -
> offsetLocationByX:byY method
> 4.1. xfactor = Xoffset + marginLeft
> 4.2. yfactor = Xoffset + marginTop
Yes, that should work.
>
> It seems, based on your response that I'll also have to resize each
> drawable object since I've changed the size of the document? Do I
> understand you correctly?
>
No, resizing the objects isn't necessary here. However, originally you
were talking about zooming which of course is mostly a scaling
operation, so to achieve the same effect as zooming a resize is
implied. I guess I understood your needs as fitting the objects to the
drawing size rather than the drawing to the objects.
> Does "slightly harder" mean less efficient? At the point of zooming,
> I've already built an array containing all drawable objects in the
> drawing document. At that point, it seems pretty easy, but if
> there's less efficiency with getting the bounds through the array
> than through the layer, I'll switch it over.
>
If you have an array of the objects already, this method is as good as
any. Either way it simply iterates over the objects and unions all
their bounding rects. The reason I suggested using the layer method is
that usually the layer owns the objects so there's no need to build an
array of objects just for the purpose of obtaining the bounding rect.
If you happen to have an array already, then there's no real difference.
BTW, it's a little unusual to maintain a separate array of the same
objects that are in the drawing. Seems to me that would complicate
things when it came to adding and removing objects from the drawing to
maintain your list in synch (especially with Undo and so on). You may
have your reasons of course. It's straightforward to obtain lists of
objects from the layer whenever you need them.
--Graham
More information about the Drawkit
mailing list