Tricks of the Windows Game Programming Gurus:Vector Rasterization and 2D Transformations

All ITKnowledge Source Code Search Tips Advanced Search |

| Tricks of the Windows Game Programming Gurus by Andre LaMothe Sams, Macmillan Computer Publishing ISBN: 0672313618 Pub Date: 09/27/99

Search this book:

Previous | Table of Contents | Next

#### Scaling

After all that, anything should be easy. Scaling is almost as simple as translation. Take a look at Figure 8.24. All you need to do to scale an object is multiply each coordinate by the scaling factor.

Figure 8.24 The math of scaling.

Scaling factors that are greater than 1.0 will make the object bigger, while scaling factors less that 1.0 will make the object smaller. A scaling factor of 1.0 will do nothing. The math to scale a point (x,y) by scaling factor s, resulting in (xs,ys), is

    xs = s*x

    ys = s*y

Also, you can scale each axis non-uniformly—that is, with different scaling factors for the x and y coordinates, like this:

    xs = sx*x

    ys = sy*y

Most of the time, you want to scale equally. But you might want to make an object grow in one axis, so who knows? Here’s a function that scales a polygon. It takes both an x- and a y-axis scaling factor, just in case:

    int Scale_Polygon2D(POLYGON2D_PTR poly, float sx, float sy)

    {

    // this function scales the local coordinates of the polygon

    // test for valid pointer

    if (!poly)

       return(0);

    // loop and scale each point

    for (int curr_vert = 0; curr_vert < poly->num_verts; curr_vert++)

        {

        // scale and store result back

        poly->vlist[curr_vert].x *= sx;

        poly->vlist[curr_vert].y *= sy;

        } // end for curr_vert

    // return success

    return(1);

    } // end Scale_Polygon2D

That one was easy, huh, hot rocks?

To scale a polygon to 1/10 its size, you would make the call

    Scale_Polygon2D(&polygon, 0.1, 0.1);

> Note: You may notice that the mouse pointer is visible on most demos. If you want to make it disappear (which is a good idea for games in general), you can make a call to the Win32 function `ShowCursor(BOOL bshow)`. If you send `TRUE`, the internal display count is incremented, and `FALSE` decrements it. When the system starts, the display count is 0. If the display count is greater than or equal to 0, the mouse pointer is displayed. Hence, the call `ShowCursor(FALSE)` will make the cursor disappear, and the call `ShowCursor(TRUE)` will make it appear again at some later time. Remember, though, that `ShowCursor()` accumulates your calls to it, so if you call `ShowCursor(FALSE)` five times, you must call `ShowCursor(TRUE)` five times to “unwind” the counter.

Notice that the x- and y-axis scale factors are both equal to 0.1. Thus, the scaling will be uniform on each axis.

As a demo of scaling, I have created `DEMO8_5.CPP|EXE`. It creates a single rotating asteroid. When you press the A key, the object gets bigger by 10 percent; when you press the S key, it gets smaller by 10 percent.

### Introduction to Matrices

When we start talking about 3D graphics, I’m really going to drown you in vectors, matrices, and other mathematical concepts. However, at this point, I just want to show you a few things about matrices and how they can be used in relation to the simple 2D transformations you’ve been doing the longhand way. Sound like a plan?

A matrix is nothing more than a rectangular array of numbers with a given number of rows and columns. We usually say that a matrix is m×n, meaning it has m rows and n columns. The m×n is also referred to as the dimension of the matrix. For example, here’s a matrix A that is 2×2:

    A = |1  4|

        |9 –1|

Notice that I use the capital letter A to denote the matrix. In general, most people use capital letters to denote matrices and bold letters for vectors. In the previous example, the first row is <1 4> and the second row is <9 –1>. Here’s a 3×2 matrix:

        |5    6|

    B = |2    3|

        |100 -7|

And here’s a 2×3 matrix:

    C = | 3  5 0 |

        |-8 12 4 |

To locate the <i,j>th element in the matrix, you simply go to the ith row and the jth column and look up the value. However, there is a gotcha…most math books start counting matrix elements with 1, rather than 0 as you do in computer programs, so keep that in mind. You’re going to start counting with 0 because this will make using C/C++ matrices work more naturally. For example, here’s the labeling of a 3×3 matrix:

        |a00 a01 a02|

    A = |a10 a11 a12|

        |a20 a21 a22|

Easy enough. So that’s all there is to the actual matrix itself and the labeling conventions. But you might ask, “Where do matrices come from?” Matrices are simply mathematical tools for lazy mathematicians, I kid you not. Basically, if you have a system of equations like

    3*x + 2*y = 1

    4*x – 9*y = 9

then that’s a lot of work writing all the variables down. You know that they’re (x,y), so why keep writing them? Why not just create a compact format that contains only the stuff you want to work with? This is how matrices came to be. In the previous example, there are three different sets of values that you can dump into matrices. You can work with these values together or separately.

Here are the coefficients:

    3*x + 2*y = 1

    4*x – 9*y = 9

    A = |3   2|

        |4  -9|

    Dimension is 2x2

Here are the variables themselves:

    3*x + 2*y = 1

    4*x – 9*y = 9

    X = |x|

        |y|

    Dimension is 2x1

And finally, here are the constants to the right:

    3*x + 2*y = 1

    4*x – 9*y = 9

    B = |1|

        |9|

    Dimension is 2x1

With all these nice matrices, you can focus on, say, the coefficient matrix A without all the other stuff. Moreover, you can write matrix equations like

    A*X = B

If you perform the math, you get

    3*x + 2*y = 1

    4*x – 9*y = 9

But how to perform the math? That’s our next topic.

Previous | Table of Contents | Next

Copyright © Macmillan Computer Publishing, Inc.

Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.