Given a set of rectangles contained within a larger rectangle such that none overlap, what is the most efficient way to determine the position of a new rectangle such that it is as close as possible to a given reference point while still not overlapping any existing rectangle?
For example, in the following diagram:
the two red rectangles are positioned within the larger, black rectangle, and a third, green rectangle is to be added. The proper position of that green rectangle is shown given three example reference points, denoted by small, blue squares. You'll see that the proper position of the green rectangle always corresponds to the shortest possible distance to its reference point, illustrated by blue lines.
Edit: I should add that the reference point will always lie on the edge of the bounding rectangle.