A ray tracer in 83 lines of Python, with no dependencies

November 24, 2026

PYTHONCOMPUTER GRAPHICS
Six mirrored spheres, animated

Ray tracing is sold as the thing that needs a new graphics card. The algorithm itself fits in one Python file that imports nothing outside the standard library. The image below was rendered by that file: 83 lines, three spheres, a checkerboard floor, shadows and a mirror.

Three spheres on a checkerboard, one of them a mirror, rendered at 1200x630 by the ray tracer from the post.

Ray tracer in 83 lines of Python - cover

All the code is on GitHub at JoaoLeonello/tiny-raytracer, including a version of the mirror scene that runs live in the browser, where you can drag the camera around the spheres.

The idea comes from Peter Shirley's Ray Tracing in One Weekend, cut down further. One warning first: "ray tracing is simple" is half true. The algorithm is simple. Making it fast is not, and the last section measures how far this version is from real time.

Part 1: One ray per pixel

1. The camera

The camera is a point. In front of it sits an imaginary screen at distance 1, and each pixel is a spot on that screen. The ray for a pixel starts at the camera and goes through that spot:

Python

The image is 1200 pixels wide, but the math is easier on a screen that goes from -1 on the left edge to 1 on the right, with 0 in the middle. The first two lines convert each pixel to that scale. The + 0.5 aims at the middle of the pixel instead of its corner. sets how wide the camera sees: tan(30°) gives a view 60° tall. Multiplying by accounts for the image being wider than it is tall; without it the spheres would come out squashed into ovals. The points the camera a bit down, toward the floor. The third line builds the ray and hands it to , which answers one question: what color does this ray see?

2. Hitting a sphere

A ray is described by three things: where it starts (o), which way it points (d) and how far along it you are (t). Walk a distance t from o in direction d and you land on the point o + t·d. The question is whether some value of t puts that point on the surface of a sphere. A point is on the surface when its distance to the center c is exactly the radius r. Writing that condition out gives a second-degree equation in t, the same kind as x² + bx + c = 0 from school, and the code solves it with the quadratic formula:

Python

The number that goes under the square root, disc (the discriminant), says whether there is a hit at all. If it is negative, the equation has no solution: the ray passes beside the sphere. If it is positive, there are two solutions, because a line through a ball goes in on one side and comes out the other. The smaller t is the way in, the side facing the camera, and that is the one we see.

The floor is simpler: it is the flat surface at height y = 0. A ray that starts at height o.y and drops d.y for every step it takes reaches zero after t = -o.y / d.y steps. The checkerboard comes from rounding the x and z of the hit point down to whole numbers and adding them: an even sum gives a dark square, an odd sum a light one. checks the ray against the three spheres and the floor and keeps the closest hit, because the closest object hides whatever is behind it. There is no shortcut: every ray tests every object.

Part 2: Light

3. Lambert and the shadow ray

The light comes from a single direction, like the sun, called L. How bright a surface gets depends on its angle to the light: facing it head on, full brightness; tilted, less; facing away, none. The code measures that with . n is the normal, an arrow sticking straight out of the surface at the hit point. The dot product of two arrows of length 1 is 1 when they point the same way, 0 when they are at a right angle, and negative when they point apart; turns the negative values into zero. This rule is called Lambert shading. Before adding the light, the tracer checks whether anything stands between the point and the sun, by firing a second ray from the point toward L:

Python

That is the shadow ray. If it hits anything, the point is in shadow and only gets AMBIENT, a small fixed amount of light (0.15) that keeps shadows dark instead of pure black. Same scene at 800 × 420, first with the shadow ray removed, then with it:

The same scene rendered without shadow rays: the spheres look like they float.

Ray tracer without shadow rays

The same scene with one shadow ray per hit: each sphere casts a shadow on the floor.

Ray tracer with shadow rays

The image has 336,000 pixels. Without shadows the tracer sent 348,971 rays; with them, 573,910. The difference, 224,939, is one shadow ray for every pixel whose ray hit something. Pixels that see the sky never ask about the light.

4. The epsilon

The shadow ray starts exactly on the surface. Computers store decimal numbers with limited precision, so "exactly on the surface" really lands a tiny bit above or below it. A ray that starts just below hits the same surface it left, about 0.000001 away, and concludes the point is in shadow. The result is black speckles over every lit surface, known as shadow acne. (0.0001) tells the tracer to ignore hits closer than that. Shadow mapping, the technique most games use for shadows, has the same problem under the same name, and fixes it with a similar small offset called depth bias.

