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?