Ray Tracing From Scratch in Python

<p>In this post I will give you a glimpse of what computer graphics algorithms may look like. I will explain the&nbsp;<em>ray tracing</em>&nbsp;algorithm and show a simple implementation in Python.</p> <p>By the end of this article you&rsquo;ll be able to make a program that will generate the above image, without making use of any fancy graphic library! Only&nbsp;<em>NumPy</em>. Isn&rsquo;t it crazy?! Let&rsquo;s dive in!</p> <p><em>P.S. This article is by no mean a complete guide / explanation of ray tracing, since this is such a vast subject, but rather an introduction for curious people :)</em></p> <h1>Prerequisites</h1> <p>We only need very basic vector geometry.</p> <ul> <li>If you have 2 points A and B &mdash; whatever the dimensionality: 1, 2, 3, &hellip;, n &mdash; then a vector that goes from&nbsp;<code>A</code>&nbsp;to&nbsp;<code>B</code>&nbsp;can be found by computing&nbsp;<code>B &mdash; A</code>&nbsp;(element-wise);</li> <li>The length of a vector &mdash; whatever the dimensionality &mdash; can be found by computing the square root of the sum of the squared components. The length of a vector&nbsp;<code>v</code>&nbsp;is denoted&nbsp;<code>||v||</code>;</li> <li>A&nbsp;<em>unit-vector</em>&nbsp;is a vector of length 1:&nbsp;<code>||v|| = 1</code>;</li> <li>Given a vector, another vector that points to the same direction but with a length of 1 can be found by dividing each component of the first vector by its length &mdash; this is called normalization:&nbsp;<code>u = v / ||v||</code>;</li> <li>Dot product for vectors. Specifically:&nbsp;<code>&lt;v, v&gt; = ||v||&sup2;</code>;</li> <li>Solving a quadratic equation;</li> <li>A bit of patience and imagination;</li> </ul> <p><a href="https://omaraflak.medium.com/ray-tracing-from-scratch-in-python-41670e6a96f9"><strong>Read More</strong></a></p>
Tags: Scratch Python