correct position
incorrect position
I changed one line in my text-layout code to be compatible with immutable rectangles:
- metrics.area.location = metrics.area.location.neg.add offset + metrics.area = rect metrics.area.location.neg.add(offset), metrics.area.sizeI can tell you there were no bugs in the new immutable Rectangle class.
Can you spot the bug?
There is no bug. The new line is correct, and yet that's all I changed.
It turns out I originally had a bug due to a mutation I wasn't taking into account, but that mutation made the layout algorithm work. When I stopped mutating the rectangle object, the "bug" stopped making the algorithm work. The reason it took me hours to find this bug was the new code was "right", but the algorithm was wrong.
Here's what was actually wrong, in 3 code snippets (CoffeeScript):
1) The original code that worked but had an accidental mutation:
updateTextLayout: -> offset = point() @area = @metricsList[0].area # object assigned here for metrics in @metricsList metrics.area.location = metrics.area.location.neg.add offset # gets mutated here the first time through the loop offset = offset.add 0, @fontSize @area = @area.union metrics.area @size = @area.size2) Code with one line changed that seemed correct but didn't work:
updateTextLayout: -> offset = point() @area = @metricsList[0].area for metrics in @metricsList metrics.area = rect metrics.area.location.neg.add(offset), metrics.area.size # @area no longer gets mutated offset = offset.add 0, @fontSize @area = @area.union metrics.area @size = @area.size3) Final working code:
updateTextLayout: -> offset = point() @area = null # FIX - don't capture the wrong value here for metrics in @metricsList metrics.area = rect metrics.area.location.neg.add(offset), metrics.area.size offset = offset.add 0, @fontSize @area = if @area then @area.union metrics.area else metrics.area # FIX - capture the right value here @size = @area.size