Solution: Generating Geometric Masks
Understand the solution to the “Generating Geometric Masks” challenge.
We'll cover the following...
We'll cover the following...
Solution
Let's execute the following solution code and see how it works:
Press + to interact
C++
Files
require 'mask'require 'chunky_png'class ConcentricSquaresMask < Maskdef initialize(band_count, band_size)size = 2 * (band_count * 2 - 1) * band_sizesuper(size, size)@mid = size / 2@mid.times do |i|block = i / band_sizemirror_octantwise(i, block.even?)end# add a narrow bands to link the ringssize.times do |i|self[@mid, i] = trueself[@mid - 1, i] = trueself[i, @mid] = trueself[i, @mid - 1] = trueendenddef mirror_octantwise(i, value)(i + 1).times do |j|self[@mid - j - 1, @mid - i - 1] = valueself[@mid - i - 1, @mid - j - 1] = valueself[@mid + j, @mid - i - 1] = valueself[@mid + i, @mid - j - 1] = valueself[@mid - j - 1, @mid + i] = valueself[@mid - i - 1, @mid + j] = valueself[@mid + j, @mid + i] = valueself[@mid + i, @mid + j] = valueendendend
Code explanation
Lines 4–19: ...