RosettaCodeData/Task/Constrained-random-points-on-a-circle/Ruby/constrained-random-points-on-a-circle-1.rb
2015-02-20 00:35:01 -05:00

21 lines
665 B
Ruby

points = (1..100).map do
# choose a random radius and angle
angle = rand * 2.0 * Math::PI
rad = rand * 5.0 + 10.0
# convert back from polar to cartesian coordinates
[rad * Math::cos(angle), rad * Math::sin(angle)].map(&:round)
end
(-15..15).each do |row|
puts (-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join
end
load 'raster_graphics.rb'
pixmap = Pixmap.new(321,321)
pixmap.draw_circle(Pixel.new(160,160),90,RGBColour::BLACK)
pixmap.draw_circle(Pixel.new(160,160),160,RGBColour::BLACK)
points.each {|(x,y)| pixmap[10*(x+16),10*(y+16)] = RGBColour::BLACK}
pngfile = __FILE__
pngfile[/\.rb/] = ".png"
pixmap.save_as_png(pngfile)