Café Wall Illusion¶
By the end of this lab you'll be able to:
- Draw a café wall pattern where each row of tiles is offset by half a tile
- Understand why the mortar lines appear to converge even though they are parallel
- Use nested loops to build a tiled grid with alternating row offsets
The Café Wall Illusion was first noticed on a café in Bristol, England. Rows of black and white tiles with thin gray mortar lines between them — every mortar line is perfectly horizontal and parallel, yet they appear to slant!
Welcome to the Café Wall Illusion!
In 1979, a researcher named Richard Gregory noticed this illusion on the tiles
of a café wall in Bristol. The mortar lines are perfectly parallel — but look slanted!
Let's code it together and see if it tricks your eyes too!
How It Works¶
Draw a grid of black and white rectangles. Odd rows are shifted right by half a tile width. Between rows, draw a thin gray horizontal mortar line. The offset tiles + mortar color creates the illusion of diverging lines.
Sample Code¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | |
What Do You Think Will Happen?
Every gray mortar line is drawn perfectly horizontal — the code guarantees it.
Will they still look slanted after you add the black and white tiles?
Make your guess — then click Run to find out!
Try It Now¶
The gray lines appear to slant even though the code draws them perfectly horizontal! Were you right?
How It Works¶
The gray mortar color sits between the dark and light tiles. Your visual system judges line position relative to local contrast. When the black and white tiles on either side of a mortar line are offset, the edge contrast makes the mortar appear to tilt toward the darker side.
Explanation Table¶
| Line | What it does |
|---|---|
offset = (row % 2) * (tile_w / 2) |
Alternate rows shifted by half a tile |
mortar = 3 |
Thin gray strips between rows — the key to the illusion |
'black' if (col+row)%2==0 else 'white' |
Checkerboard coloring within each row |
filled_rect(...) |
Helper to draw a filled rectangle |
Learning Check¶
Your Turn — Change the Mortar Color
Change 'gray' in the mortar lines to 'black'. Will the illusion disappear or change?
The illusion requires a mortar color between black and white. What do you predict?
With black mortar, the illusion weakens considerably — the mortar blends with the black tiles and loses its role as the "middle ground" element that creates the apparent tilt.
Experiments¶
-
Increase mortar thickness. Change
mortar = 3tomortar = 8. You'll know it worked when the gray strips are much wider. -
Try colored tiles. Replace
'black'with'navy'and'white'with'lightyellow'. You'll know it worked when colored tiles appear — does the illusion still work? -
Remove the row offset. Change
offset = (row % 2) * (tile_w / 2)tooffset = 0. You'll know it worked when the illusion disappears — all tiles align. -
Vary the offset amount. Try
offset = (row % 2) * (tile_w / 3)ortile_w / 4. You'll know it worked when a different strength of illusion appears.
Your Brain Was Tricked Again!
The café wall illusion teaches us that perception isn't just about what's there —
it's about local contrast and context. Perfectly parallel lines look slanted!
Up next: Moiré Pattern — two grids overlapping to create beating patterns.