refactored the nearby method into it's own module
This commit is contained in:
parent
5b7c6f8be0
commit
42fb9be4ef
@ -2,6 +2,7 @@ require 'chunky_png'
|
|||||||
require 'matrix'
|
require 'matrix'
|
||||||
|
|
||||||
require 'edge_detect/version'
|
require 'edge_detect/version'
|
||||||
|
require 'edge_detect/nearby'
|
||||||
require 'edge_detect/matrix'
|
require 'edge_detect/matrix'
|
||||||
require 'edge_detect/gray_scaler'
|
require 'edge_detect/gray_scaler'
|
||||||
require 'edge_detect/differ'
|
require 'edge_detect/differ'
|
||||||
|
@ -5,6 +5,7 @@ module EdgeDetect
|
|||||||
# the horizontal and vertical differ.
|
# the horizontal and vertical differ.
|
||||||
#
|
#
|
||||||
class Differ
|
class Differ
|
||||||
|
include Nearby
|
||||||
|
|
||||||
##
|
##
|
||||||
# Initializes a new differ with a given image
|
# Initializes a new differ with a given image
|
||||||
@ -25,24 +26,5 @@ module EdgeDetect
|
|||||||
def [](x, y)
|
def [](x, y)
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
@ -3,7 +3,7 @@ module EdgeDetect
|
|||||||
# Grayscales a given image
|
# Grayscales a given image
|
||||||
#
|
#
|
||||||
class GrayScaler
|
class GrayScaler
|
||||||
|
include Nearby
|
||||||
##
|
##
|
||||||
# Initializes a new GrayScaler with the given image
|
# Initializes a new GrayScaler with the given image
|
||||||
#
|
#
|
||||||
@ -26,24 +26,5 @@ module EdgeDetect
|
|||||||
0
|
0
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
26
lib/edge_detect/nearby.rb
Normal file
26
lib/edge_detect/nearby.rb
Normal 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
|
Loading…
Reference in New Issue
Block a user