Constrained random points on a circle: Difference between revisions

Content deleted Content added
Nimrod -> Nim
→‎{{header|Ruby}}: add algorithm 2:
Line 2,451: Line 2,451:
=={{header|Ruby}}==
=={{header|Ruby}}==
Create the image with [[Raster graphics operations/Ruby]]
Create the image with [[Raster graphics operations/Ruby]]
<lang Ruby>points = (1...100).map {
<lang Ruby>points = (1..100).map do
# choose a random radius and angle
# choose a random radius and angle
angle = rand * 2.0 * Math::PI
angle = rand * 2.0 * Math::PI
rad = rand * 5.0 + 10.0
rad = rand * 5.0 + 10.0
# convert back from polar to cartesian coordinates
# convert back from polar to cartesian coordinates
[rad * Math::cos(angle), rad * Math::sin(angle)].map(&:round)
[rad * Math::cos(angle), rad * Math::sin(angle)].map(&:round)
end
}


(-15..15).each do |row|
(-15..15).each do |row|
puts((-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join)
puts (-15..15).map { |col| points.include?([row, col]) ? "X" : " " }.join
end
end


Line 2,473: Line 2,473:
pixmap.save_as_png(pngfile)</lang>
pixmap.save_as_png(pngfile)</lang>


{{out}}
outputs:
[[File:constrainedrandompointsonacircle.png|thumg|right|Sample output from Ruby program]]
[[File:constrainedrandompointsonacircle.png|thumg|right|Sample output from Ruby program]]
<pre> X X
<pre> X X
Line 2,504: Line 2,504:
X X X
X X X
X X</pre>
X X</pre>

===algorithm 2:===
<lang ruby>r2 = 10*10..15*15
range = (-15..15).to_a
points = range.product(range).each_with_object([]) do |(i,j), pt|
pt << [i,j] if r2.cover?(i*i + j*j)
end

puts "Precalculate: #{points.size}"
pt = Hash.new(" ")
points.sample(100).each{|i,j| pt[[i,j]] = " o"}
puts range.map{|i| range.map{|j| pt[[i,j]]}.join}</lang>

{{out}}
<pre>
Precalculate: 404
o o o
o o o o
o o o o o
o o o o o o
o o o o o
o o
o o
o o o
o o o
o o o o
o o
o
o
o o
o o o
o
o o o o o
o o o
o o o o
o
o o o
o o o
o o o o o o
o o o o o o o o o
o o o o o
o o o o o o
o o o o o
o o
o
</pre>


=={{header|Run BASIC}}==
=={{header|Run BASIC}}==