MarkE

MarkE

Gradients: Checking my reasoning

Hi Jamis,

I’ve been working my way through Chapter 10: Generalising Patterns and came up agains a problem with the gradient. When applied to a Sphere, I found that the gradient was running twice across it with a hard line at X=0 (same issue as logged here: The Ray Tracer Challenge: Gradient pattern is not a gradient

I just want to double-check my interpretation of your answer is correct.

I have an AbstractPattern class which implements the general colour_at_object method. This translates the point to pattern-space via object-space thus:

    def colour_at_object(self, obj: "AbstractObject", p: Point) -> Colour:
        # Convert point to object space
        object_point = cast(Point, obj.inverse_transform * p)

        # Convert object point to pattern space
        pattern_point = cast(Point, self.inverse_transform * object_point)

        return self.colour_at(pattern_point)

Then, in the Gradient sub-class I’m implementing the colour_at method as follows:

    def colour_at(self, p: Point) -> Colour:
        distance = cast(Colour, self.b - self.a)
        # Map pattern space from [-1, 1] to [0, 1] so pattern doesn't repeat
        multiplier = (p.x + 1) / 2

        return self.a + distance * multiplier

This seems to correctly map the returned colour so that the gradient will now work across a unit sphere (breaks the tests, mind so I’ll need to fix those) … but just want to ensure that my reasoning is sound and there’s not something I’ve missed along the way?

First Post!

Shadoka

Shadoka

Hi MarkE,

I’m the author of the thread that you linked and I’m pretty sure I tried something similar to your approach at first :slight_smile:

The problem with your solution is that you shift your whole gradient, so it won’t render as you might want.

I rendered two images, one with my (I mean, Jamis really) implementation and one with yours.
Both gradients should go from red to green.

My implementation:

Your implementation:

And it makes sense when you think about it. Plug in -1 for X and the term in your method evaluates to 0. Which means that the leftmost point is going to get mapped to exactly the middle of the gradient.

Hope this helps!

Where Next?

Popular Pragmatic Bookshelf topics Top

New
ianwillie
Hello Brian, I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
Mmm
Hi, build fails on: bracket-lib = “~0.8.1” when running on Mac Mini M1 Rust version 1.5.0: Compiling winit v0.22.2 error[E0308]: mi...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
leba0495
Hello! Thanks for the great book. I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
Keton
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New

Other popular topics Top

PragmaticBookshelf
Write Elixir tests that you can be proud of. Dive into Elixir’s test philosophy and gain mastery over the terminology and concepts that u...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
wolf4earth
@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness. https://f...
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
New
New
PragmaticBookshelf
Fight complexity and reclaim the original spirit of agility by learning to simplify how you develop software. The result: a more humane a...
New
mindriot
Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New

Sub Categories: