# Rotated Square from two co-ordinates

Anna Greene

Greenhorn

Posts: 26

posted 2 years ago

I'm trying to write a program that will draw a square from simply having two co-ordinates input. The idea is that the first click will be the first click should be the upper left corner, and the second click should be the upper right corner. From this, I should have a line equal to the distance of all four sides of the square. My calculations seem okay on paper, but it doesn't seem to be working correctly. This is the code:

I'm starting to think that my math may actually be wrong, but I need someone to tell me if that is the case.

I'm starting to think that my math may actually be wrong, but I need someone to tell me if that is the case.

Campbell Ritchie

Sheriff

Posts: 49466

64

posted 2 years ago

Forget about the code for the time being. What is the algorithm/formula, as written on paper? If you have a rectangle, you cannot define it from two corners without edditional information. But you can define a square from two corners, but only if they are opposite corners.

Anna Greene

Greenhorn

Posts: 26

posted 2 years ago

I already have a rectangle formula which takes in two points and works perfectly (first click:top left, second click: bottom right). I can see no way to ensure a perfect square from that method though, since there is nothing to ensure equality of the x and y directions, as the points are arbitrarily selected. But why do you need opposite corners to define a square? Surely, once you have defined a line, you just need to extend two lines at a 90 degree angle going in the same direction from each endpoint, then all you need to do is connect them. As far as I can tell, that limits you to two possible squares, and which one will just depend on which direction you choose to extend those lines from the endpoints. No?

Edit: Just so it's clear, the rectangle program I have made didn't need to allow for rotation, whereas the square one does. That's why it was a bit simpler.

Campbell Ritchie wrote:Forget about the code for the time being. What is the algorithm/formula, as written on paper? If you have a rectangle, you cannot define it from two corners without edditional information. But you can define a square from two corners, but only if they are opposite corners.

I already have a rectangle formula which takes in two points and works perfectly (first click:top left, second click: bottom right). I can see no way to ensure a perfect square from that method though, since there is nothing to ensure equality of the x and y directions, as the points are arbitrarily selected. But why do you need opposite corners to define a square? Surely, once you have defined a line, you just need to extend two lines at a 90 degree angle going in the same direction from each endpoint, then all you need to do is connect them. As far as I can tell, that limits you to two possible squares, and which one will just depend on which direction you choose to extend those lines from the endpoints. No?

Edit: Just so it's clear, the rectangle program I have made didn't need to allow for rotation, whereas the square one does. That's why it was a bit simpler.

Campbell Ritchie

Sheriff

Posts: 49466

64

posted 2 years ago

Let us imagine that you have two points, (25, 125) and (175, 201). Assume those points are opposite corners of a square. Now you have a diagonal, and you can draw a square on that diagonal. Draw those points on paper, draw the diagonal, and then draw the square. Obviously it will not have horizontal and vertical sides. See if you can work out the other two corners:

HINT x₁ + x₃ = x₂ + x₄ and y₁ + y₃ = y₂ + y₄ where corners 1 and 3 are opposite each other.

Note that you might be one pixel out if you use integer arithmetic because integer arithmetic does not support ½.

- 1

As you said, one to the left and one to the right, so that requires additional information. You can draw an ordinary rectangle on a diagonal if you have additional information, in the case you describe that the sides are vertical/horizontal.Anna Greene wrote: . . . Surely, once you have defined a line, you just need to extend two lines at a 90 degree angle going in the same direction . . .

Let us imagine that you have two points, (25, 125) and (175, 201). Assume those points are opposite corners of a square. Now you have a diagonal, and you can draw a square on that diagonal. Draw those points on paper, draw the diagonal, and then draw the square. Obviously it will not have horizontal and vertical sides. See if you can work out the other two corners:

HINT x₁ + x₃ = x₂ + x₄ and y₁ + y₃ = y₂ + y₄ where corners 1 and 3 are opposite each other.

Note that you might be one pixel out if you use integer arithmetic because integer arithmetic does not support ½.

Piet Souris

Rancher

Posts: 1297

28

posted 2 years ago

hi Anna,

