2
$\begingroup$

I'm trying to get the corner coordinates of a Square (NOTE, always a square) problematically. (EX: With a formula) and I'm having a hard time adding this into my computer application. Here's an illustration of what I'm trying to do:

Basically, I have the green stars, and I need the blue circles.

enter image description here

Here's what I've tried, but I got lost. I'm not that great with math.

var leftBorder = (center) - (width / 2);
var rightBorder = (center) + (width / 2);
var topBorder = (center) - (height / 2);
var bottomBorder = (center) + (height / 2);

Here's what I came up with, however it's wrong.

BOTTOM_LEFT  { X: leftBorder  || Y: topBorder - leftBorder }
TOP_LEFT     { X: leftBorder  || Y: topBorder + leftBorder }
BOTTOM_RIGHT { X: rightBorder || Y: bottomBorder - rightBorder }
TOP_RIGHT    { X: rightBorder || Y: bottomBorder + rightBorder }

I'm at a complete loss of ideas.

$\endgroup$
8
  • $\begingroup$ Are you familiar with linear algebra (specifically matrix transformations)? $\endgroup$
    – Archaick
    Jul 10, 2015 at 4:30
  • $\begingroup$ @Archaick - Unfortunately I only know very basic math, addition/subtraction multiplation/division and a few solve for x problems. I've never been good at working with grids. (Even though as a programmer I'm constantly working with a grid of pixels). I don't even know what a matrix transformation is. $\endgroup$
    – Hobbyist
    Jul 10, 2015 at 4:34
  • $\begingroup$ That's totally fine. Is it given which points are opposite (i.e., do we know which points lie on opposite edges of the square)? $\endgroup$
    – Archaick
    Jul 10, 2015 at 4:35
  • $\begingroup$ There is not a set in stone X/Y at which the the square is located. This is all done dynamically, so using any x/y coordinate for the square, and any length/width would be fine. By default my square is 64x64, sitting at x:50 y:50. Which would make the left side x:18, and the right side x:82 I believe. Same for the top and bottom. $\endgroup$
    – Hobbyist
    Jul 10, 2015 at 4:38
  • $\begingroup$ This can be moved around however, so I'm just trying to find the correct formula for calculating the four corners, granted the height/width of the square are basically X. $\endgroup$
    – Hobbyist
    Jul 10, 2015 at 4:39

3 Answers 3

1
$\begingroup$

Given that $\textbf{a}_1=(a_{1,1},a_{1,2})$, $\textbf{a}_2=(a_{2,1},a_{2,2})$, $\textbf{a}_3=(a_{3,1},a_{3,2})$, and $\textbf{a}_4=(a_{4,1},a_{4,2})$ are the mid-points of the edges of a square in the Euclidean plane, the location of the vertices of the square can be determined as follows:

Compute $r=|\textbf{a}_1-\textbf{a}_2|^2=(a_{1,1}-a_{2,1})^2+(a_{1,2}-a_{2,2})^2$ and $q=|\textbf{a}_1-\textbf{a}_3|^2=(a_{1,1}-a_{3,1})^2+(a_{1,2}-a_{3,2})^2$

If $r>q$ then the vertices (corners) of the square are

$\textbf{v}_1=\textbf{a}_3+\frac{1}{2}(\textbf{a}_1-\textbf{a}_2)$,

$\textbf{v}_2=\textbf{a}_3-\frac{1}{2}(\textbf{a}_1-\textbf{a}_2)$,

$\textbf{v}_3=\textbf{a}_4+\frac{1}{2}(\textbf{a}_1-\textbf{a}_2)$, and

$\textbf{v}_4=\textbf{a}_4-\frac{1}{2}(\textbf{a}_1-\textbf{a}_2)$.

If $q>r$, then the vertices are

$\textbf{v}_1=\textbf{a}_2+\frac{1}{2}(\textbf{a}_1-\textbf{a}_3)$,

$\textbf{v}_2=\textbf{a}_2-\frac{1}{2}(\textbf{a}_1-\textbf{a}_3)$,

$\textbf{v}_3=\textbf{a}_4+\frac{1}{2}(\textbf{a}_1-\textbf{a}_3)$, and

$\textbf{v}_3=\textbf{a}_4-\frac{1}{2}(\textbf{a}_1-\textbf{a}_3)$.

I hope that helps! Please let me know if anything is unclear here. =)

$\endgroup$
0
$\begingroup$

If the square is axis aligned, the $y$ coordinates are the $\max$ and $\min$ of the $y$ coordinates of your stars and similarly for your $x$'s. Take the four combinations and you are done. I don' read the language you are using. What is $||$ as an operator?

$\endgroup$
1
  • $\begingroup$ || is simply just a separator showing the X and Y axis and how I was calculating it, the provided example was not real code. (As this is a math board and not a programming board) however, I am using JavaScript. $\endgroup$
    – Hobbyist
    Jul 10, 2015 at 4:44
0
$\begingroup$

Is the length of the square known?
Assuming it is and taking it to be s, coordinates of centre of square to be (x,y) we have co-ordinates of the green corners:

1. left border: (x-s/2,y)
2. right border: (x+s/2,y)

from these, you can easily get the coordinates of the corners:

1. north west corner: (x-s/2,y) + (0,s/2) = (x-s/2,y+s/2)
[adding s/2 to y coordinate of centre point of left border]

2. north east corner: (x+s/2,y) + (0,s/2) = (x+s/2,y+s/2)
[adding s/2 to y coordinate of centre point of right border]

3. south west corner: (x-s/2,y) + (0,-s/2) = (x-s/2,y-s/2)
[subtracting s/2 from y coordinate of centre point of left border]

4. south east corner: (x+s/2,y) + (0,-s/2) = (x+s/2,y-s/2)
[subtracting s/2 from y coordinate of centre point of right border]

$\endgroup$
1
  • $\begingroup$ Since you know the coordinates of the green stars (I think that's what you meant in your question) you can easily find the length of side using the distance formula (distance between any two opposite mid points is equal to length of side) $\endgroup$
    – Sabhijiit
    Jul 10, 2015 at 5:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .