Diana's Domain






Flatland Ray-Tracer


A "ray tracer" is a kind of graphics renderer which constructs images by determining how mathematically represented light rays interact with various objects within a scene. In order to save computational time and resources, the light rays in a ray tracer are typically imagined as originating from the viewer's position as opposed to some light source. Simple geometrical formulations can be used (really the ray abstraction itself) such that the mathematics of a light ray stay the same moving in either direction. Computing from the viewer outward means a lot fewer rays to worry about since the system only considers those which have a direct effect on what the viewer perceives.

Flatland Ray-Tracer is written in C++ and uses Simple DirectMedia Layer (SDL) to handle window creation and basic pixel-setting functionality. It currently only runs on Windows, although I intend to compile the program for other operating systems in the future. The images on this page are 1 pixel tall, although they are generally displayed a little taller so as to make them easier to make out.

Ray tracers are more-often-than-not built to view 3D worlds, not unlike the one we happy humans live in and navigate every day. The ray tracer presented here, however, is a 2D ray tracer. In fact, it is a Flatland Ray-Tracer. It generates images in accordance with what the denizens of Edwin Abbott's fictional Flatland might perceive within the 2D plane-world in which they are embedded.


A. Square

In much the same way as we perceive our 3D world as if projected on a 2D viewport, Flatlanders see the 2D world in which they live in one dimension. Hence, the contents of their environment (chiefly other Flatlanders) appear as 1-dimensional lines of various lengths.

Depth-Perception by Distance

Like those who live in the three-dimensional Spaceland, the shape-people of Flatland can perceive changes in distance based off of the apparent size of something or someone within their fields of vision. As narrator A. Square explains,

If our friend comes closer to us we see his line become larger; if he leaves us it becomes smaller: but still he looks like a straight line; be he a Triangle, Square, Pentagon, Hexagon, Circle, what you will—a straight Line he looks and nothing else. (4)

This is what a resident of Flatland might perceive as they approached or backed-away-from one of their square acquaintances, directly facing one of the square's sides. The change in distance between viewer and square-friend is reflected in the changing size of the apparent line.

The apparent image of the square is smaller at larger distances because as the square gets further away, fewer of the rays being emitted from the viewer's position are intersecting it. This occurs because the vision rays are arranged in a cone-like structure.

Depth-Perception by Fog

Another feature of the Flatland Ray-Tracer is atmospheric perspective.


Flatland is populated by various shapes, including straight lines, triangles, squares, pentagons, and "circles". Because to the perceiver everyone ends up looking like similar 1-dimensional lines, Flatlanders are confronted by the problem of being able to tell the shape (and consequently social class) of another Flatlander. An important depth cue in this respect is the pervasive fog which sits across the Flatlanders' home plane. A. Square discusses the art of sight recognition:

"I am about to appear very inconsistent. In previous sections I have said that all figures in Flatland present the appearance of a straight line; and it was added or implied that it is consequently impossible to distinguish by the visual organ between individuals of different classes: yet now I am about to explain to my Spaceland critics how we are able to recognize one another by the sense of sight[...]

That this power exists in any regions and for any classes is the result of Fog; which prevails during the greater part of the year in all parts save the torrid zones.

[...]wherever there is a rich supply of Fog objects that are at a distance, say of three feet, are appreciably dimmer than those at a distance of two feet eleven inches; and the result is that by careful and constant experimental observation of comparative dimness and clearness, we are enabled to infer with great exactness the configuration of the object observed." (18)

They infer distance from dimness. If a Flatlander views another Flatlander such that the one is looking straight on at the other's angle, the two sides which constitute that angle will recede into the distance at a rate corresponding to the angle's measure. The larger angles of shapes with more sides recede into the fog less rapidly than do the smaller angles found on shapes with fewer sides.



Isoceles Triangle, Equilateral Triangle, Square, and Pentagon

In this way (and with the training in this art provided to the higher classes) the various shapes/social classes can be determined by sight. The other reliable option is to feel another Flatlander's angle, but this method is complicated by certain asymmetrical social rules, not to mention the risk of impalement on a pointy Triangle.

It may be noticed that reliable sight recognition would only work for regular figures where all the sides are of the same length and all the angles have the same measure. In order for society to function smoothly, a shape born "Irregular" is either euthanized at adolescence if outside some established boundary of deviance or otherwise forced to live confined within an office as a charge of the state. Isoceles Triangles are an exception—they simply occupy the lowest rung of the polygon social ladder, hoping that their offspring will be born with an angle configuration slightly closer than their own to 60°-60°-60°.

A. Square says that objects at a further distance appear dimmer than closer objects because of the presence of the Fog. In our experience, further objects have a lower contrast when compared to the background color—that is, their color is closer to the background color, and far away hills on a clear day look blue like the sky. In fact, if far away objects are appearing quite white (as in the case of haze or fog or whatnot), they could potentially look brighter than objects which are near. Perhaps by dimness A. Square simply means the loss of foreground saturation. The movement towards greater paleness and lack of clarity as one looks into a foggy distance could be said to be a kind of dimming. Alternatively, perhaps the atmosphere is different in Flatland and the sky there is dark/dim/blackish. For now we imagine the Flatland Fog as being white. As for the coded implementation of said distance Fog, one *could* take steps towards a nuanced simulation of ray scattering itself—this sounds like a very interesting project, but perhaps one for another day. A simple way to get the wanted visual effects for now is to simply consider the distance of the viewer from each ray intersection point at the time of film-line pixel color assignment and adjust the constrast (relative to some chosen "background" color) accordingly.

The Colour Revolt

In the absence of a trained sensitivity for color gradations, making out the starts and ends of the shape sides is much easier with side-specific coloration. In fact, there was a period of time when, following the example of one audacious Pentagon, Flatlanders painted their various sides different colors in order to assert their own individuality within a sea of geometric conformity.

Wherever Chromatistes,—for by that name the most trustworthy authorities concur in calling him, —turned his variegated frame, there he at once excited attention, and attracted respect. No one now needed to "feel" him; no one mistook his front for his back; all his movements were readily ascertained by his neighbours without the slightest strain on their powers of calculation[...]

The side-coloration fad became very popular:

"The fashion spread like wildfire. Before a week was over, every Square and Triangle in the district had copied the eample of Chromatistes, and only a few of the more conservative Pentagons still held out. A month or two even found the Dodecagons infected with the innovation. A year had not elapsed before the habit had spread to all but the very highest of the Nobility." (26)



The Flatland Priests make the claim of being "Circles" but are in reality polygons with uncountably many sides. Subjecting one to side-coloration, he looks quite festive with the curtain pulled back on his illusory roundness.



The Flatlanders' Colour Revolt was eventually suppressed by the ruling classes following massive political upheaval, leaving it only a shared cultural memory:

"Immoral, licentious, anarchical, unscientific—call them by what names you will—yet, from an aesthetic point of view, those ancient days of the Colour Revolt were the glorious childhood of Art in Flatland—a childhood, alas, that never ripened into manhood, nor even reached the blossom of youth. To live was then in itself a delight, because living implied seeing. Even at a small party, the company was a pleasure to behold; the richly varied hues of the assembly in a church or theatre are said to have more than once proved too distracting for our greatest teachers and actors; but most ravishing of all is said to have been the unspeakable magnificence of a military review.

The sight of a line of battle of twenty thousand Isoceles suddenly facing about, and exchanging the sombre black of their bases for the orange and purple of the two sides including their acute angle; the militia of the Equilateral Triangles tricoloured in red, white, and blue; the mauve, ultra-marine, gamboge, and burnt umber of the Square artillery-men rapidly rotating near their vermilion guns; the dashing and flashing of the five-coloured and six-coloured Pentagons and Hexagons careering across the field in their offices of surgeons, geometricians, and aides-de-camp—all these may well have been sufficient to render credible the famous story how an illustrious Circle, overcome by the artistic beauty of the forces under his command, threw aside his marshal's baton and his royal crown, exclaiming that he henceforth exchanged them for the artist's pencil." (27)