5. The mirror

The middle sphere is 80% mirror (reflectivity 0.8). When a ray hits it, the tracer bounces the ray the way a ball bounces off a wall: the part of the direction going into the surface flips, the part running along it stays. Written with vectors, that is r = d - 2(d·n)n. Then it calls again, from the hit point and with the new direction: the function calls itself, which is called recursion. The final color is 20% the sphere's own and 80% whatever the bounced ray found. The recursion stops after 3 bounces, so two mirrors facing each other cannot bounce a ray forever. Look at the first image: the red and blue spheres and the floor's shadows show up in the mirror without any extra code for them.

Turn every sphere into a mirror, tint each reflection by the sphere's color and let a ray bounce up to 10 times, and the spheres start reflecting each other's reflections. The animation below is 60 frames from that variant of the same tracer, with two spheres bouncing and one orbiting above:

60 frames rendered with the Python tracer: six reflective spheres, two bouncing and one orbiting, with up to 10 reflection bounces.

Six mirrored spheres, animated

Part 3: What it costs

6. Measured

Python 3.12 on a laptop, under WSL, one core:

resolution

pixels

rays

time

1200 × 630

756,000

1,290,852

3.4 to 3.9 s

1920 × 1080

2,073,600

3,551,344

9.2 s

About 1.7 rays per pixel: the camera ray, a shadow ray when it hits something, and the extra bounces off the mirror. At 1080p one frame takes 9.2 seconds. A game at 60 frames per second has 16.7 milliseconds, so this tracer is about 550 times too slow, and the scene has four objects.

7. Why the graphics card gets there

Every pixel in this file is computed on its own: the color of pixel (10, 20) never depends on pixel (11, 20). Nothing forces the double loop in to run in order. I split the 1200 × 630 image into 64 × 64 tiles and handed the 190 tiles to a pool of processes on a laptop with 12 logical cores. The render went from 2.5 s on one core to 0.42 s, about 6 times faster, with the same function inside each tile.

A GPU takes the same idea much further. Instead of 12 big cores it has thousands of small ones (an RTX 4090 has 16,384) and runs the per-pixel function on all of them at once. The browser version in the repo is this tracer ported to a fragment shader: the shader is the body of the double loop, and the GPU calls it once per pixel, every frame. The cores work in groups that execute the same instruction together, 32 threads per group on NVIDIA, which they call a warp. That is why the work goes out in blocks of neighboring pixels: neighbors tend to hit the same sphere and take the same branch. When one pixel in the group bounces off a mirror and its neighbor sees the sky, the neighbor sits idle until the bounce finishes. Rays that leave the camera side by side stop agreeing after the first reflection, and that is the part of ray tracing GPUs still find hard.

8. Where the time would go in a real scene

Every ray here tests every object. With four objects that is fine. A game character alone has tens of thousands of triangles, and a linear scan per ray is out of the question. Real ray tracers put the scene in a BVH, a tree of bounding boxes, so a ray only tests the triangles in the boxes it crosses. That part is most of the code in a production renderer, and it is what the RT cores in recent GPUs do in hardware.

The other cost is visible in the first image. Far away, the checkerboard turns into a moiré pattern. Each pixel sends one ray through its center, and when a pixel covers several squares, that one ray picks a square almost at random. The fix is several rays per pixel, averaged, which multiplies everything in the table above by the number of samples.

In short: the algorithm costs 83 lines, and everything that makes it usable, from the BVH to the extra samples, costs rays.

References
  • Peter Shirley, Ray Tracing in One Weekend (free at raytracing.github.io): the same path in C++, with antialiasing, materials and a camera you can move.

  • Turner Whitted, An Improved Illumination Model for Shaded Display (1980): the recursive ray tracer with shadow, reflection and refraction rays, which is what this file implements minus refraction.

  • Matt Pharr, Wenzel Jakob and Greg Humphreys, Physically Based Rendering (4th edition, free at pbr-book.org): BVH construction and traversal in detail.

  • JoaoLeonello/tiny-raytracer: the code from this post, the mirrors variant and the WebGL version.

bitwiseopsonmaster❯logout