# How can I implement next event estimation?

I am trying to implement next event estimation to reduce the noise of path tracer and I need a pseudocode.

Edit: I know how to sample direct illumination but the visibility function is the real problem I face. How to code the visibility function without iterating over the objects in the scene?

You can provide an intersect_any(ray, scene, min_t, max_t) function, that returns true at the first intersection. So it has the option to exit early compared to the usual intersect function. Additionally you do not care about normals, texture coordinates, positions, so you can avoid computing those in many cases. You need max_t though to limit the ray to the segment between the two points you are testing.