Created
October 20, 2017 10:02
-
-
Save gsingharoy/f3e2d82f21694ba29ab1dfa2a04cc2ec to your computer and use it in GitHub Desktop.
A service to calculate the manhattan distance from a certain point.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This class represents a service which can be used to find all the reachble points | |
# from a point specified in a matrix. | |
# Any point is reachable only by moving horizontally or vertically and not diagonally. | |
class ManhattanDistance | |
attr_reader :block_matrix, :center_x, :center_y | |
# Takes input of a matrix which has values all 0 and only one 1 | |
# The distance would be calculated from the point which has 1 | |
def initialize(block_matrix) | |
@block_matrix = block_matrix | |
initialize_center_coords | |
end | |
# Makes all the reachable from the center of the matrix as 1 | |
def blur(distance) | |
start_x = center_x - distance > 0 ? center_x - distance : 0 | |
start_y = center_y - distance > 0 ? center_y - distance : 0 | |
(start_x..(start_x + (2 * distance))).each do |col_index| | |
next if col_index >= col_length | |
(start_y..(start_y + (2 * distance))).each do |row_index| | |
break if row_index >= row_length | |
# Make point 1 if it is reachable to the center | |
@block_matrix[row_index][col_index] = 1 if point_reachable?(col_index, row_index, distance) | |
end | |
end | |
block_matrix | |
end | |
# prints the block matrix in a readable form | |
def to_s | |
block_matrix.each do |row| | |
print("#{row.join(" ")}\n") | |
end | |
end | |
private | |
# Finds the center coordinates in the matrix and initializes it to instance vars | |
def initialize_center_coords | |
block_matrix.each_with_index do |row, i| | |
row.each_with_index do |col, j| | |
if col == 1 | |
@center_x = j | |
@center_y = i | |
break | |
end | |
end | |
break if !center_x.nil? && !center_y.nil? | |
end | |
end | |
# checks if the x, y coordinates are reachable from the center | |
def point_reachable?(x, y, distance) | |
(center_x - x).abs + (center_y - y).abs <= distance | |
end | |
def col_length | |
@col_length ||= block_matrix.first.size | |
end | |
def row_length | |
@row_length ||= block_matrix.size | |
end | |
end | |
# Helper module to initilialize a matrix | |
module MatrixInitializer | |
extend self | |
# Returns an empty matrix of size mentioned here | |
# @example MatrixInitializer.of_size(6,4) | |
def of_size(rows, cols) | |
col_result = (1..cols).map{ |_| 0 } | |
(1..rows).map{ |_| col_result.dup } | |
end | |
end | |
# Use the following code block to check how the above services can be used | |
# | |
# | |
# | |
# | |
# matrix = MatrixInitializer.of_size(6,4) | |
# matrix[3][1] = 1 | |
# md = ManhattanDistance.new(matrix) | |
# md.to_s | |
# md.blur(2) | |
# p '##############################' | |
# md.to_s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment