Constrained random points on a circle: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Add Swift)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 325:
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)</lang>
 
=={{header|BASIC}}==
 
Line 492 ⟶ 493:
.
. </lang>
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.Diagnostics;
using System.Drawing;
 
namespace RosettaConstrainedRandomCircle
{
class Program
{
static void Main(string[] args)
{
var points = new Point[404];
int i = 0;
for (int y = -15; y <= 15; y++)
for (int x = -15; x <= 15 && i < 404; x++)
{
var c = Math.Sqrt(x * x + y * y);
if (10 <= c && c <= 15)
{
points[i++] = new Point(x, y);
}
}
 
var bm = new Bitmap(600, 600);
var g = Graphics.FromImage(bm);
var brush = new SolidBrush(Color.Magenta);
 
var r = new System.Random();
for (int count = 0; count < 100; count++)
{
var p = points[r.Next(403)];
g.FillEllipse(brush, new Rectangle(290 + 19 * p.X, 290 + 19 * p.Y, 10, 10));
}
const string filename = "Constrained Random Circle.png";
bm.Save(filename);
Process.Start(filename);
}
}
}</lang>
 
=={{header|C++}}==
Line 562 ⟶ 604:
//--------------------------------------------------------------------------------------------------
</lang>
 
=={{header|C sharp|C#}}==
 
<lang csharp>using System;
using System.Diagnostics;
using System.Drawing;
 
namespace RosettaConstrainedRandomCircle
{
class Program
{
static void Main(string[] args)
{
var points = new Point[404];
int i = 0;
for (int y = -15; y <= 15; y++)
for (int x = -15; x <= 15 && i < 404; x++)
{
var c = Math.Sqrt(x * x + y * y);
if (10 <= c && c <= 15)
{
points[i++] = new Point(x, y);
}
}
 
var bm = new Bitmap(600, 600);
var g = Graphics.FromImage(bm);
var brush = new SolidBrush(Color.Magenta);
 
var r = new System.Random();
for (int count = 0; count < 100; count++)
{
var p = points[r.Next(403)];
g.FillEllipse(brush, new Rectangle(290 + 19 * p.X, 290 + 19 * p.Y, 10, 10));
}
const string filename = "Constrained Random Circle.png";
bm.Save(filename);
Process.Start(filename);
}
}
}</lang>
 
=={{header|Clojure}}==
Line 900 ⟶ 901:
* * * *
** </pre>
 
 
=={{header|EchoLisp}}==
Line 1,426:
* * ** *
*</pre>
 
=={{header|gnuplot}}==
{{Works with|gnuplot|5.0 (patchlevel 3) and above}}
Line 1,729 ⟶ 1,730:
* * *
** * </lang>
 
=={{header|Java}}==
<lang java>import java.util.Random;
Line 2,188 ⟶ 2,190:
o
oo o
 
=={{header|PARI/GP}}==
Line 2,287 ⟶ 2,288:
* *
*</pre>
 
=={{header|Perl 6}}==
{{works with|rakudo|2015.09}}
<lang perl6>my @range = -15..16;
 
my @points = gather for @range X @range -> ($x, $y) {
take [$x,$y] if 10 <= sqrt($x*$x+$y*$y) <= 15
}
my @samples = @points.roll(100); # or .pick(100) to get distinct points
 
# format and print
my %matrix;
for @range X @range -> ($x, $y) { %matrix{$y}{$x} = ' ' }
%matrix{.[1]}{.[0]} = '*' for @samples;
%matrix{$_}{@range}.join(' ').say for @range;</lang>
{{out}}
<pre> *
* *
* * * * *
* * * *
* * * * *
* *
* * *
* * * *
*
* * * *
* * *
* * * *
*
* *
* * *
* * * *
* * *
*
* *
* *
* * *
* * *
* *
* * *
* * * * * * *
* * * * * *
* * * *
* *
* * *</pre>
Turning that program completely inside-out and reducing to a single statement with a single non-parameter variable, we get another version that also works.
 
This uses, among other things, a 0-based matrix rather than a hash, a <tt>given</tt> on the first line that allows us to print the final value of the matrix straight from its initial declaration, a <tt>for</tt> statement feeding a <tt>for</tt> statement modifier, a lambda that unpacks a single x-y argument into two variables, the functional form of pick rather than the method form, a quasi-list comprehension in the middle loop that filters each <tt>given</tt> with a <tt>when</tt>, precalculated squared limits so we don't have to take the square root, use of X- and X** to subtract and exponentiate both <tt>$x</tt> and <tt>$y</tt> in parallel.
 
After the <tt>given do</tt> has loaded up <tt>@matrix</tt> with our circle, the <tt>map</tt> on the first line substitutes a space for any undefined matrix element, and the extra space between elements is supplied by the stringification of the list value, performed by the prefix <tt>~</tt> operator, the unary equivalent of concatenation in Perl&nbsp;6.
 
At this point you would be justified in concluding that we are completely mad. <tt>:-)</tt>
 
<lang perl6>(say ~.map: { $_ // ' ' } for my @matrix) given do
-> [$x, $y] { @matrix[$x][$y] = '*' } for pick 100, do
for ^32 X ^32 -> ($x, $y) {
[$x,$y] when 100..225 given [+] ($x,$y X- 15) X** 2;
}
</lang>
{{out}}
<pre> * * *
* * *
* * * * * *
* * * * * *
* * * * * *
* * *
* * * * *
* * * *
* *
* *
*
* * *
 
* *
* * * * * *
* *
 
* * * *
* * *
* * * *
* *
* *
* * *
* * * * * *
* * * *
* * * * * *
* * * *
* * * * *
* * *</pre>
 
=={{header|Phix}}==
Line 2,832 ⟶ 2,744:
 
[[File:FuzzyCircle.jpg]]
 
=={{header|Racket}}==
<lang racket>#lang racket
Line 2,844 ⟶ 2,757:
#:when (<= 10 (vmag xy) 15))
xy)))</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015.09}}
<lang perl6>my @range = -15..16;
 
