refactored the nearby method into it's own module

This commit is contained in:
Tim Kächele 2016-05-31 18:05:18 +02:00
parent 5b7c6f8be0
commit 42fb9be4ef
5 changed files with 46 additions and 56 deletions

View File

@ -2,6 +2,7 @@ require 'chunky_png'
require 'matrix'
require 'edge_detect/version'
require 'edge_detect/nearby'
require 'edge_detect/matrix'
require 'edge_detect/gray_scaler'
require 'edge_detect/differ'

View File

@ -5,6 +5,7 @@ module EdgeDetect
# the horizontal and vertical differ.
#
class Differ
include Nearby
##
# Initializes a new differ with a given image
@ -25,24 +26,5 @@ module EdgeDetect
def [](x, y)
raise NotImplementedError
end
##
# Returns the nearby diffs of the given coordinates (x, y) with respect to
# the given radius.
#
# @return [Array<Array<Integer>>] a two dimensional array with the diffs.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end

View File

@ -3,7 +3,7 @@ module EdgeDetect
# Grayscales a given image
#
class GrayScaler
include Nearby
##
# Initializes a new GrayScaler with the given image
#
@ -26,24 +26,5 @@ module EdgeDetect
0
end
end
##
# Returns the nearby grayscale values of the given coordinates (x, y) with
# respect to the given radius.
#
# @return [Array<Array<Integer>>] a two dimensional array with the grayscale values.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end

26
lib/edge_detect/nearby.rb Normal file
View File

@ -0,0 +1,26 @@
module EdgeDetect
##
# The nearby module provides a method that uses the [x, y] method
# to query a two dimensional array.
#
module Nearby
##
# Returns the nearby values of the given coordinates (x, y) with respect to
# the given radius.
#
# @return [Array<Array<Value>>] a two dimensional array with the Values.
#
def nearby(x, y, radius)
output = []
((x - radius)..(x + radius)).each do |i|
row = []
((y - radius)..(y + radius)).each do |j|
row << self[i, j]
end
output << row
end
output
end
end
end