Distance from a point to a rectangle

Function
This script computes the distance between a point and a rectangle. It's a 2D computation so it's assumed that the point and rectangle lie in a plane. If the point is inside the rectangle, the distance returned is 0.

Background
This is part of a larger framework I worked on called the Cygnet Engine. It's a math function, so I put it in a class called CygnetMath. I may release other parts of Cygnet in the future, but for now, this is the only part.

Usage
The class is designed for use in Unity's GUI coordinates. With this coordinate system, the origin is at the top left of the screen, x values increase from left to right, and y values increase as you go DOWN the screen. If you do some careful coordinate transformations, you probably should be able to use the code with an arbitrary coordinate system, but this was not the usage I designed it for, so beware.

Operation
There are several ways to compute the distance. This one determines if the point is either (a) within the rectangle (b) closest to an edge (c) closest to a corner. This is done by splitting the area around the rectangle into 9 regions, and doing a few branches. It may not be the most efficient way. If not, let me know or edit this page.

Original Author
Philip Peterson (i.e. who to send bug reports to)

Code
It's written in C#. Sorry.

License
This code is released under the MIT license. Please provide attribution to the original author, Philip Peterson.