pdarnowsky

pdarnowsky

The Ray Tracer Challenge: use of a type code (pg. 3-4)

@jamis

After introducing the ideas of tuples, vectors, and points, the book goes on to say:

But looking at (4, -4, 3) and (-4, 4, -3) , it’s impossible to know that one is a point
and the other is a vector. Let’s add a fourth component to these (x, y, z) tuples,
called w , to help us tell them apart. Set w to 1 for points, and 0 for vectors.
Thus, your point becomes (4, -4, 3, 1) , and your vector becomes (-4, 4, -3, 0) .
Now, the choice of 0 or 1 for w probably seems arbitrary just now, but sit
tight! It’ll make more sense when you get to Chapter 3, Matrices, on page 25,
where it turns out to be rather important for multiplying matrices and tuples.

This advice seems misguided to me, as it amounts to using the fourth component as a type code, and not even a symbolic one–you just have to remember that 1.0 in the fourth component means a vector and 0.0 indicates a point. Especially given that this book appears to be aimed towards fairly junior developers, that’s not a practice we should be encouraging.

I agree that it’s necessary to differentiate between vectors and points, and the best way to do it depends largely on the language you’re using. That said, every mainstream language that I know of has a better way to do that than what is suggested here.

First Post!

srmo

srmo

@jamis

I feel this. But let me give my two cents. I’m coding in Java.
I’ve just started the book today and got stuck when it came to implementation of the different operations on Tuples, Points and Vectors.

TL;DR: in the end, the current description in the book allows for two main points:

  • it has me thinking really hard on what the best approach is. And in the spirit of TDD, I came to the realisation that the stage I’m in now needs me to not overthink all of that too early and go with the flow and trust the process.
  • the loose description gives you the full freedom. Sticking to only point and vector is a too narrow view. Using Tuple with the convenient “w” component allows for a broader understanding of the underlying mechanisms

Here’s a more detailed description of what happened to me:
I’ve started immediately with a type system, Tuple, Point and Vector. Now, may it be to language restrictions or inherently complex design, implementing the different rules per Type seemed to create an enormous mess. I.e. is Tuple allowed to add two “points” but the Point type isn’t? Does each type need an extra implementation to return the correct type, depending on the operation (e.g. returning a vector when adding point+vector)?
Does every type have to implement this? The inheritence tree looks like garbage after a few tries.
From my point of view: using narrow types requires much more design skill from a beginner developer and is more prone to end up in a mess that is really hard to refactor your way out of.
Using a “flag” on a single type is way easier for the initial design.

My current solution is to just stick to the Tuple type with some convenience methods. This is clean and I expect most flexibility with this approach.

Popular Pragmatic Bookshelf topics Top

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
New
jskubick
I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New