## Regular polygon equation

A while ago, a blog reader named BJ (he seems to prefer being called BJ. He? *checks email…* Yeah, he) emailed me with his answer to this question: Is there an equation to describe regular polygons?

I’m not clever enough to do much editing and explanation, so I’ll post his email (got his permission and clarified some points) here.

*Start email quote*

An example: A polygon equation can be approximated by a single continuous implicit equation. Suppose x*y=0.1 The concept is to think of this hyperbola as almost being the product of two lines intersecting at x=0 and y=0.

Construct a (3,4,5) right triangle like this: Begin with the multiplication (x – 1)(y + 1) = 0.1. This product is the first approximated vertex. Multiply a second time using (3y – 4x – 5). This creates two more curved vertices. The multiplication expression now has three factors on the LHS. The RHS remains 0.1. The final equation will form a figure with the approximated triangle being a central “island”. Below is an equation for a (3,4,5) right triangle that morphs from intersecting lines to the triangle and then on to the circle. I sandwiched the approximated triangle between two circles, and it becomes one circle in the limit as 0.1 -> 0.

((x + 0.5)^2 + (y – 1)^2 – 6.125) – ((x + 0.5)^2 + (y – 1)^2 – 6.125)(3y – 4x – 5)(x – 1)(y + 1) = 0.1

Notice that I have multiplied by a circle in the last term. I sandwiched the approximated triangle between two circles, and it becomes one circle in the limit as .1 -> 0. The last term is subtracted from the circle that in the first term. The multiplication expression goes to zero, and only the circle remains. This method is somewhat analogous to the method used in Euclidean geometry.

*End email quote*

He also sent a follow-up email:

*Start email quote*

Here is a quadratic form for the triangle [(0,0),(4,0),(4,3)].
(x – 4) y (y – 0.75x) = 0.01

Solve for y=f(x) on the range 0 <= x <= 4 y = (15x^2 - 60x + sqrt(225x^4 - 1800x^3 + 3600x^2 + 16x - 64)) / (40x - 160) y = (15x^2 - 60x - sqrt(225x^4 - 1800x^3 + 3600x^2 + 16x - 64)) / (40x - 160) Plot using two colors, one for each solution. *End email quote* You can add your thoughts on this in the comments.

### Story time

I don’t have much else to add to that, so I’ll just tell you a story instead.

Back when I was in university, there was a programming problem that’s to calculate the value of PI. There were 2 methods involved.

The first method used Monte Carlo simulation. Basically you have a circle with radius 1 unit. So the diameter is 2 units. And you have a square that just contains this circle, so the square is 2 units wide by 2 units high.

The area of the square is 2*2 = 4 units. The area of the circle is given by PI*r*r which is PI (because r is 1 unit). And the ratio of area of circle to area of square is PI/4.

Using Monte Carlo simulation, I randomly selected a point within the square. I made a note of whether the point was within the circle or without. And

[Number of points within circle]/[Total number of points] = PI/4

Since PI is the only “unknown”, there you have it. Solve for PI. The more points you use, the more accurate PI is.

The other method involves calculating the circumference of a circle. Suppose you have a square with a width of square root 2. This is chosen such that the diagonal length of the square is 2 units. This means the distance from the centre of the square to the corner point of the square (any of the 4 of them) is 1 unit.

Are you getting the idea yet?

The perimeter of this square is 4 * (square root of 2). Then we double the number of sides so we get an 8-sided polygon, an octagon. But still keeping the “from centre to outer-most point is 1 unit length” condition. Using some more maths, we get the length of one side of this octagon and multiply it by 8. That will be the perimeter.

As we keep doubling the number of sides, this polygon eventually approximates a circle. And so the perimeter of said (regular) polygon approaches the circumference of a circle, which is 2*PI*r, or just 2*PI (because r is 1 unit). Solve for PI.

### Exercise

To end this, I will leave it to you as an exercise to calculate the length of that regular polygon in the 2nd method. Start by understanding why one side of a square is square root of 2. Then continue with calculating the side length for an octagon.

Side note: The 2nd method terminates faster as an iterative process than the Monte Carlo simulation. However, it is also less “stable”. Hahaha… for extra credit, explain why it is less “stable”.

Extra side note: If people knew the formula for a circle is PI*r*r, wouldn’t they have known the value of PI already? This was why I found the Monte Carlo simulation method a little on the self-fulfilling side. You’re solving for something that you sort of know the value of. That’s weird, almost like cheating.

## Is there an equation to describe regular polygons?

So a blog reader, Michael Gmirkin, sent me an in-depth email about the possibility of the existence of a super equation that can describe any regular polygon. I wasn’t sure. For reference, you might want to check out these 2 blog posts about the equation for a square: question, answer.

I was going to just ask you here. Then I remembered there’s a Stack Overflow equivalent for maths. So I went there and asked the question. So if you know the answer, you can comment here, or go to the maths StackExchange site and earn yourself some points.

You can assume that the centre of the regular polygon in at the origin (0,0). Researching a little on the topic, I also learnt about the apothem, which is also the shortest distance from the centre to a polygon’s side. The “normal” radius is the distance from the centre to one of the regular polygon’s vertex.

If you trace 2 circles, one with the apothem and one with the radius, you get an inscribed circle and circumscribed circle respectively.

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

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…