The Ray Tracer Challenge: Chapter 5 (p. 58) - Point with Distance to Origin

I think the following pseudocode from the book contains a bug:

function​ position(ray, t)
​ 	  ​return​ ray.origin + ray.direction * t
​ end​ ​function​

Isn’t it neccessary to normalize the direction vector first, before scaling it? The associated test case works only, because the given direction vector has already length = 1.

1 Like

I’m pretty sure the pseudocode is correct as published; when the direction is a unit vector, then t represents how many units from the origin the point will be. When the direction is not a unit vector, then t simply represents the distance to the point in units of that vector.

The latter may not seem very useful, but there are instances in the ray tracer where it’s really what you want. For instance, when the ray has been transformed by a scaling operation, the direction will not be a unit vector, but the position will still be computed as given, because the transformation ought to apply to the resulting point as well.

I hope that makes some kind of sense! Please let me know if I’ve only made things worse. :slight_smile:

1 Like