lib/ruby_marks/recognizer.rb
def find_marks(image, group)
      block = group.coordinates
      y = block.y1
      blocks = []
      blocks.tap do |chunks|
        while y < block.y2
          x = block.x1
          while x < block.x2
            if image[y][x] == ' '
              x += 1
              next
            end

            result = find_in_blocks(chunks, x, y)
            unless result
              result = find_block(image, x, y, '.', block)

              mark_width  = ImageUtils.calc_width(*result.values_at(:x1, :x2))
              mark_height = ImageUtils.calc_height(*result.values_at(:y1, :y2))

              if mark_width > group.mark_width_with_up_tolerance
                distance_x1 = x - result[:x1]
                distance_x2 = result[:x2] - x
                if distance_x1 <= distance_x2
                  result[:x2] = result[:x1] + group.mark_width
                else
                  result[:x1] = result[:x2] - group.mark_width
                end
              end

              if mark_height > group.mark_height_with_up_tolerance
                distance_y1 = y - result[:y1]
                distance_y2 = result[:y2] - y
                if distance_y1 <= distance_y2
                  result[:y2] = result[:y1] + group.mark_height
                else
                  result[:y1] = result[:y2] - group.mark_height
                end
              end

              chunks << result unless chunks.any? { |b| b == result }

            end
            x += 1
          end
          y += 1
        end
      end
    end
def find_marks(image, group)
      block = group.coordinates
      y = block.y1
      blocks = []
      blocks.tap do |chunks|
        while y < block.y2
          x = block.x1
          while x < block.x2
            if image[y][x] == ' '
              x += 1
              next
            end

            result = find_in_blocks(chunks, x, y)
            unless result
              result = find_block(image, x, y, '.', block)

              mark_width  = ImageUtils.calc_width(*result.values_at(:x1, :x2))
              mark_height = ImageUtils.calc_height(*result.values_at(:y1, :y2))

              if mark_width > group.mark_width_with_up_tolerance
                distance_x1 = x - result[:x1]
                distance_x2 = result[:x2] - x
                if distance_x1 <= distance_x2
                  result[:x2] = result[:x1] + group.mark_width
                else
                  result[:x1] = result[:x2] - group.mark_width
                end
              end

              if mark_height > group.mark_height_with_up_tolerance
                distance_y1 = y - result[:y1]
                distance_y2 = result[:y2] - y
                if distance_y1 <= distance_y2
                  result[:y2] = result[:y1] + group.mark_height
                else
                  result[:y1] = result[:y2] - group.mark_height
                end
              end

              chunks << result unless chunks.any? { |b| b == result }

            end
            x += 1
          end
          y += 1
        end
      end
    end
rubocop:disable Metrics/CyclomaticComplexity
 rubocop:disable Metrics/MethodLength
 rubocop:disable Metrics/PerceivedComplexity
 rubocop:disable Metrics/BlockNesting