Vasarely Sphere¶
By the end of this lab you'll be able to:
- Draw a regular grid of dots with nested loops
- Measure each dot's distance from the center with
math.sqrt - Map distance to dot size so a flat grid appears to bulge into a 3-D sphere
A perfectly regular grid of dots — except that inside a central circle, the dots swell up based on how close they are to the center. That one change makes a sphere appear to push out of the flat page. The Hungarian-French artist Victor Vasarely, the "grandfather of op art," used exactly this trick in his famous Vega paintings.
Welcome to the Vasarely Sphere!
Every dot in this picture sits exactly on a flat, square grid.
Yet a 3-D sphere will bulge right out at you!
Victor Vasarely turned this trick into world-famous art — now it's your turn.
How It Works¶
Two nested loops place a dot every 28 pixels in a 13 × 13 grid. For each dot we compute its distance d from the center using the Pythagorean theorem: d = sqrt(x*x + y*y). If the dot is inside the radius R, we compute a swell factor with cos(d / R * pi / 2) — it equals 1 at the center and fades to 0 at the rim — and add bulge * swell to the dot's size. Dots outside R stay small, so the sphere sits inside a calm, flat grid.
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 | |
What Do You Think Will Happen?
The grid spacing never changes — only the dot size does.
Will the middle of the picture look like a flat patch of bigger dots,
or like a 3-D ball pushing out of the page? Guess, then click Run!
Try It Now¶
Most people see a ball bulging out of the page — your brain reads "bigger" as "closer" and builds the 3-D shape automatically. Were you right?
How It Works¶
Your visual system constantly guesses depth from size: things that are closer look bigger. When dot size grows smoothly toward the center, your brain's simplest explanation is that the surface itself is curving toward you — so it perceives a sphere. The cosine function is the secret sauce: it changes slowly near the center and quickly near the rim, exactly like the surface of a real ball tilting away from you.
Explanation Table¶
| Line | What it does |
|---|---|
for row in range(-n, n + 1): |
Nested loops visit all 13 × 13 = 169 grid positions |
d = math.sqrt(x * x + y * y) |
Pythagorean distance from the dot to the center |
swell = math.cos(d / R * math.pi / 2) |
1 at the center, fading to 0 at the rim of the bulge |
size = base + bulge * swell |
Maps distance to dot size — the whole illusion in one line |
Learning Check¶
Your Turn — Add the Missing Line
This version computes swell but never uses it, so the sphere is gone —
every dot stays the same size. Add one line inside the if block
(right after swell is computed) to bring the bulge back!
The missing line is size = base + bulge * swell — without it, swell is computed and thrown away, and every dot keeps the base size.
Experiments¶
-
Make the bulge stronger. Change
bulge = 16tobulge = 22. You'll know it worked when the center dots nearly touch and the sphere looks even rounder. -
Make a dent instead of a bump. Change the size line to
size = base + bulge - bulge * swellso dots shrink toward the center. You'll know it worked when the grid looks like it is denting away from you. -
Grow the sphere. Change
R = 130toR = 180. You'll know it worked when the bulge swallows almost the whole grid, leaving only the corners flat. -
Move the sphere off-center. Change the distance line to
d = math.sqrt((x - 56) * (x - 56) + y * y). You'll know it worked when the bulge slides to the right side of the grid.
You Made Flat Paper Bulge!
One if statement and a cosine turned a boring grid into 3-D op art —
the same math Vasarely used in galleries around the world.
Up next: Scintillating Grid — white dots that flash black when you're not looking.