[Math] Given two diagonally opposite points on a square, how to calculate the other two points

geometrytrigonometry

I'm not a mathematician, just a programmer working on a (pro bono) job with a bit of geometry involved.

Basically I have two points A(a,b) and B(c,d) (For what it's worth, these are geographical coordinates.)

I have two scenarios to work out. In the first scenario, these two points are opposite vertices of a square, and I need to work out the coordinates of the other two vertices.

I thought I was onto something with this post: Given coordinates of hypotenuse, how can I calculate coordinates of other vertex? which seems to be in the right ballpark, but I am stuck at the step where you need to subtract two linear equations. However, in my case the lengths of the sides are equal, so it should be easier?

In my second scenario, the two points are centrepoints on the opposite sides of a square (so that the distance AB is also the length of each side of the square) and from this I need to derive the coordinates of all four sides.

I'm pretty confident that if I can work out scenario 1, I can also work out scenario 2, but any advice on either would be gratefully received.

Added Later:
Thanks very much for your help. For information, here is how I tackled scenario 2 (just in case anyone ever wants to reproduce something similar.) This could probably be tidied up, but will work for the moment. (Haven't checked this code, just transcribed it from my program. It's C#.)

public List<Point> GetVertices(double x1, double y1, double x2, double y2)
{
// x1,y1 are point A coordinates, x2,y2 are point B coordinates
// Centrepoint calculation
var cX = (x1 + x2) / 2;
var cY = (y1 + y2) / 2;

// Length of Side
var pLen = Math.Sqrt(((y2 - y1) * (y2 - y1)) + ((x2 - x1) * (x2 - x1)))

// Length of Diagonal
var diagLen = Math.Sqrt(pLen*pLen + pLen*pLen);

// Ratio of Diagonal to Side
var ratio = diagLen / pLen;

// Now use centrepoint and one of the existing points to find a new vertex
var newX = cX + ratio*(x2 - cX);
var newY = cY + ratio*(y2 - cY);

// Make some points for rotation
var pCentre = new Point(cX, cY);
var pNew = new Point(newX, newY);

var vertices = new List<Point>();
// Now rotate the points
var p1 = RotatePoint(pNew, pCentre, 45);
vertices.Add(p1);
var p2 = RotatePoint(p1, pCentre, 90);
vertices.Add(p2);
var p3 = RotatePoint(p2, pCentre, 90);
vertices.Add(p3);
var p4 = RotatePoint(p3, pCentre, 90);
vertices.Add(p4);

return vertices;

}

private Point RotatePoint(Point rotater, Point centre, double angleInDegrees)
{
        double angleInRadians = angleInDegrees * (Math.PI / 180);
        double cosTheta = Math.Cos(angleInRadians);
        double sinTheta = Math.Sin(angleInRadians);
        return new Point
        {
            X = 

                (cosTheta * (rotater.Longitude - centre.Longitude) -
                sinTheta * (rotater.Latitude - centre.Latitude) + centre.Longitude),
            Y = 

                (sinTheta * (rotater.Longitude - centre.Longitude) +
                cosTheta * (rotater.Latitude - centre.Latitude) + centre.Latitude)
        };

}

Best Answer

Here is the code for the first problem:

  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
Related Question