In the image below I have a rectangle (with corners 1-4). I have a blue point. I'm trying to find which side is closest to this point. Finding one of the corners (that make up the side) seems easy. As far as I can tell, the closest point must necessarily be one of the two points that make up the side.
The second corner... not so much. Given the first point, the second obviously has to be one of the adjacent ones. What I'm requesting help with is a means to determine which direction I must rotate to get the second point. I expect there is some relationship between the corner and the point that I'm simply not seeing.
The use case here is for a computer program, but it feels like there must be a mathematical solution. I apologize if this is is the incorrect place for this.
There is actually no need to find the closest corner. By drawing(and extending infinitely far) the diagonals of the rectangle, you can split the screen(assuming rectangle and point are both on the screen) into areas where two points in the same area are closest to the same side. This is probably easier seen with a visualization:
Any point in the top region is closest to the top side, any point in right region is closest to the right side, etc. It can be proved by some pretty basic geometry that this is the case, but it should be intuitively obvious.
Here is some pseudocode to determine which side is the closest. The pseudocode assumes that the rectangle is centered at $(0, 0)$, the point lies outside the rectangle, and rectangle's sides are parallel to the axes(the geometry above does not assume the last point):
Some slight modifications could be made if the point is inside the rectangle.