Pro OpenGL ES for iOS

(singke) #1

36 CHAPTER 2: All That Math Jazz^


Mathematicians are always fond of expressing things in the most compact form
possible. So 2D rotations can be ‘‘simplified’’ using matrix notation:






⎡ −
=
sin( ) cos( )

cos( ) sin( )
a a

a a
Ra

Note One of the most overused words in Star Trek is matrix. Pattern-matrix here, buffer-
matrix there—“Number One, I have a headache-matrix and need to take a nap-matrix.” (And
don’t get me started on the use of protocol in 24 .) Every self-respecting Star Trek drinking
game (as if any drinking game would be self-respecting) should use matrix in its selection of
words.

Ra is shorthand for our 2D rotation matrix. Although matrices might look busy, they are
actually pretty straightforward and easy to code because they follow precise patterns. In
this case, x and y can be represented as a teeny matrix:












⎡ −
⎥=





y

x
a a

a a
y

x
sin( ) cos( )

cos( ) sin( )
'

'


Translations can also be encoded in a matrix form. Because translations are merely
moving the point around, the translated values of x and y come from adding the amount
of movement to the point. What if you wanted to do a rotation and a translation on the
same object? The translation matrix requires just a tiny bit of nonobvious thought. Which
is the right one, the first or second shown here?







=
Tx Ty

T


1 1
or










=
1

0 1 0


1 0 0


Tx Ty


T


The answer is obviously the second one, or maybe it’s not so obvious. The first one
ends up as the following, which doesn’t make much sense:

x′=x+yTx and y'=x+yTy


So, to create a matrix for translation, we need a third component for our 2D point,
commonly written as (x,y,1), as is the case in the second expression. Ignoring where the
1 comes from for a moment, notice that this can be easily reduced to the following:

x′=x+Tx and y′=y+Ty


The value of 1 is not to be confused with a third dimension of z; rather, it is a means
used to express an equation of a line (in 2D space for this example) that is slightly
different from the slope/intercept we learned in grade school. A set of coordinates in this
Free download pdf