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 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.

- 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 ½.

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

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.

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!

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.

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

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?

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |