Calculate opposite points in Square from diagonal.

geometrytrigonometry

I see the following solution from another link, but can't get my head around it as to why this works.

Especially , with square sides not parallel to x/y axis. Like why subtracting/adding half the diagonal length always gives correct third and fourth points.

Can someone please explain.

Link:
Given two diagonally opposite points on a square, how to calculate the other two points

  x1 = ?  ;  y1 = ? ;    // First diagonal point
  x2 = ?  ;  y2 = ? ;    // Second diagonal point

  xc = (x1 + x2)/2  ;  yc = (y1 + y2)/2  ;    // Center point
  xd = (x1 - x2)/2  ;  yd = (y1 - y2)/2  ;    // Half-diagonal

  x3 = xc - yd  ;  y3 = yc + xd;    // Third corner
  x4 = xc + yd  ;  y4 = yc - xd;    // Fourth corner

Best Answer

So we have two points: $A(x_1,y_1)$ and $B(x_2,y_2)$. The first step is to find midpoint $M(x_c,y_c)$. This is straightforward: $x_c=\frac{x_1+x_2}{2}, y_c=\frac{y_1+y_2}{2}$

The next step is to move the origin to point $M$. Our points $A$ and $B$ will have new coordinates: $A'(x_1-x_c,y_1-y_c)$ and $B'(x_2-x_c,y_2-y_c)$.

Finally, we rotate $A'B'$ about the origin $90$ degrees to get the other two vertices. The rotated points will be $A''(y_c-y_1, x_1-x_c)$ and $B''(y_c-y_2, x_2-x_c)$. Moving the origin back to the original location will give us coordinates of the vertices: $C(y_c-y_1+x_c, x_1-x_c+y_c)$ and $D(y_c-y_2+x_c, x_2-x_c+y_c)$. We don't really need to calculate half-diagonal but it's easy to see that $y_c-y_1=-y_d$ and so forth.