How to position rectangles such that they are as close as possible to a reference point but do not overlap? 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.
 A: I've decided this is way too complicated to ask someone to do for you. Here are my thoughts:
Let $T$ be the rectangle to place.


*

*Consider the outer dilation of each rectangle already placed by $2*T$ (double both dimensions - see Wikipedia picture), and the inner dilation of the bounding rectangle. If the point of reference is not in the union of these dilations, there must be no box near enough to it to stop you placing the box directly on it.

*Consider that the whitespace of a rectangle - the dilation minus the rectangle - has the same width or the same height as $T$ in any region. This implies that if one box enters the whitespace of another, both boxes are in eachother's whitespace.

*Notice this can be associated with a specific Region of Anger. If Box A is above Box B, projecting each box's vertical sides onto the other's horizontal sides will yield two or four of the corners of this Region of Anger. See: collision detection using Separating Axis Theorem.

*Notice the whitespace of the Region of Anger is within the union of Box A and Box B's whitespace. So, the collision regions for rectangles is stable. However, the Total Region of Anger is not - consider placing a box a little to the bottom-right of the two red boxes, and removing the two Regions of Anger it would create for $T$. It would not remove the corner region. Thus, if one removes Regions of Anger pairwise, one does not need to add it as a new box and redo the whole calculation, but rather to, upon detecting whitespace infringement, add all infringers (one one side of the same box) to a list, and then add each Region of Anger obtained pairwise to the list of infringers until no new boxes are added

*If there are no infringers on some box's whitespace, of course that box can simply be removed from the union of the dilations. The analogous thing when there are some whitespace infringers is to take the union over all the boxes in infringement lists on all sides as well as all boxes with no infringers, and remove THAT from the union of dilations. This I reckon will leave only whitespace some $T$ can sit in at all, reducing the problem to calculating the nearest point in this region to the reference point.

