## A square described in any rotational orientation with 1 equation

What started out as an innocent question meant to poke me became an interesting math problem to ponder. You might want to read the original question and the answers presented. The final question was, can a square be described in any rotational orientation (based on the 2D Cartesian plane) with just 1 equation?

The answer is a resounding YES.

Meet Roie, who proposed this solution:

maximum {|r cos(theta-theta_0)|,|r sin(theta-theta_0)|} = c

This is basically modified from the max-abs solution by Mike Anderson:

max(abs(x),abs(y)) = c

You move from Cartesian coordinates to polar coordinates. Once you’re in polar plane, you can rotate by changing one variable (the angle). I actually wrote something about converting between Cartesian and polar coordinates for the use of image rotations. I can’t believe I forgot about that…

Roie also gave sample (GNU Octave or MATLAB) code:

```theta0 = 0.5;
x = ones(201,1) * (-10:0.1:10);
y = x’;
z = max(abs(sqrt(x.^2+y.^2) .* cos(atan(y./x)-theta0)),abs(sqrt(x.^2+y.^2) .* sin(atan(y./x)-theta0)));
contour(z)
axis square
```

Unfortunately, I am unable to verify that. Fortunately, Cees Meijer confirmed it for me. He introduced me to FreeMat, a free open source software that works like MATLAB. Unfortunately, I cannot install the Windows version because I need an x64 executable (I’m on Windows 7 64-bit), and FreeMat currently only has x86 version (32-bit).

Oh well, if it works, then I can finally do matrix multiplications in one line of code. So there, Will.

## Answered: Can you describe a square with 1 equation?

First, I want to say how humbled I am by the people who read this blog. You are awesome.

You might want to read the original post where the question came from. So, can you describe a square with only 1 equation? 3 readers gave their opinions, and they are all correct (meaning they’re smarter than me). I will present their answers first, then present my engineer friend’s answer, and then mine (which is worthless, and didn’t work out the way I wanted).

The first answer was from Mike Anderson, who gave this:

max(abs(x),abs(y)) = c

That actually took me a while to understand. And is actually the more complicated answer. But it’s exactly the answer my friend was looking for, which is an upright square. We’ll assume the centre of the square to be the origin. The absolute function reduces the working Cartesian plane to just the first quadrant (if it works in the first quadrant, it works in the other quadrants). The maximum function ensures that either the x-value or y-value (or both) is equal to c (half the width of the square).

What if the absolute function is not in action? Then if -c < x < c, then y must be either -c or c (because of the maximum function). Therefore there are 2 horizontal parallel lines. Similarly vice versa, creating 2 vertical parallel lines. And there’s your square. This is brilliant.

The second answer was from Christopher Tay, who gave this:

|x| + |y| = C

which is the L1 norm.

The norm of a matrix is the magnitude of the matrix. In this case, the matrix is actually a 2 dimensional vector. The pipe character | surrounding the x and y values mean we’re taking the absolute value.

This one is interesting. It means that the larger the value of x, the smaller y has to be to compensate, so that the sum of x and y is C. This see-sawing effect created a triangular shape for each quadrant. Which when formed together, becomes a square (tilt your head 45 degrees to see it better).

Then Chris McAloney came in with this:

abs(x) + abs(y) = c

This equation is equivalent to the L1 norm form. Chris also pointed out that the squares formed by the previous 2 equations differ from each other by a rotation of PI/4 (radians) or 45 degrees.

So there are 2 solutions! My engineer friend was shocked when I told him. I had to admit, I was gloating just a little when I told him my readers solved his problem…

One small point to note. The constant c in each equation is specific to each equation. As in if you supplied the same constant to each equation, the width of the squares formed are different. Let me illustrate:

The square formed by the max-abs way has a width of 2c.

The square formed by the L1 norm way has a width of sqrt(2)*c.

### The engineer’s answer

So what’s my friend’s answer? He said the answer might anger some mathematicians. He also said the answer might demonstrate a fundamental difference between how engineers and mathematicians think. And his answer is…

x^10000 + y^10000 = 1

Or basically x and y to the power of a sufficiently large number. The result is not exactly a square, but it looks sufficiently like a square.

And that was his point. That engineers take “good enough” and practicality as priority. If a wooden beam is in the correct position, and is supporting the weight of the roof, who cares if it’s 1 nanometre off the intended position?

How did he come up with it? He was playing with Wolfram Alpha and was just dumping equations into it… To see what happens, start with

x^2 + y^2 = 1

That’s a circle with radius 1. Then increase the power to 10

x^10 + y^10 = 1

You’ll see a square with round edges. Actually x^100 + y^100 = 1 gives a nice square plot. To my friend, that’s a good enough square. He posed the question to me for an analytical answer, because even he knows his answer wasn’t exact.

### And my (worthless excuse of an) answer?

