Constrained random points on a circle: Difference between revisions

m
m (→‎{{header|Ruby}}: add image)
Line 1,460:
 
=={{header|Ruby}}==
Create the image with [[Raster graphics operations/Ruby]]
{{incorrect|Ruby|<x,y> points do not have equal probability: points at low radius capture more probability than ones at higher radius; also, points near the "edges" of the dount have less probability}}
<lang Ruby>points = (1...100).map {
# choose a random radius and angle
Line 1,471:
(-15..15).each do |row|
puts((-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join)
end</lang>
 
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)</lang>
 
outputs:
[[File:constrainedrandompointsonacircle.png|thumg|right|Sample output from Ruby program]]
<pre> X X
X XX
X XXX
XX X X X X X
XXXXX
XX XX
X X X
X X
X X
X X XXX
X
XX X X
X X
X X
XXX X
XX X
XXX X X
 
XX X X X
XX X X
X X
X
XX X X
X X X
X X X
X
X X X
X X X
X X</pre>
 
=={{header|SystemVerilog}}==
Anonymous user