## Changing Perspectives

You can change the whole world or you can change your own perspective. In a computer, changing a virtual world is just as easy as changing the camera view point.

With computer graphics, with the right set of values, you can move and rotate the virtual world in such a way that it looks as though you’re moving the virtual camera. When you get down to it, it’s just pushing matrices into the pipeline in OpenGL or DirectX or whatever 3D graphics engine you’re using.

## Multiply by -1 to change sign considered harmful

Recently, I had the honour to delve into the depths of some legacy code. The program handled some financial data, and there was a section where the value had to change sign. The exact details aren’t important to the story, so let me come up with a suitable example. Uh… payment!

The monetary value stored in the database was considered positive (or profit). When people pay you \$10, you have a record with \$10 in the database. What happens when you pay other people? You use a negative value. This keeps the column data storage logic consistent.

So the variables used in the program stored positive values of the amount you need to pay other people, performing calculations as normal. When it came time to store in the database, there was a last line of code before the database update:

```decPayment = decPayment * (-1);
```

Was -1 a magic number (as in, it was hardcoded, and could be some other number)? Was the multiplication in error (maybe it was supposed to be addition)? Was this line of code even supposed to exist?

It turned out that the programmer wanted to change the sign of the variable value. This method of changing sign has some downsides:

• Multiplication is expensive. Maybe processor chips are fast now, but still…
• The intent is not clear (what is it trying to achieve?)
• It has more characters than the more optimal way

So what’s the “more optimal way”?

```decPayment = -decPayment;
```

You don’t have to multiply by -1 to change the sign of a variable. Just directly negate it.

## All systems are in a state of entropy

I’m surprised to find multiple meanings for the word “entropy”. I thought it just meant “breaking down” or something. I was searching online and doing research, and … well, even though I quoted:

Anyway, it occurred to me that all systems are in a state of entropy (we shall use the version of “breaking down” henceforth). A tree is in a state of entropy. A lump of rock is in a state of entropy (the molecules are breaking down, just very very slowly). The rubbish in land fills is breaking down. You are breaking down (the molecular deconstruction part, not the crying part).

Yet all seems fine. The tree is still standing. That lump of rock is still showing the world how durable it is. The rubbish in land fills is still … decomposing (unfortunately not fast enough). And you are still here (thankfully).

### The virus behaviour simulation program

The young man was stuck. He needed to write a thesis paper to complete his degree qualifications, but he didn’t know what to write about. The topics available weren’t interesting enough, so he consulted his thesis advisor. And the advisor said, “Why don’t you write something on computer viruses?”.

And that’s what the young man did. And to complement the research, he would write a program to simulate how computer viruses spread and behave, based on his research.

He wrote a preliminary program to do simple “infection” and “disinfection” of nodes (representing computers). The node system became Resident Evil after a few iterations.

Then from his research, he learned about topography. So he applied a few of them. The results were interesting, and one showed promise: the tree structure.

Did more research (and worked on his thesis, solving some integration equations on the way), and thought of the Internet as a fragmented tree structure. And so he created a new topological structure based on uneven connectedness.

That was promising. But the stabilising of computer viral infection was too slow. Something was missing…

And then he came upon the brilliant idea of periodicity. Maybe computers weren’t “on” all the time! He factored that in, and the resulting “epidemic” simulation finally approached what looked like a sample chart from a well known virus protection software provider.

### Just keep repairing

I was thinking of the general sentiment “Every program has bugs”. Every program is imperfect, even the Hello World program. Why is that so?

Because even programs obey the entropy rule. The moment you think up the code for the program, it’s already on the way to decay.

In the case of the story above, the simulation program was “buggy”. As new information surfaced from the research, that knowledge was used to improve the simulation logic. But it’s nowhere near done. [In it’s present state, the simulation is probably so far off the mark, that I’m … uh, I mean the young man is ashamed of it.]

You may think it’s done, it’s perfect. There shouldn’t be any bugs, or any changes that will make it better. But it’s not.

Because there are always patches on the most reliable of software. Someone always manages to find a vulnerability to exploit.

