[geometry] Improving calculation performance?

It seems like Famous 0.3.x (https://github.com/Famous/famous) does
less calculations, and ends with the same results. When rotating
around Z, using Famous' `Transform.rotateZ(45 * Math.PI/180)`, we get
the same value for m11 as we do with `DOMMatrix.rotate(45)`.

Here's what Famous is doing:
https://github.com/Famous/famous/blob/develop/src/core/Transform.js#L284-L306

We see the value for m11 is calculated as

```
x * x * (1 - Math.cos(theta)) + Math.cos(theta)
```

(inferred by replacing all the variables with their actual calculations).

In DOMMatrix, the value for m11 is calculated as

```
1-2*(y*y + z*z)*Math.pow(Math.sin(angle/2 * Math.PI/180), 2)
```

. If we assume radians in the calculation, that is

```
1-2*(y*y + z*z)*Math.pow(Math.sin(theta/2), 2)
```

, which can be written as

```
1-2*(y*y + z*z)*Math.sin(theta)*Math.sin(theta)
```

. Now, comparing the two, we see that the Famous Transform calculation
has 6 steps and the DOMMatrix calculation has 9 steps, so the Famous
Transform calculations may be faster.

If we simplify each calculation (Trnsform and DOMMatrix m11 rotation,
assuming we are rotating around the Z axis), then both formulas
become, respectively:

```
Math.cos(theta)
```

and

```
1-2*Math.sin(theta/2)*Math.sin(theta/2)
```

There might be room for improvement on the calculations specified in
the spec, depending on which trig identities we choose. This would be
good considering we want graphic calculations to be as fast as
possible.

/#!/JoePea

Received on Tuesday, 5 January 2016 01:16:10 UTC