Meanwhile, in Spaceland:

Women

While in Flatland all the Women appear as straight lines, in Flatland Ray-Tracer all objects of the Woman class are ontologically Polygons, i.e., they are derived from the same parent class as every other Flatlander. In order to make them appear as straight lines, an extra shear transformation is applied to squeeze their sides until they look 1-dimensional.



Flatland Women are quite dangerous. When viewed straight on, they are nearly invisible.

Like Triangles, they are also sharp—and oppressed. Women are denied education and any existence as independent persons. In addition to lacking a third dimension, Flatland also lacks egalitarianism.

A Visitor From Spaceland

Flatland, of course, is not a tale merely about a 2D-plane world and its inhabitants. A. Square also finds himself experiencing worlds of different dimensions. In the chapters before he is lifted from the limits of Flatland and sees Spaceland for himself, A. Square is visited by a mysterious stranger who claims to be from another realm. When verbal explanations are not enough to convince the narrator of his visitor's extra-dimensional origins, the stranger resorts to demonstration:



As the visiting sphere moved up and down through his home plane, A. Square would see something of the sort:

For a symbolic reason why the above is true:

Polygon Representation: Intersecting Half-Spaces

Polygons and polytopes of higher dimensions can be represented as the intersection of the half-spaces defined by a set of linear inequalities \(Ax \le b\), where \(A\) is a matrix of coefficients, \(x\) is a vector of variables, and \(b\) is a vector of constants.

On the left are ray-traced images of various polygons made using this intersecting half-space representation. On the right are top-down images of the half-spaces made with the Desmos graphing calculator. Listed also are the calculated sets of linear inequalities for these different polygons when the circumscribed circle is centered at the origin with a radius of 100 units.



Triangle

Isoceles Triangle

Angle measures 20°, 80°, 80°





Half-Spaces:

$$-5.671282x + y \le 100 \\ -y \le 93.969262\\ 5.671282x + y \le 100 $$


This is essentially:

$$-\tan (80) x + y \le 100 \\ -y \le 93.969262\\ \tan (80) x + y \le 100 $$







Equilateral Triangle

Angle measures 60°, 60°, 60°





Half-Spaces:

$$-1.732051x + y \le 100 \\ -y \le 50\\ 1.732051x + y \le 100 $$


This is approximately:

$$-\sqrt 3 x + y \le 100 \\ -y \le 50\\ \sqrt 3 x + y \le 100 $$







Square




Half-Spaces:

$$-x + y \le 100 \\ -x - y \le 100\\ x - y \le 100\\ x + y \le 100 $$







Pentagon




Half-Spaces:

$$-0.726543x + y \le 100 \\ -3.077684x - y \le 261.803399 \\ -y \le 80.901699 \\ 3.077684x - y \le 261.803399 \\ 0.726543x + y \le 100 $$

This is essentially:

$$-\tan (36) x + y \le 100 \\ -\tan (72)x - y \le 100(\phi + 1) \\ -y \le 80.901699 \\ \tan (72)x - y \le 100(\phi + 1) \\ \tan (36)x + y \le 100 $$







Hexagon






Half-Spaces:

$$-0.57735x + y \le 100 \\ -x \le 86.602540 \\ -0.57735x - y \le 100 \\ 0.57735x - y \le 100 \\ x \le 86.602540 \\ 0.57735x + y \le 100 \\ $$

This is essentially:

$$-\sqrt \frac{1}{3} x + y \le 100 \\ -x \le 86.602540\\ -\sqrt \frac{1}{3} x - y \le 100 \\ \sqrt \frac{1}{3} x + y \le 100 \\ x \le 86.602540 \\ \sqrt \frac{1}{3} + y \le 100 \\ $$














Heptagon






Half-Spaces:

