Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 50 ยท Getting Started

Coordinate Systems

Learn screen vs Cartesian coordinates, the y-axis flip, and how world, view, normalized device and screen spaces fit together, with live demos.

To draw anything, you must say where it goes, and "where" always depends on a coordinate system: an origin, a set of axes and a unit of measure. Computer graphics uses several coordinate systems at once. A character is modeled around its own center, placed in a world, seen from a camera, and finally shown at pixel positions. Mixing them up is one of the most common sources of bugs, such as upside-down images, objects that vanish, or shapes that are mirrored.

This lesson separates the systems, explains why the screen's y-axis points the "wrong" way, and gives you the vocabulary for the transformations of the next chapters.

Cartesian versus screen coordinates

In school mathematics you draw the Cartesian plane with the origin in the middle (or bottom-left), x growing to the right and y growing upward. The point (3, 2) is three units right and two units up.

Screens work differently, for historical reasons. Early displays scanned the picture from the top-left corner, line by line, and framebuffers store the first row first. So the screen coordinate system has its origin at the top-left pixel, x grows to the right, and y grows downward. The point (3, 2) is three pixels from the left edge and two pixels down from the top.

flowchart LR
  subgraph M["Cartesian (math)"]
    direction TB
    M1["origin: center or bottom-left"]
    M2["x: right, y: UP"]
  end
  subgraph S["Screen (canvas)"]
    direction TB
    S1["origin: top-left pixel"]
    S2["x: right, y: DOWN"]
  end
  M -->|"y_screen = H - y"| S

Converting between the two flips the y-axis.

If your drawing area is H pixels high and you want a Cartesian point with the origin at the bottom-left, the conversion is simply:

TEXT
x_screen = x
y_screen = H - y

If you want the Cartesian origin in the middle of the canvas (handy for plotting and for rotating things around the center), add offsets: x_screen = W/2 + x and y_screen = H/2 - y.

The demo draws a grid in Cartesian coordinates with the origin in the middle. Move the mouse over it: the page shows the screen position (what the browser gives you) next to the Cartesian position (what the math wants). Both describe the same spot.

Screen versus Cartesian coordinates

The red square marks the screen origin. The blue dot at Cartesian (3, 2) is above the x-axis even though its screen y is smaller than the axis's y.

Many spaces, one pipeline

Real scenes use more than two systems. In 3D graphics each stage of the pipeline has a name for the space its numbers live in:

  1. Object (model) space. Coordinates measured from the object's own origin. A car model might have its origin at the center of its floor. The numbers never change, however the car moves.
  2. World space. One shared system for the whole scene. Each object is placed here with a transformation (scale, rotate, translate). Two objects can now be compared: how far apart are they?
  3. View (camera, eye) space. The world as seen from the camera, with the camera at the origin looking along a fixed axis. Moving the camera is the same as moving the whole world the opposite way.
  4. Clip space and normalized device coordinates (NDC). After projection, everything the camera can see is squeezed into a standard cube (or square in 2D). In NDC the visible region always spans -1 to +1 in x and y, whatever the real window size is.
  5. Screen (window) space. The final pixel coordinates inside the framebuffer.
flowchart LR
  A["Object space<br/>local to the model"] -->|"model matrix"| B["World space<br/>shared scene"]
  B -->|"view matrix"| C["View space<br/>camera at origin"]
  C -->|"projection"| D["NDC<br/>-1 to +1"]
  D -->|"viewport mapping"| E["Screen space<br/>pixels"]

Each arrow is a transformation. You will build all of them in the coming chapters.

Why so many? Each space makes one job easy. Modelers build in object space without caring where the model ends up. Game logic works in world space. Cameras move in view space. NDC lets the hardware clip and rasterize without knowing the window size, and the last step adapts the result to the real screen.

Normalized device coordinates

NDC deserves a special mention because the last step is formulaic. For a screen of W by H pixels, mapping NDC (nx, ny) with y up to pixels with y down is:

TEXT
x_screen = (nx + 1) / 2 * W
y_screen = (1 - ny) / 2 * H

Check the corners: NDC (-1, 1), the top-left of the view, gives pixel (0, 0). NDC (1, -1), the bottom-right, gives (W, H). The 1 - ny is the y-flip again.

Following one point through the spaces

The next example is a 2D miniature of the pipeline. A unit square is modeled around its own center. It is then scaled by 2 and moved to (5, 3) in the world. A camera sits at (4, 2) and sees an area 10 units wide and 6 units high. We follow the corner (1, 1) of the model through every stage and plot the whole scene.

One point through every space

Walk through the log: (1, 1) becomes (7, 5) in the world, (3, 3) relative to the camera, (0.6, 1) in NDC, and finally pixel (240, 0): the top edge of the screen, because NDC y = 1. Try changing camera and watch the square slide the opposite way, which is exactly what "moving the camera" means.

Left-handed and right-handed systems

One more detail will matter in 3D. When the x-axis points right and y points up, the z-axis can point either toward the viewer (a right-handed system) or away from the viewer (left-handed). OpenGL and most math textbooks use right-handed; DirectX traditionally uses left-handed. Neither is better, but you must not mix them. The lesson on 3D coordinates and transformations covers this in detail.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a canvas, where is the origin and which way does y grow?

    Screen coordinates start at the top-left pixel and y increases downward, following the order in which the framebuffer is stored and scanned.
  2. 2. A Cartesian point (4, 3) is plotted on a canvas of height H with the origin at the bottom-left corner. What is its screen y?

    The y-axis flips, so y_screen = H - y.
  3. 3. What is the purpose of normalized device coordinates?

    NDC is a resolution-independent standard space. Clipping and rasterization can work on it, and a last viewport step maps it to real pixels.
  4. 4. A model is placed in the scene with a model matrix. Which space do the vertices move from and to?

    The model matrix converts coordinates relative to the object's own origin into coordinates in the shared world.

Summary

  • Math uses Cartesian coordinates with y up; screens use the top-left origin with y down. Converting needs a y-flip such as y_screen = H - y.
  • A graphics pipeline passes vertices through object, world, view, clip/NDC and screen space; each transformation solves one problem.
  • NDC spans -1 to +1 regardless of window size; the last step maps it to pixels with (n + 1) / 2 * size, flipping y.
  • Always know which space a number belongs to. Most "my picture is upside down" bugs are a missed flip.

Next, we start drawing: The DDA Line Algorithm.