after you have solved Campbells question (nice guy, isn't he? Setting you up with extra questions

instead of giving you a solution; but, alas, that's the risk of visiting this site),

please have a look at your original listing.

There are some nasties in it. First of all, indeed, there are two possible squares when you know

one of the sides, and indeed, if you determine a fixed direction, then you end up with one

square. The problems I see with this (well, if you consider them as problems, that is), is

that depending on where the two clicks appear, your square may not fit in your panel.

And is your direction easy to determine if, say, the first click is right and bottom of the second click?

Then: your maths are, indeed, incorrect. You can see that if you draw a rotated square on a piece of

paper. You correctly calculate the length of the side. But does that mean that x4 = x1 + length,

and y4 = y1 + length?

Greetz,

Piet

after you have solved Campbells question (nice guy, isn't he? Setting you up with extra questions

instead of giving you a solution; but, alas, that's the risk of visiting this site),

please have a look at your original listing.

There are some nasties in it. First of all, indeed, there are two possible squares when you know

one of the sides, and indeed, if you determine a fixed direction, then you end up with one

square. The problems I see with this (well, if you consider them as problems, that is), is

that depending on where the two clicks appear, your square may not fit in your panel.

And is your direction easy to determine if, say, the first click is right and bottom of the second click?

Then: your maths are, indeed, incorrect. You can see that if you draw a rotated square on a piece of

paper. You correctly calculate the length of the side. But does that mean that x4 = x1 + length,

and y4 = y1 + length?

Greetz,

Piet

Campbell Ritchie

Sheriff

Posts: 49466

64

Campbell Ritchie

Sheriff

Posts: 49466

64

posted 2 years ago

If you work it out for yourself, you will know much better how to handle the next difficult case like this.

I still think it is easier to work out the locations of the corners than the lengths of the sides.

Flattery like that will get you anywhere!Piet Souris wrote: . . . nice guy, isn't he?

If I tell you what to do, you will get it right this time and have no idea what to do next time you encounter such a problem.Setting you up with extra questions

instead of giving you a solution; but, alas, that's the risk of visiting this site) . . .

If you work it out for yourself, you will know much better how to handle the next difficult case like this.

I still think it is easier to work out the locations of the corners than the lengths of the sides.

Piet Souris

Rancher

Posts: 1297

28

posted 2 years ago

I meant it as a compliment. Certainly liked the questions. I'll use emoticons next time!

Campbell Ritchie wrote:If I tell you what to do, you will get it right this time and have no idea what to do next time you encounter such a problem.

If you work it out for yourself, you will know much better how to handle the next difficult case like this.

.

I meant it as a compliment. Certainly liked the questions. I'll use emoticons next time!

Campbell Ritchie

Sheriff

Posts: 49466

64

Anna Greene

Greenhorn

Posts: 26

posted 2 years ago

And it's a damn good policy too. Finding practical applications for basic geometry formulae is one of my favourite parts of programming. Nevertheless, the first reply only made sense to me when I realised that for a line of any possible length, there exists a perfect square which has each endpoint of that line as two of its corners. I didn't actually realise that, though it's fairly intuitive when you think about it.

I found lots of interesting things when googling this problem. The folks on Math.stackexchange in particular had some lovely solutions using complex numbers and linear algebra, though I personally just stuck with algebraic manipulation for the sake of this project.

I found lots of interesting things when googling this problem. The folks on Math.stackexchange in particular had some lovely solutions using complex numbers and linear algebra, though I personally just stuck with algebraic manipulation for the sake of this project.

Piet Souris

Rancher

Posts: 1297

28

posted 2 years ago

hi Anna,

yes, its a lovely hobby. Though, having watched 'Strictly Come Dancing' at the BBC

this evening, I must admit to find Ballroom Dancing even more lovely!

Your original topic stated constructing a square, given one side. That is easier to

solve than starting from a given diagonal. Linear algebra certainly helps here,

but complex numbers? Sounds like using a canon to shoot a musquito.

Anyway, have you managed to solve your squares, starting from a side or a diagonal?

Greetings,

Piet

yes, its a lovely hobby. Though, having watched 'Strictly Come Dancing' at the BBC

this evening, I must admit to find Ballroom Dancing even more lovely!

Your original topic stated constructing a square, given one side. That is easier to

solve than starting from a given diagonal. Linear algebra certainly helps here,

but complex numbers? Sounds like using a canon to shoot a musquito.

Anyway, have you managed to solve your squares, starting from a side or a diagonal?

Greetings,

Piet

Campbell Ritchie

Sheriff

Posts: 49466

64

posted 2 years ago

So what formula did you use to find the other two corners from a diagonal?

That might be nice to show off one's mathematical skills, but for programming one needs simple code which can be maintained, and “dumb code” can more easily be optimised.Anna Greene wrote: . . . The folks on Math.stackexchange in particular had some lovely solutions using complex numbers and linear algebra, . . .

So what formula did you use to find the other two corners from a diagonal?

Anna Greene

Greenhorn

Posts: 26

posted 2 years ago

This is the one I used in pseudocode:

I can't remember what the project I was doing this for is called, so I can't find my exact translation, but that's pretty straightforward to convert into java.

I can't remember what the project I was doing this for is called, so I can't find my exact translation, but that's pretty straightforward to convert into java.