$$-0.481575x + y \le 100 \\ -4.381286x + y \le 404.891734 \\ -1.25396x - y \le 144.504187 \\ -y \le 90.096887 \\ 1.25396x - y \le 144.504187 \\ 4.381286x + y \le 404.891734 \\ 0.481575x + y \le 100 $$







Octagon






Half-Spaces:

$$-0.414214x + y \le 100 \\ -2.414214x + y \le 241.421356 \\ -2.414214x - y \le 241.421356 \\ -0.414214x - y \le 100 \\ 0.414214x - y \le 100 \\ 2.414214x - y \le 241.421356 \\ 2.414214x + y \le 241.421356 \\ 0.414214x + y \le 100 $$

This is essentially:

$$-(\sqrt 2 - 1)x + y \le 100 \\ -(\sqrt 2 + 1)x + y \le 100(\sqrt 2 +1)\\ -(\sqrt 2 + 1)x - y \le 100(\sqrt 2 +1) \\ -(\sqrt 2 - 1)x - y \le 100 \\ (\sqrt 2 - 1)x - y \le 100 \\ (\sqrt 2 + 1)x - y \le 100(\sqrt 2 +1) \\ (\sqrt 2 + 1)x + y \le 100(\sqrt 2 +1) \\ (\sqrt 2 - 1)x + y \le 100 $$











Nonagon

A 9-sided polygon can also be called an "enneagon".






Half-Spaces:

$$-0.36397x + y \le 100 \\ -1.732051x + y \le 187.938524 \\ -5.671282x - y \le 541.147413\\ -0.8391x - y \le 122.668160 \\ -y \le 93.969262 \\ 0.8391x - y \le 122.668160 \\ 5.671282x - y \le 541.147413 \\ 1.732051x + y \le 187.938524 \\ 0.36397x + y \le 100 $$

This is essentially:

$$-\tan (20)x + y \le 100 \\ -\tan (60)x + y \le 187.938524 \\ -\tan (80) x - y \le 541.147413\\ -\tan (40)x - y \le 122.668160 \\ -y \le 93.969262 \\ \tan (40)x - y \le 122.668160 \\ \tan (80)x - y \le 541.147413 \\ \tan (60)x + y \le 187.938524 \\ \tan (20)x + y \le 100 $$













Decagon







Half-Spaces:

$$-0.32492x + y \le 100 \\ -1.376382x + y \le 161.803399\\ -x \le 95.105652\\ -1.376382x - y \le 161.803399 \\ -0.32492x - y \le 100 \\ 0.32492x - y \le 100 \\ 1.376382x - y \le 161.803399 \\ x \le 95.105652\\ 1.376382x + y \le 161.803399 \\ 0.32492x + y \le 100 $$

This is essentially:

$$-\tan (18) x + y \le 100 \\ -\tan (54) x + y \le 100 \phi\\ -x \le 95.105652\\ -\tan (54)x - y \le 100 \phi \\ -\tan (18) x - y \le 100 \\ \tan (18) x - y \le 100 \\ \tan (54) x - y \le 100 \phi \\ x \le 95.105652\\ \tan (54) x + y \le 100 \phi \\ \tan (18) x + y \le 100 $$













The algorithm for going from the number of sides \(n\) to the coefficients of the linear inequalities which define the half-spaces which make up the polygon is not particularly complicated. In summary, it first determines the proper rotation angle based on n-fold symmetry, where n is the number of sides in the polygon. This means dividing 360/n.

Starting at the "top" vertex (where θ = 90), it then travels counter-clockwise around the circle, incrementing the current position θ by the rotation value it determined earlier. It uses the parametric circle equation to convert from Polar to Cartesian coordinates. Once it has made a trip around the circle and discovered all the vertices, it goes through the vertices in counterclockwise order and finds the equation of the line connecting each adjacent pair. In this manner the coefficients of the lines of the various sides are discovered. This equation is put into so-called "standard form", although in the form of an inequality: i.e., Ax + By <= C.

Here is what the function prints to the console as it works its way around a hexagon:

Now, what about the special case of isoceles triangles? Most of the algorithm is the same. The difference comes in the rotation angle determination. In any regular polygon, all we had to do was divide 360 by the number of sides in order to get the rotation angle in accordance with n-fold rotational symmetry. With the isoceles triangle, two of these rotation movements (the first and third) will be of the same value, while one (the second) will be different. Thankfully, it is simple to discern the lonely rotation angle—this is the central angle of which the base is also the side opposite the "top" angle of the triangle. This angle is simply twice the value of the top angle. A ready-made proof of this fact is given in Euclid: Bk. III, Prop. 20.

Democratizing the Circle

In Flatland Ray-Tracer, all objects of the Polygon class have a true Circle. In OOP parlance, this is a "has-a" relationship between two classes. The Circle possessed by each object serves as its bounding circle. As an optimization, a bounding circle allows for early rejection of rays which do not fall within it. Since the polygon half-spaces extend infinitely, the bounding circle "caps" off these lines (and in the case of regular polygons always does so at a vertex).

Transformation Matrices

Transformations allow for shapes to change and move around a space. Affine transformations include linear transformations such as rotation, scaling, and shearing, as well as non-linear transformations such as translation. Transformations are often representated as matrices which can be multiplied by coordinates or other information in order to modify them. All objects of the Polygon class contain an inverted transform matrix which allows for rotation, translation, and shearing, and is applied to the ray at the time of intersection testing.

A versatile way to handle transformations in a ray tracer is to apply the inverse of an object's composed transformational matrix to the ray at the time of intersection testing as opposed to applying the matrix to the object representation itself (which may or may not contain explicit vertex information, depending on what you're doing—that being said, in the absence of vertex coordinates, transformation matrices can still be applied, for example, to the coefficients of a line, but when used on many-sided polygons this ends up being rather convoluted to implement and all the effects are backwards).

Something non-obvious about applying the composed transform inverse to the ray is that the direction vector should only be transformed by the linear part, while the emission point should be transformed by the whole thing.

Below are some transformation matrices and their inverses.

Translation

An affine transformation. Helpful link about homogeneous coordinates.

$$ \left[ \begin{array}{cc} 1 & 0 & tx \\ 0 & 1 & ty \\ 0 & 0 & 1 \\ \end{array} \right]^{-1} = \left[ \begin{array}{cc} 1 & 0 & -tx \\ 0 & 1 & -ty \\ 0 & 0 & 1 \\ \end{array} \right] $$

Rotation

$$ \left[ \begin{array}{cc} cos(\theta) & -sin(\theta) \\ sin(\theta) & cos(\theta) \\ \end{array} \right]^{-1} = \left[ \begin{array}{cc} cos(\theta) & sin(\theta) \\ -sin(\theta) & cos(\theta) \\ \end{array} \right] $$

Shearing

Shear matrices come in two varieties: horizontal and vertical. With \(f\) as the shear factor,

$$ \left[ \begin{array}{cc} 1 & f \\ 0 & 1 \\ \end{array} \right]^{-1} = \left[ \begin{array}{cc} 1 & -f \\ 0 & 1 \\ \end{array} \right] $$ Horizontal Shear
$$ \left[ \begin{array}{cc} 1 & 0 \\ f & 1 \\ \end{array} \right]^{-1} = \left[ \begin{array}{cc} 1 & 0 \\ -f & 1 \\ \end{array} \right] $$ Vertical Shear

The order in which transformational matrices are applied does make a difference. If a shearing operation is being included, a reasonable order to compose the matrices is \(TRHS\), that is, perform scaling, shearing, rotation, and then finally translation. Although my ray-tracer doesn't actually include a scaling matrix, some outside testing would indicate that applying a scale before a shear gives a more dramatic result, as the shear operation takes the size of the shape into account.

In the case of applying a inverted transformation matrix to a ray, the reasonable order to multiply the individual inverted matrices would be \(S^{-1}H^{-1}R^{-1}T^{-1}\), as this is equal to \((TRHS)^{-1}\)


References