Perhaps the program works fine, but its environment is “decaying”. The data it fed on changed slightly, causing its input mechanic to fail. The hardware it’s running on blew a small circuit, causing tiny imperfections on a physical level, which caused it to choke when it ran certain code constructs.

So if all systems are in a state of entropy, even software, what can you do? Keep repairing them. Keep improving them.

If you’re not the negative type, then all systems are in a state of change. But are they changing for the worse, or better?

Here’s a question for you to think about. Is an idea in a state of entropy?

P.S. I just finished reading The Lost Symbol by Dan Brown. If you’re not the philosophical type, I apologise. And then I blame Dan Brown.

## Rotate backwards to stay level

It was a university programming assignment. I was to write an OpenGL program to render a Ferris wheel. The requirements were simple. There had to be 7 spokes emanating from the centre, each at an equal angle from each other. At the end of each spoke, there was to be a carriage. No outer rim was required. All 7 spokes and 7 carriages were to be simple cuboids. The wheel was to turn slowly. Colour aesthetics up to the individual.

I’ve already had lessons on simple rotation and translation operations in OpenGL. Ambient colouring, materials and shading were also taught. And simple cuboids were like basic rendering stuff.

The hard part that my fellow students found was in keeping the carriages level, while rotating the Ferris wheel.

My professor, being the evil mind that he was, chose 7 spokes, so the angle between each spoke was “weird” (no nice number). Believe it or not, that confused a heck of a lot of students… Rendering the spokes were easy. Render a long cuboid with one end at the origin, and rotate multiples of 360/7 degrees. The carriages on the other hand, needed some work…

A simple way of orienting the Ferris wheel is to align it with the XY plane, with the centre of the wheel at the origin. There are then 2 methods to render the carriages. The first is to calculate the XY coordinates of centres of all the carriages, and simply translate them there. Yes, there will be sines and cosines in the calculation. I’ll leave it to you as an exercise. If you were able to follow the article on bilinear interpolation in image rotation, you can do this.

The second method is to just use the rendering engine’s in-built functions. For example, you render a vertical spoke with one end at the origin, and the other end along the positive Y-axis. Then you render a carriage at the latter end of the spoke.

Then what do you do? Render the 2nd spoke-carriage combination exactly the same as the 1st, but rotate the whole thing 360/7 degrees clockwise. Here’s where the problem comes. Since the carriage is “tied” to the spoke, the rotation operation affects the carriage as well.

To keep the carriage level, you have to undo the rotation operation. How do you do that? Rotate the carriage in the other direction with the same angle.

Let’s leave the spokes out of the picture. To render a carriage in the correct position, at the correct angle, this is the series of steps to take (assuming the carriage is at the origin):

• Rotate -i * (360/7) degrees (anti-clockwise)
• Translate len units in positive Y direction
• Rotate i * (360/7) degrees (clockwise)

where i is the number of multiples required, and len is the length of the spoke.

If you’re following this with OpenGL or DirectX, take care. That series of steps have to be reversed, because the 2 rendering engines apply the transformations in reverse order.

Hmm… that was a long story…

Did you know chickens have this ability to keep their heads stable, even if their bodies are moving? Check this video out.

I believe chickens use a similar principle as discussed in the Ferris wheel. For example, if a chicken’s body was moved forwards (in the direction of its head), to keep its head in the same position, it has to move its head backwards.

### A fluttering thought

To stay the same in the face of change, one must replicate and execute the change in the opposite manner.

Sort of like Newton’s First and Third Laws combined.

I was just thinking, in the face of changes in these times, staying the same is actually more tiring than going with the flow. I mean, you expend the exact same amount of effort to stay the same, and you have to expend more to improve (on business, on technology and so on). That doesn’t quite make sense…

It’s a cliche, I know, but the only constant in life is change. Expect it. Embrace it. Besides, staying the same is boring at some level…

### A simple experiment

To convince yourself of the “backwards” principle discussed in the rendering of a Ferris wheel, try the following.

• Stand up straight, face and body forwards
• Turn your body, from the shoulders down, clockwise
• You must keep your head still facing the same direction as at the start

Did you have to turn your head anti-clockwise to keep facing the same original direction?