Skip to content

Instantly share code, notes, and snippets.

@defsan
Created May 25, 2014 17:31
Show Gist options
  • Save defsan/2b9fa7ce947adf2732d8 to your computer and use it in GitHub Desktop.
Save defsan/2b9fa7ce947adf2732d8 to your computer and use it in GitHub Desktop.
Circle in matrix
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