## Dissecting Trigonometric Particles part 2 – Axis functions

This is a continuation of the explanation behind Trigonometric Particles. Read up on part 1 if you haven’t done so.

Actually, there isn’t a clever coding construct I used to implement the axis functions. I just created a function that does a combination of polynomials and trigonometry functions. It looks something like this:

```float function AxisFunction(float t, float p0, float p1, float p2, float p3, float p4, float p5, float p6)
{
float result = 0.0;
result += p0 + p1*t + p2*t*t + p3*t*t*t;
result += p4*sin(t) + p5*cos(t) + p6*tan(t);

return result;
}
```

The variable t is the time elapsed. The first part of the function is basically a cubic polynomial. The second part is a sum of the 3 standard trigonometric functions. Basically, I’m just passing in parameters which are the coefficients of the respective terms.

To simulate a sphere, since I can’t quite summon the mental energy to switch between my preferred Y-axis-pointing-skywards and the more widely known Z-axis-pointing-skywards coordinate system, I’ll just use the more famous version to illustrate. So the axes are:
x = r * sin(theta) * cos(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(theta)

To calculate the X, Y, Z coordinates, I just use different combinations of AxisFunction(). So
x = AxisFunction(t, 0,0,0,0, r,0,0) * AxisFunction(t/2, 0,0,0,0, 0,1,0)
y = AxisFunction(t, 0,0,0,0, r,0,0) * AxisFunction(t/2, 0,0,0,0, 1,0,0)
z = AxisFunction(t, 0,0,0,0, 0,r,0)

I’m passing t/2 for a different-valued phi. I can’t remember the exact multiple of t I used… so I’m just using t/2 as an example. For the sphere simulation, I was playing around with t to get the particles to swirl and end roughly near the top of the sphere. Took me a while to figure out the right magic number…

To simulate a cylinder, just set polar coordinates on X and Y, then use Z as the height.
x = AxisFunction(t, 0,0,0,0, 0,r,0)
y = AxisFunction(t, 0,0,0,0, r,0,0)
z = AxisFunction(t, 0,H,0,0, 0,0,0)
where H corresponds to the speed value you want the particles to “climb” the cylinder. Again, much time taken to figure out the right magic number…

As for the tornado simulation, notice that it’s similar to that of a cylinder, and the particles circle with a wider radius as they climb the cylinder. So the radius is now a function of the height.

Actually, we just need the radius to increase as t increases, not necessarily as a function of height. So we apply a linear function to r.
x = AF(t, 0,0,0,0, 0, r * AF(t, 0,L,0,0, 0,0,0), 0)
y = AF(t, 0,0,0,0, r * AF(t, 0,L,0,0, 0,0,0), 0,0)
z = AF(t, 0,H,0,0, 0,0,0)
where L is some magic number such that the radius increases in a reasonable manner proportionate to the time elapsed (and effectively proportionate to the height).

I have a confession to make. When I first introduced Trigonometric Particles, I said I used a W axis as well as composite functions. Well I truly remembered having a W axis, and I think I used it as such:
x = W * AF(…)
or maybe
x = AF( … W, …)

But I’m not using W in my explanations above. Hmm… somehow while writing the explanations, the W axis wasn’t required.

As for composite functions, a short description. H is a composite function when
H(x) = F(G(x))
meaning you calculate G(x) first, then calculate function F using G(x). That’s what we’re doing when we passed in one form of AxisFunction() to another AxisFunction() as a parameter.

Well, my memory being foggy, I seem to recall having both W axis and the use of composite functions. It appears one can simulate the results with one or the other. Oh well, no one’s perfect…

And the last simulation pattern, the sun’s surface? I think I used a pure polynomial. Quadratic, I think…

## The math of swing doors

Have you seen those bidirectional swing doors (as I’ll call them)?

They’re usually transparent, made of glass, and used in clinics, fast food restaurants, offices and department stores. Something weird happens when two people arrive at the swing doors on opposite sides at the same time. Have you ever walked along a street and happened to nearly bump into another person, and both of you were trying to figure out which way the other was going to go?

“Is he going to move to his left? Then I’ll move to my left. Wait! He’s moving in the same direction as me! Ok, I’ll go to my right. What?! He’s moving to my right too!”

I usually just stop, step to one side and let the other person choose a direction, and both of us will be on our merry way. Why are we talking about this again? Oh right, swing doors.

So this stranger Bob is on the outside and I’m on the inside. Bob’s thinking if he should quickly push the door on his side and come in, but is scared of knocking me. I’m thinking if I should quickly pull the door on my side and go out, but is scared of him knocking me. It’s a dilemma, I tell ya.

So what do I do? Being the gentleman that I am, I pull the door open and I stay on the inside, silently beckoning Bob to come in. I don’t mind being a doorman for a while. Besides, I get to train my bulging biceps from pulling the door and holding it open.

Apparently, Bob is taken aback at being treated like a hotel guest. And stays shocked still for a couple of seconds. Could be stunned by the sight of my bulging biceps though, I’m not sure…

At this point, I have to digress and talk about the finer points of opening a swing door. To open a swing door with the least effort, you should use your full body weight to help. This can be done by fully leaning onto the door and push. Or you can straighten your arms, locking at the elbow and pull.

For some reason, people like to push. I also noted that few people actually straighten arms, whether it’s pushing or pulling. Maybe it doesn’t look natural? Or it looks funny?

Anyway, I attempted to fathom this, and concluded with 3 reasons. Based on the embarrassment factor, you’d probably bend your arm instead of straightening it. Pushing involves more of the triceps, whereas pulling involves more of the biceps. Since triceps are a larger muscle group than biceps, your triceps are probably stronger than your biceps. Hence pushing is easier. Doctors and physiologists, please feel free to correct me. That’s the first reason.

The second one is sight. You can’t see behind you, and you might knock someone over if you pull without looking back. The third is momentum. You’re striding towards the swing door, and you don’t want to stop and pull. The natural flow is to push the door.

So what does Bob do? He pushes the door.

No, no, no, no, no, no, NO! And I’m going to mathematically prove to you why that’s a bad decision.

First, let’s define the door gap distance as the shortest distance between the tips of both doors. So when both doors are closed, the door gap distance is zero. So opening swing doors means maximising the door gap distance so you can pass through. And what’s the shortest path between two points? A straight line.

Let’s consider the case where I open the door on my side 90 degrees. Let the doors each be 1 unit wide. Thus we have the following diagram.

What’s the door gap distance in this case? Using Pythagoras’ Theorem, d = sqrt(2).

What’s the angle if you open the other door such that the door gap distance is the smallest? Remember the shortest path? There should be a straight line.

So using Pythagoras’ Theorem again, we have
d = (length of hypotenuse) – 1 = sqrt(2^2 + 1^1) – 1
= sqrt(5) – 1
This is less than sqrt(2), which is when the door was closed.

The angle theta is calculated by taking the inverse tangent of 1/2. At this point, I have to digress into some basic trigonometry in case you can’t follow.

The hypotenuse is the side opposite the right angle in a right-angled triangle. The opposite and adjacent sides depend on the angle you’re looking at. And now, I’m going to give you a mnemonic.

toa cah soh

In the Chinese dialect, Hokkien, it translates to “big foot lady” or some such. At least, that’s how my teacher taught me to remember. So “toa” is tangent equals opposite over adjacent. “cah” is cosine equals to adjacent over hypotenuse. And “soh” is sine equals to opposite over hypotenuse.

What’s that got to do with the inverse tangent? Well,
tan(theta) = (opposite) / (adjacent) = 1 / 2
Therefore, theta = inverse tangent of 1/2, which is roughly 26.5651 degrees.

Notice that at this angle, the door gap distance is smaller than when the door was closed (that is, the angle is zero). This means opening the door actually shrunk the door gap distance. What were we trying to do with opening the door? Maximising the door gap distance. Peculiar, isn’t it?

Which leads me to the next question. At what angle would we have the door gap distance to at least be as when we left the door closed?

You’ll have to study the diagram carefully for the following discussion. From the sides formed by p, q and d, we have
p^2 + q^2 = 2 (Pythagoras’ Theorem)

We also have
p + r = 2 (convince yourself of this by inspection)

We also have
sin(theta) = (opp) / (hyp) = (1-q) / 1 = 1-q

We also have
cos(theta) = (adj) / (hyp) = r/1 = r

From a trigonometry property (I can’t remember what it’s called), the sum of squares of sine’s and cosine’s of an angle is 1. So
(sin(theta))^2 + (cos(theta))^2 = 1
meaning
(1-q)^2 + r^2 = 1
=> 1 – 2q + q^2 + r^2 = 1
=> -2q + q^2 + r^2 = 0

Note that p + r = 2, so r = 2-p.
We also have p^2 + q^2 = 2, so q^2 = 2 – p^2

Substituting, we have
-2q + 2 – p^2 + (2-p)^2 = 0
=> -2q + 2 – p^2 + 4 – 4p + p^2 = 0
=> 6 – 2q – 4p = 0
=> p = (3-q) / 2

Substituting into p^2 + q^2 = 2, we have
((3-q) / 2)^2 + q^2 = 2
=> 5q^2 – 6q + 1 = 0

Using the quadratic formula, we solve for q,
q = [ -(-6) ± sqrt( (-6)^2 – 4(5)(1) ) ] / 2(5)
= 1 or 1/5

Since q should be less than 1, therefore q = 1/5. With that, remember that
sin(theta) = 1-q = 4/5
Using the inverse sine, we have theta = 53.13 degrees.

There’s actually an easier way to calculate theta. I was puzzling through the arcane math formulae that I’ve not touched for a long time and gotten a wrong result (both my q’s were greater than 1). I needed to bathe anyway, so I took a break.

As soon as the water from the shower head hit me, it hit me. There’s a simple and more elegant solution to this!

The angle to reach at least the door gap distance when the door was closed, was double the angle when the door gap distance was the minimum! It’s a mirror image! Just look at the diagram above and convince yourself of that. So the angle is 2 * 26.5651, which is roughly 53.13 degrees.

Yeah, I’m a dunce. Goes to show that sometimes, taking a break really does give you a new perspective on things. Elegant solutions can pop out of the most obscure circumstances. Well, at least I got some practice with algebra and trigonometry functions. Oh right, I nearly forgot why we’re doing all this.

Are you telling me you’re going to push the door 53 degrees in, when you could have the same door gap distance without doing anything in the first place?!

The other obvious solution to this is to open the door the other way, so the door gap distance becomes larger immediately. But we’ve already established that most people don’t like to pull on doors. And now, I’m telling you there’s a subtler solution without using much more energy. Check this out.

Some people can’t just accept a polite gesture, can they?

P.S. There’s a flaw in the argument I’ve presented in this entire article. Can you spot it?