my @points = gather for @range X @range -> ($x, $y) {
take [$x,$y] if 10 <= sqrt($x*$x+$y*$y) <= 15
}
my @samples = @points.roll(100); # or .pick(100) to get distinct points
 
# format and print
my %matrix;
for @range X @range -> ($x, $y) { %matrix{$y}{$x} = ' ' }
%matrix{.[1]}{.[0]} = '*' for @samples;
%matrix{$_}{@range}.join(' ').say for @range;</lang>
{{out}}
<pre> *
* *
* * * * *
* * * *
* * * * *
* *
* * *
* * * *
*
* * * *
* * *
* * * *
*
* *
* * *
* * * *
* * *
*
* *
* *
* * *
* * *
* *
* * *
* * * * * * *
* * * * * *
* * * *
* *
* * *</pre>
Turning that program completely inside-out and reducing to a single statement with a single non-parameter variable, we get another version that also works.
 
This uses, among other things, a 0-based matrix rather than a hash, a <tt>given</tt> on the first line that allows us to print the final value of the matrix straight from its initial declaration, a <tt>for</tt> statement feeding a <tt>for</tt> statement modifier, a lambda that unpacks a single x-y argument into two variables, the functional form of pick rather than the method form, a quasi-list comprehension in the middle loop that filters each <tt>given</tt> with a <tt>when</tt>, precalculated squared limits so we don't have to take the square root, use of X- and X** to subtract and exponentiate both <tt>$x</tt> and <tt>$y</tt> in parallel.
 
After the <tt>given do</tt> has loaded up <tt>@matrix</tt> with our circle, the <tt>map</tt> on the first line substitutes a space for any undefined matrix element, and the extra space between elements is supplied by the stringification of the list value, performed by the prefix <tt>~</tt> operator, the unary equivalent of concatenation in Perl&nbsp;6.
 
At this point you would be justified in concluding that we are completely mad. <tt>:-)</tt>
 