I hit upon the idea of starting with a circle. Then morphing it somehow into a square. The idea came from a super equation that can describe a cube or sphere or some other 3D object. I can’t find it anymore… I saw the equation when I was doing research on the demoscene. You vary a parameter t, and you get different 3D objects. That’s neat.

So I started with this:

(r * sin(θ))^2 + (r * cos(θ))^2 = r^2

Yup, I used polar coordinates. I was thinking of somehow wrangling the sines and cosines with ceiling and floor and rounding functions. I was trying to force something like:

ceiling(cos(θ)) * (sin(θ))^2 [<- NOOO, THIS IS WRONG]

to work… That was for y. I had a correspondingly elaborate term for x.

Then it dawned on me.

How am I going to draw a square in the polar coordinate plane when I basically only had theta to work with? Everything will be circular.

I tore my math working off my writing pad and threw it down the rubbish chute. I might also have sworn vehemently. Can’t remember…

So there you have it, 2 equations to describing a square. And 1 equation that creates a result that looks like a square, which for most purposes and intents may be regarded as a square.

Then my friend came in with the last word:

“For bonus points, can you use some trig functions to tilt it back so that the equation can have any angle?”

We have 1 upright square, and 1 square that’s tilted 45 degrees. My friend wanted to know if there’s an equation that creates a square in any rotational orientation. I don’t believe there’s one, so please don’t bother to try. It’ll just waste your brain cells. But you’re welcome to try it as an intellectual exercise.

Me? I need to get another writing pad…

## Quiz: Can you describe a square with 1 equation?

I was hanging out with my friends, and somehow the topics wandered into something that prompted a mischievous grin from one of the guys.

“Eh, I have a maths problem for you.” He could probably blind someone with that playful twinkle in his eye.

Can you describe a square with just 1 equation?

“What?” My mind was already racing to solve the problem.

“Just take it as a square in the 2D Cartesian plane. The centre of the square will be at the origin (0,0) to simplify the equation.” He seemed delighted to have stumped me.

I asked if there were any boundary conditions. No. Was the equation elegant? As in fairly simple when looked at, no mangling of terms. He gave it a second, and… yes, it was elegant.

Then he said the most important piece of information so far. “The answer might anger mathematicians. It is probably one of the fundamental differences between an engineer and a mathematician.”

Our conversations left this topic. But my brain was subconsciously still working on it. Then I blurted, “I think I solved it.” I described my way of thinking, and he said that’s along the correct line of thought. Then he just gave me the answer.

Yes, I agree. The answer will probably ruffle the feathers of the math purists.

As of this writing, I have yet to come up with the analytical formula of an equation to describe a square. I will post my findings, and my friend’s answer in a week’s time. You are encouraged to come up with your own solution. Post in the comments. Better yet, write about it in your blog. Tell your friends about it. Show them how awesomely clever you are.

Can you describe a square with 1 equation?

Bonus fun: My friend tried to explain a bit more. “There’s only one equal sign.” I think he was trying to insult me or something…

Update: Find out the answer.

## Squares, hexagons and distance

Most traditional board games use squares to segregate space.

Space is divided evenly. Lines are easy to draw. Everything is structured. Bliss.

Except that when you need to move to a diagonal square, you need to move 2 squares instead of √2 squares. Wait, a non-integer movement? That cannot be tracked!

Dungeons & Dragons 4th Edition (a tabletop RPG with physical positional tracking) state that a diagonal square movement costs the same as a perpendicular square movement. Meaning you just move 1 square to reach that diagonal square. This also has its problems.

For example, there’s a concept of push in D&D, where as long as the target being pushed is moved further away, it counts as a push. Bringing us to this situation:

I talked about this briefly on my other blog, but didn’t go into the math details. So the blue dot is you, the red dot is the enemy, and the brown dot is where the enemy is pushed to.

The distance between you and the enemy is (do the Pythagoras thing) 2√2 (approx 2.8284). The distance between you and the final position of the enemy is √10 (approx 3.1623). Sure √10 is greater than 2√2, so mathematically speaking, the enemy is moving away from you. But common sense is telling me otherwise, because the direction of the push emanates from you.

Anyway, to combat the shortcomings of the square terrain, there’s the hexagonal terrain.

Under this division of space, all adjacent spots are equidistant to your position. Well, it still has its problems. You still need 2 hexagons of movement to reach the first hexagon directly above you.

So what’s the actual cost of movement? Let’s look at this extracted diagram:

Let h be half of the actual distance. Doing the Pythagoras thing again, we have
1^2 = h^2 + (1/2)^2
=> 1 = h^2 + 1/4
=> h^2 = 3/4
=> h = √3/2 (h is positive)

Therefore, the actual distance is 2h which is √3 (approx 1.7321). Not quite 2 hexagons, is it?

This is part of the reason why, when games are created on computers, that these limitations disappear. Because computers can do the distance calculations and tracking. You can move in any direction, for any amount of units of movement, as long as you do not hit anything.

And that is called collision detection.