Created
May 25, 2014 17:31
-
-
Save defsan/2b9fa7ce947adf2732d8 to your computer and use it in GitHub Desktop.
Circle in matrix
This file contains hidden or 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
require 'matrix' | |
# Matrix enhance | |
class Matrix | |
def []=(i, j, x) | |
@rows[i][j] = x | |
end | |
end | |
# | |
center = 6 | |
radius = 5 | |
width = (radius+2)*2 | |
matrix = Matrix.build(width,width){|y,x| [0,0]} | |
vector = [] | |
output = "" | |
width.times do |y| | |
width.times do |x| | |
nx = x-center | |
ny = y-center | |
distance = Math.hypot(nx, ny) | |
puts "#{x}:#{y} = #{distance}" | |
if distance <= radius | |
matrix[x,y] = [nx,ny] | |
vector << [nx,ny] | |
output +="1" | |
else | |
output += "0" | |
end | |
end | |
output += "\n" | |
end | |
puts "\n\n" + output | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment