Given a corner, draw a square of known size minimizing vertex distance to a point

117 Views Asked by At

I've been going a little crazy trying to solve this what I think is a simple problem.

Given:

  • A known corner ($\sim 90^\circ$) as defined by $\angle ABC$. Segment $AB$ is defined by two points on $AB$ with coordinates $(x_1, y_1)$ and $(x_2, y_2)$. Segment $BC$ is defined by two points on $BC$ with coordinates $(x_3, y_3)$ and $(x_4, y_4)$.
  • length of the green square is $S$
  • An arbitrary point $P$ with coordinates $(x, y)$.
  • Have access to matrix library if that significantly can simplify the problem.
  • four corners of green square are called $A$, $D$, $E$, $F$.

Want:

  • Calculate the coordinates of the four corners ($A$, $D$, $E$, $F$) of the square, minimizing the sum of distances of each vertex to point $(x, y)$. i.e., $\text{minimize} (PA+PD+PE+PF)$

diagram

Green square is what I want, red square would not be.

What I've thought: To solve this, although this method seems horribly inefficient, I propose the following:

  1. Take points AB and compute the angle formed.
  2. Compute the point distance S away from point B along step 1 angle and along (step 1: $\text{angle} + 180^\circ$)
  3. Select which point is closer to the arbitrary point.
  4. Repeat the steps then for points BC, which after this step I should have 3 points of the square.

What I'm wondering is if there is some way instead to perhaps rotate the coordinate frame so that the angle is aligned to cartesian coordinates (and the arbitrary point $(X, Y)$, compute the square, rotate all coordinates then back? This is similar to what I found here. I see they converted everything to polar coordinates, but not 100% sure.