<lang perl6>(say ~.map: { $_ // ' ' } for my @matrix) given do
-> [$x, $y] { @matrix[$x][$y] = '*' } for pick 100, do
for ^32 X ^32 -> ($x, $y) {
[$x,$y] when 100..225 given [+] ($x,$y X- 15) X** 2;
}
</lang>
{{out}}
<pre> * * *
* * *
* * * * * *
* * * * * *
* * * * * *
* * *
* * * * *
* * * *
* *
* *
*
* * *
 
* *
* * * * * *
* *
 
* * * *
* * *
* * * *
* *
* *
* * *
* * * * * *
* * * *
* * * * * *
* * * *
* * * * *
* * *</pre>
 
=={{header|REXX}}==
Line 3,445 ⟶ 3,448:
points.each { |pt| say "#{pt.join(' ')} pt" };
print '%%EOF';</lang>
 
=={{header|Swift}}==
 
{{trans|Rust}}
 
<lang swift>let nPoints = 100
 
func generatePoint() -> (Int, Int) {
while true {
let x = Int.random(in: -15...16)
let y = Int.random(in: -15...16)
let r2 = x * x + y * y
 
if r2 >= 100 && r2 <= 225 {
return (x, y)
}
}
}
 
func filteringMethod() {
var rows = [[String]](repeating: Array(repeating: " ", count: 62), count: 31)
 
for _ in 0..<nPoints {
let (x, y) = generatePoint()
 
rows[y + 15][x + 15 * 2] = "*"
}
 
for row in rows {
print(row.joined())
}
}
 
func precalculatingMethod() {
var possiblePoints = [(Int, Int)]()
 
for y in -15...15 {
for x in -15...15 {
let r2 = x * x + y * y
 
if r2 >= 100 && r2 <= 225 {
possiblePoints.append((x, y))
}
}
}
 
possiblePoints.shuffle()
 
var rows = [[String]](repeating: Array(repeating: " ", count: 62), count: 31)
 
for (x, y) in possiblePoints {
rows[y + 15][x + 15 * 2] = "*"
}
 
for row in rows {
print(row.joined())
}
}
 
print("Filtering method:")
filteringMethod()
 
print("Precalculating method:")
precalculatingMethod()</lang>
 
{{out}}
 
<pre>Filtering method:
*
** *
** ** *
* ** * *
** * *
* ** **
* *
*
* ** *
* *
* * * ***
*
* * *
**
* *
* *
*
* * *
*
* **
*
* * *
* *
*
** **
* **
*** * *
* ** * *
* *
* * *
*
Precalculating method:
*
***********
***************
*******************
*********************
***********************
******** ********
******* *******
****** ******
****** ******
****** ******
***** *****
***** *****
***** *****
***** *****
****** ******
***** *****
***** *****
***** *****
***** *****
****** ******
****** ******
****** ******
******* *******
******** ********
***********************
*********************
*******************
***************
***********
*
</pre>
 
=={{header|SystemVerilog}}==
Line 3,581 ⟶ 3,716:
1 1 1
1
</pre>
 
=={{header|Swift}}==
 
{{trans|Rust}}
 
<lang swift>let nPoints = 100
 
func generatePoint() -> (Int, Int) {
while true {
let x = Int.random(in: -15...16)
let y = Int.random(in: -15...16)
let r2 = x * x + y * y
 
if r2 >= 100 && r2 <= 225 {
return (x, y)
}
}
}
 
func filteringMethod() {
var rows = [[String]](repeating: Array(repeating: " ", count: 62), count: 31)
 
for _ in 0..<nPoints {
let (x, y) = generatePoint()
 
rows[y + 15][x + 15 * 2] = "*"
}
 
for row in rows {
print(row.joined())
}
}
 
func precalculatingMethod() {
var possiblePoints = [(Int, Int)]()
 
for y in -15...15 {
for x in -15...15 {
let r2 = x * x + y * y
 
if r2 >= 100 && r2 <= 225 {
possiblePoints.append((x, y))
}
}
}
 
possiblePoints.shuffle()
 
var rows = [[String]](repeating: Array(repeating: " ", count: 62), count: 31)
 
for (x, y) in possiblePoints {
rows[y + 15][x + 15 * 2] = "*"
}
 
for row in rows {
print(row.joined())
}
}
 
print("Filtering method:")
filteringMethod()
 
print("Precalculating method:")
precalculatingMethod()</lang>
 
{{out}}
 
<pre>Filtering method:
*
** *
** ** *
* ** * *
** * *
* ** **
* *
*
* ** *
* *
* * * ***
*
* * *
**
* *
* *
*
* * *
*
* **
*
* * *
* *
*
** **
* **
*** * *
* ** * *
* *
* * *
*
Precalculating method:
*
***********
***************
*******************
*********************
***********************
******** ********
******* *******
****** ******
****** ******
****** ******
***** *****
***** *****
***** *****
***** *****
****** ******
***** *****
***** *****
***** *****
***** *****
****** ******
****** ******
****** ******
******* *******
******** ********
***********************
*********************
*******************
***************
***********
*
</pre>
 
10,333

edits