Concepts / Pixel Grid and Screen Representation

Pixel Grid and Screen Representation

A screen is a grid of pixels, and drawing means choosing the colors of individual pixels.

  • Programming

Why a Program Needs Exact Names for Places

Suppose you want a program to place a dot on a window. The program has to say exactly where the dot goes. A phrase such as near the top and a bit to the right is not precise enough for a machine. What the program needs is a numbering scheme, a set of rules that turns every location on the surface into a pair of numbers. That scheme is the coordinate system. Before we can use it, we need to know what the surface is made of, because the numbers only make sense against that underlying structure.

EASY

Before reading on, think about it: if you had to tell a friend exactly which spot on a screen to color, what information would you need to give them so there is no possible confusion?

Hints
  • Think about whether a description like near the top is precise enough.
  • Think about what kind of names a program can compute and compare.

A Screen Built from Pixels

Look closely at any screen and you will see that the image is assembled from small squares. These squares are called pixels, and the whole display is a grid of them. Every pixel can be given its own color, and each one can be given a different color from its neighbors.

Definition

A pixel is one of the small squares that make up a display. Drawing on the screen means choosing the color of particular pixels.

This gives drawing a very concrete meaning. A straight line is not a special object stored by the screen. It is a chain of pixels that have been colored so that they look like a line to the eye. The same is true of a circle, a letter, or a photograph: each is a particular choice of pixel colors across the grid.

Key point

When a program draws any shape, the underlying operation is always the same: decide which pixels to color and with what color. This is why every pixel needs a name the program can refer to and compute.

Naming a Position with an (x, y) Pair

A position inside a rectangle of pixels is written as a pair of integers, traditionally called x and y, and written together as (x, y). The count begins at the upper left corner, which is assigned the pair (0,0). From that origin, x measures how far a position lies from the left side and grows as you move right. The value of y measures how far a position lies from the top and grows as you move downward.

Worked example

Finding the pixel at (2, 1)

Starting at the origin (0,0), which pixel does the pair (2, 1) name?

  1. 1
    Start at the origin

    The count begins at the upper left corner, which is assigned (0,0).

  2. 2
    Read x

    The first number, x = 2, tells you to move two steps to the right from the origin.

  3. 3
    Read y

    The second number, y = 1, tells you to move one step downward.

  4. 4
    Name the spot

    The pixel reached after two steps right and one step down is the one named (2,1). Every integer pair names exactly one spot in the rectangle.

Result

The pair (2, 1) names the pixel two columns right of the left edge and one row below the top edge.

Predicting the Location of (2, 1)

What do you think happens?

Starting from the upper left corner at (0,0), where does the pixel (2, 1) sit relative to that corner?

  • Two steps right and one step down

  • Two steps down and one step right

  • One step right and two steps down

  • Two steps left and one step up

Reveal answer
Answer:

Two steps right and one step down.

The first number x = 2 counts steps to the right, and the second number y = 1 counts steps downward, because y grows as you move down from the top edge.

Why the Origin Sits at the Upper Left

The origin is placed at the upper left corner because the coordinates are measured from there, and each direction of growth follows from that choice. x grows to the right, so moving across a row increases the first number. y grows downward, so moving down a column increases the second number.

Because every integer pair names exactly one spot and every spot has exactly one integer pair, a point such as (2,1) is never ambiguous once the origin and the two growth directions are fixed. Reading the pair in order also matters: (2,1) and (1,2) name different spots.

Drawing a Short Line by Changing Pixel Colors

Here is what drawing looks like when we trace the grid state. Suppose a program colors three neighboring pixels in one row, starting at the upper left, so that they look like a short horizontal line. Before the drawing call, all three pixels are white. After it, those same three pixels are black. No pixel was added or moved; only the colors of the named pixels changed.

Key point

The line exists only as the pattern of colored pixels. The program named three pixels and gave each one a color. That chain of colored pixels is what the eye reads as a line.

The Graphics Context and the Pixel Grid

Coordinates by themselves only name spots. Something has to turn those names into colored pixels, and in this model that role belongs to the graphics context. A graphics context is the thing that draws, and it draws inside a rectangle made up of pixels. Anything you ask it to draw is restricted to that rectangle, and every position you give it is interpreted relative to the rectangle's upper left corner.

So the graphics context ties together three ideas: a drawing surface, a grid of pixels on that surface, and a coordinate system that tells it where to act. A drawing call names positions, the graphics context resolves those positions against its rectangle's upper left corner, and the matching pixels are the ones whose colors change.

Coordinates Belong to Grid Lines, Not Squares

There is a subtle point to slow down for. When we say the position (0,0) is the upper left corner, it is tempting to picture that coordinate as belonging to the first square. The more precise statement is that the coordinates belong to the grid lines that separate the pixels, not to the pixels themselves. Picture the boundaries between squares as lines running across and down the surface. Each integer coordinate names one of those line intersections, and the pixels are the regions between them.

Mistakes When Reading Pixel Coordinates

  • Common mistake

    Reading the pair in the wrong order

    What it looks like
    Treating (2,1) as if it were (1,2)
    Why it fails

    x is always the first number and counts steps to the right; y is always the second and counts steps downward. Swapping them names a different spot.

    Do this instead

    Read the first number as horizontal distance from the left, then the second as vertical distance from the top.

  • Common mistake

    Assuming y grows upward

    What it looks like

    Expecting (0,1) to sit above the origin

    Why it fails

    y grows downward from the upper left corner, which is the reverse of ordinary school graphs.

    Do this instead

    Picture the origin at the top left and move down to increase y.

  • Common mistake

    Thinking coordinates belong to the pixel squares

    What it looks like

    Assuming (0,0) is the center of the first square

    Why it fails

    The coordinates belong to the grid lines between pixels, so each integer names an intersection of those lines.

    Do this instead

    Treat integer coordinates as boundary points and the pixels as the regions between them.

  • Common mistake

    Forgetting that drawing is restricted to the graphics context rectangle

Checking Your Understanding of a Pixel Location

EASY

Starting from the origin (0,0), describe the moves that take you to the pixel (3, 2). Then say what x and y each measure.

Hints
  • The first number tells you how many steps to take to the right.
  • The second number tells you how many steps to take downward from the top edge.

Key Takeaways

  • A screen is a grid of pixels, and drawing means choosing the colors of individual pixels; a line is a chain of colored pixels, not a special stored object.
  • A position is named by an integer pair (x, y) counted from the upper left corner at (0,0), where x grows to the right and y grows downward.
  • Reading the pair in order matters, since (2,1) and (1,2) name different spots, and coordinates belong to the grid lines between pixels rather than to the squares themselves.
  • A graphics context draws inside a rectangle of pixels and interprets every position relative to that rectangle's upper left corner, linking coordinates to the pixels that change color.