Shadoka

Shadoka

The Ray Tracer Challenge: Gradient pattern is not a gradient

Hello @jamis and everyone,

I’ve implemented the gradient pattern as described in the book and it gives me this output:

The code for pattern_at (I’ve renamed it to ColorAt) is the following:

func (gp *GradientPattern) ColorAt(point math.Point) math.Color {
	distance := gp.ColorB.Subtract(gp.ColorA)
	fraction := point.X - gomath.Floor(point.X)

	return gp.ColorA.Add(distance.Mul(fraction))
}

As you can see it is not exactly a gradient, but the output makes sense to me.
Our spheres are centered at 0, 0, 0 in object space and at x = 0 the fraction evaluates to 0, which in turn means that we just return colorA.

But I highly suspect that this function is supposed to return an image of a smooth gradient from A to B.
I’m going mildly insane about this issue and if anyone got any advice I would greatly appreciate that.

Thanks! :slight_smile:

Marked As Solved

jamis

jamis

Author of Mazes for Programmers and 1 other title

The issue here is that your pattern space needs to be translated and scaled so that the [0,1] range fits the object you’re applying it to. If your sphere has radius 1, and is centered at (0,0), then the pattern (by default) will be covering [-1, 1] (from -x to +x), which means the gradient will be doubled (running once from [-1,0] and again from [0,1]). If you scale the pattern space by 2 (so the gradient runs from [0,2]) and then translate it by -x (so it runs from [-1,1]), it should look more like what you’re expecting.

Hopefully that helps! If you’d like me to restate/clarify anything, let me know.

Also Liked

Shadoka

Shadoka

Thank you so much, that indeed fixes it!

I am facepalming really hard right now, because I already tried scaling by 2 and translating by -1, but I never thought of having to combine those two…

Thanks again for your help and for writing that book, having a blast with it!

Popular Prag Prog topics Top

New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
redconfetti
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

Other popular topics Top

malloryerik
Any thoughts on Svelte? Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New
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
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
New
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg

View all threads ❯