InfoQ Homepage News Ray Tracers using C# and LINQ

Ray Tracers using C# and LINQ


Luke H. shows how to write a ray tracer using C# 3 and LINQ in about 400 lines of code.

Ray tracing is a method of rendering 3D images on a two dimensional surface such as a computer screen or a printed page. It usually relies on backtracking a virtual light beam from a pixel on the 2D surface to the first three-dimensional object it strikes. Depending on the texture of the object it strikes, it may continue to backtrack until it reaches a light source.

Luke uses LINQ to simplify code for sorting and filtering collections, as well as a replacement for loops when calculating intersections. Object and collection initializers are demonstrated, though they do not significantly change the code in this example.

The use of lambda expressions instead of subclasses and overrides or explicit delegates is the real winner for increasing clarity and reducing lines of code.

A write-up on the ray tracer and the source code is available on Luke's blog.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • You should check out the source code...

    by Kurt Christensen,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    ...because it's pretty damn cool. To say that it's ~400 lines doesn't even do it justice; if you ignore the various utility classes (e.g., Vector, Camera, etc.), the code is just amazingly small and easy to read.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.