<p>This is a recursive function (it calls itself). Each time it calls itself it does so with <code>depth + 1</code> instead of <code>depth</code>. When it is called with <code>depth &gt; MAX</code>, it simply returns <code>Black</code> rather than call itself again. This ensures that the function will eventually stop recursing and return a value. It is the colour equivalent of returning zero.</p>

<p>You can visualise this as a light ray reflecting from multiple surfaces successively. Each time the ray reflects from a surface the same function is used to calculate the colour of the light in that direction. If you set <code>MAX</code> to 0, there will be no reflections in your resulting image. If you set <code>MAX</code> to 2, there will be reflections of reflections which will look more realistic, but take longer to calculate.</p>

<p>Intuitively, MAX is the number of surfaces the ray will bounce off before it is decided what colour should be displayed.</p>

CC BY-SA 3.0
2015-12-28T16:30:32.037Z
2015-12-28T16:36:12.337Z
2015-12-28T16:36:12.337Z
trichoplax is on Codidact now
<p><a href="https://i.sstatic.net/KbrW7m.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/KbrW7m.png" alt="ray tracing pseudocode" /></a></p>
<p>I don't understand the <code>if (depth &gt; MAX) return Black</code> part. Does it have something to do with shadows, because in other algorithms they shoot a shadow ray towards the light source to check for shadows but they don't have it here?</p>

CC BY-SA 3.0
2015-12-28T17:10:03.210Z
Hello and welcome. Are you trying to circumvent the [future MIT license](http://meta.stackexchange.com/questions/271080/the-mit-license-clarity-on-using-code-on-stack-overflow-and-stack-exchange?cb=1) by posting a image or what up with that?
joojaa
<raytracing>
Ray tracing pseudocode shadow
user2976568
# Ray tracing pseudocode shadow

(Asked by: username_0 on 2015-12-28)

[![ray tracing pseudocode](https://i.sstatic.net/KbrW7m.png)](https://i.sstatic.net/KbrW7m.png)

I don't understand the `if (depth > MAX) return Black` part. Does it have something to do with shadows, because in other algorithms they shoot a shadow ray towards the light source to check for shadows but they don't have it here?

**Tags:** raytracing

> **Comments:**
> **username_1**: Hello and welcome. Are you trying to circumvent the [future MIT license](http://meta.stackexchange.com/questions/271080/the-mit-license-clarity-on-using-code-on-stack-overflow-and-stack-exchange?cb=1) by posting a image or what up with that?  

---

## 1 answers

### Answer by username_2 on 2015-12-28. Score: 7 | Accepted answer

This is a recursive function (it calls itself). Each time it calls itself it does so with `depth + 1` instead of `depth`. When it is called with `depth > MAX`, it simply returns `Black` rather than call itself again. This ensures that the function will eventually stop recursing and return a value. It is the colour equivalent of returning zero.

You can visualise this as a light ray reflecting from multiple surfaces successively. Each time the ray reflects from a surface the same function is used to calculate the colour of the light in that direction. If you set `MAX` to 0, there will be no reflections in your resulting image. If you set `MAX` to 2, there will be reflections of reflections which will look more realistic, but take longer to calculate.

Intuitively, MAX is the number of surfaces the ray will bounce off before it is decided what colour should be displayed.


---

