# Chapter 3: Coordinate Mapping

## Coordinate Mapping

To draw something, we need to transform from world to pixel!

Before transform, we must constrain the view into the world.

### General Form

In general, transforms can look like this:

Where $c$ and $e$ are scales and $d$ and $f$ are shifts.

(it’s going to seem like we use these equations for this assignment and never again, but this is the basis for most of the transforms we will do this quarter)

We must constrain our world into a particular view. We will determine a mapping from four world space values to pixel/screen space values. On the left is world coordinates, on the right is pixel coordinates:

Where $w$ and $h$ are the width and height of the screen in pixels (e.g. 1024 x 768).

$L$, $R$, $B$, and $T$ are the left, right, bottom, and top (respectively) of the viewport in world space. We will use $-1$ for Left and Bottom, and $1$ for Right and Top.

### Solve for $c$ and $d$

So what about $c$?

Plug back in to our $w$ equation.

Let’s say our window is 320x240 and using the L/R/B/T values from above.

And now plugging in to our $c$ equation:

So our general transformation from world coordinates into pixel coordinates is:

The formulation for y will be quite similar.

### Aspect Ratio

To account for aspect ratio, we need to pick different L and R values.