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
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 ![]()
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!
Popular Pragmatic Bookshelf topics
Other popular topics
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /zig
- /deepseek
- /php
- /scala
- /react-native
- /textmate
- /lisp
- /sublime-text
- /debian
- /nixos
- /agda
- /kubuntu
- /django
- /arch-linux
- /deno
- /revery
- /nodejs
- /ubuntu
- /manjaro
- /spring
- /diversity
- /lua
- /julia
- /markdown
- /slackware










