Concepts / Drawing Lines and Shapes Pixel by Pixel

Drawing Lines and Shapes Pixel by Pixel

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

  • Programming

Why a Program Needs Numbered Positions

Suppose you want a program to place a dot on a window. The program has to say exactly where the dot goes, and it needs a way to name that spot without ambiguity. 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 a coordinate system, we need to know what the surface is made of. The numbers only make sense against that underlying structure, so the next part of this article looks at the screen itself before it looks at the numbers.

A Screen Built from Pixels

Look closely at any screen and you will eventually 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.

Drawing on the screen has a very concrete meaning: it is the act of choosing the color of particular pixels. 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. A diagonal line, a circle, or a filled rectangle follows the same rule: the underlying operation is always to decide which pixels to color and with what color.

Key point

Every drawing command, no matter how complex the shape it produces, reduces to one operation: choosing the colors of individual pixels. Because of this, each pixel needs a name the program can refer to, and the names must be systematic enough that the program can compute the name of any pixel it wants.

Reading an (x, y) Pair from the Origin

Definition

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.

The grid below shows a small rectangle of positions, four across and three down. Read it as a set of moves from the origin. Each step to the right increases x by one, and each step downward increases y by one. The highlighted position (2,1) is reached by taking two steps right and one step down from the corner. Every integer pair names exactly one spot in the rectangle, and every spot in the rectangle has exactly one integer pair.

(0, 0)origin(1, 0)(2, 0)(3, 0)(0, 1)(1, 1)(2, 1)target(3, 1)(0, 2)(1, 2)(2, 2)(3, 2)
Given an (x, y) pair such as (2, 1), which spot does it refer to, and how do x and y map to columns and rows?
Worked example

Finding the spot named by (2, 1)

Starting at the upper left corner, which spot does the pair (2, 1) name?

  1. 1
    Start at the origin

    The origin is assigned the pair (0,0), so every count begins at the upper left corner.

  2. 2
    Read x first

    The first number is x = 2, which means move two steps to the right.

  3. 3
    Read y second

    The second number is y = 1, which means move one step downward.

  4. 4
    Identify the spot

    Two steps right and one step down reaches the spot named (2,1). Reversing the order would describe a different spot, which is why the order of the pair matters.

Result

The pair (2, 1) names the spot two steps right of the corner and one step down from it.

Why y Grows Downward

In this system the origin sits at the upper left, not the lower left. Moving down the screen makes y larger, and moving right makes x larger. This is the reverse of the vertical direction used in ordinary school graphs, where the origin is usually drawn at the lower left and y grows upward. Once you internalize the screen direction, you will read every coordinate on a screen correctly.

The Graphics Context and Its Rectangle

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.

Every position you give the graphics context is interpreted relative to the rectangle's upper left corner, not relative to the whole screen. 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. When it draws into that rectangle, the pixels that change are the ones that appear on the screen.

Graphics contextthe thing that drawsPixel rectangledrawing is restricted tothisCoordinate pairmeasured from the upperleft cornerColored pixelswhat appears on the screendraws insideinterpretscontainsselects
What does the graphics context contain, and how does drawing into it change the pixels that appear on the screen?

Coordinates Name Grid Lines, Not Squares

There is a subtle point worth slowing 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 a set of lines running across and down the surface. Each integer coordinate names one of those line intersections, and the pixels are the regions between them.

Stepping Through Pixels to Build a Line

Put the pieces together and trace what happens when a program draws a line. The program does not hand the screen a line object. It works through a sequence of positions, one at a time, and for each position it decides whether that pixel should take the drawing color. Each chosen pixel is named by its (x, y) pair relative to the graphics context's rectangle, so the program can compute the name of every pixel it touches.

  1. 1
    Choose the rectangle the graphics context draws into, and treat its upper left corner as (0,0).
  2. 2
    Pick the first position along the line and name it with its integer (x, y) pair.
  3. 3
    Color the pixel that this pair selects with the drawing color.
  4. 4
    Move to the next position along the line by stepping right, down, or both, and repeat.
  5. 5
    Stop when the chain of colored pixels reaches the end of the line, and the eye reads the chain as a line.
In the real world

A horizontal line is the simplest chain: y stays the same while x increases one step at a time. A line that slopes down the screen keeps stepping in the direction where y grows, because y grows downward. Each pixel in the chain is still just one colored square, and the line exists only as the pattern those squares form.

Mistakes Beginners Make with Screen Coordinates

  • Common mistake

    Treating the origin as the lower left corner

    What it looks like

    Reading (0, 0) as the bottom left of the window

    Why it fails

    The source places the origin at the upper left corner, where the count begins.

    Do this instead

    Always start counting from the upper left and move right for x and down for y.

  • Common mistake

    Assuming y grows upward

    What it looks like

    Expecting a point with a larger y to sit higher on the screen

    Why it fails

    y grows downward, which is the reverse of the vertical direction in ordinary school graphs.

    Do this instead

    A larger y means a lower position on the screen.

  • Common mistake

    Swapping the order of the pair

    What it looks like

    Reading (2, 1) as two steps down and one step right

    Why it fails

    The pair is read in order: x first, then y. The source stresses that reading the pair in order matters.

    Do this instead

    Read the first number as horizontal distance and the second as vertical distance.

  • Common mistake

    Thinking a line is stored as a line object

    What it looks like

    Expecting the screen to keep a line that can be retrieved as a single item

    Why it fails

    The source says a straight line is not a special object stored by the screen; it is a chain of colored pixels.

    Do this instead

    Think of every shape as a set of individually colored pixels.

  • Common mistake

    Measuring coordinates from the whole screen

    What it looks like

    Assuming a coordinate is relative to the monitor's corner when drawing into a smaller rectangle

    Why it fails

    The source says positions are interpreted relative to the upper left corner of the graphics context's rectangle.

    Do this instead

    Measure from the upper left corner of the rectangle the graphics context draws into.

Predicting a Pixel Location

What do you think happens?

Starting from the upper left corner, you take two steps to the right and one step down. Which integer pair names this spot?

  • (1, 2)

  • (2, 1)

  • (1, 1)

  • (2, 2)

Reveal answer
Answer:

(2, 1)

x counts steps to the right, so two steps right gives x = 2. y counts steps downward, so one step down gives y = 1. The pair is written in order as (x, y), giving (2, 1).

EASY

A graphics context draws into a rectangle of pixels. Describe in your own words the path from the origin to the pair (3, 2), naming how many steps you take right and how many steps you take down. Then state which direction y grows on the screen and why the spot (3, 2) sits lower than the spot (3, 0).

Hints
  • Count the x steps first, then the y steps, using the upper left corner as the start.
  • Remember that y grows downward on a screen, not upward as on a school graph.

Key Takeaways

  • A screen is a grid of pixels, and drawing means choosing the colors of individual pixels; a straight line is a chain of colored pixels, not a 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.
  • A graphics context draws inside a rectangle of pixels, and its coordinates are measured from that rectangle's upper left corner.
  • Coordinates name grid line intersections, while pixels are the regions between those lines.