Constrained random points on a circle: Difference between revisions
Constrained random points on a circle (view source)
Revision as of 22:25, 13 February 2024
, 3 months ago→{{header|EasyLang}}
(→{{header|C sharp|C#}}: r.Next(403) should be r.Next(404) as the parameter is an exclusive upper-bound.) |
|||
(23 intermediate revisions by 17 users not shown) | |||
Line 14:
=={{header|11l}}==
{{trans|Julia}}
<
V canvas = [[0B] * (2*hi+1)] * (2*hi+1)
V i = 0
Line 27:
print(canvas[i].map(j -> I j {‘♦ ’} E ‘ ’).join(‘’))
print_circle(10, 15, 100)</
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC DrawCircle(BYTE rmin,rmax,max,x0,y0)
BYTE count,limit
INT x,y,r2,rmin2,rmax2
limit=rmax*2+1
rmin2=rmin*rmin
rmax2=rmax*rmax
count=0
WHILE count<max
DO
x=Rand(limit) y=Rand(limit)
x==-rmax y==-rmax
r2=x*x+y*y
IF r2>=rmin2 AND r2<=rmax2 THEN
Plot(x+x0,y+y0)
count==+1
FI
OD
RETURN
PROC Main()
BYTE CH=$02FC,COLOR0=$02C4
Graphics(5+16)
Color=1
COLOR0=$0C
DrawCircle(10,15,100,40,24)
DO UNTIL CH#$FF OD
CH=$FF
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Constrained_random_points_on_a_circle.png Screenshot from Atari 8-bit computer]
=={{header|Ada}}==
<
with Ada.Numerics.Discrete_Random;
procedure Circle is
Line 124 ⟶ 160:
Ada.Text_IO.Put_Line ("Chosen from precalculated:");
Print_Points (My_Circle_Precalculated);
end Circle;</
Output:
Line 199 ⟶ 235:
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to use of '''format'''[ted] ''transput''}}
<
printf(($g"[2J"$,REPR 27)); # ansi.sys #
Line 270 ⟶ 306:
gotoxy(2*radius+1, 2*radius+1);
newline(stand in)
)</
Sample output:
<pre>
Line 307 ⟶ 343:
=={{header|AutoHotkey}}==
Requires the GDI+ standard library by tic: http://www.autohotkey.com/forum/viewtopic.php?t=32238<br /> Works with individual pixels.
[[File:Ahk_fuzzycircle.png|thumb|right]]<
pToken := Gdip_Startup()
Line 324 ⟶ 360:
Gdip_DisposeImage(pBitmap)
Gdip_Shutdown(pToken)</
=={{header|BASIC}}==
==={{header|BASIC256}}===
{{trans|PureBasic}}
<syntaxhighlight lang="freebasic">graphsize 31, 31
for i = 1 to 100
do
x = int(rand * 30) - 15
y = int(rand * 30) - 15
r = sqr(x*x + y*y)
until 10 <= r and r <= 15
color rgb(255, 0, 0)
plot(x+15, y+15)
next i
end</syntaxhighlight>
==={{header|BBC BASIC}}===
<
ORIGIN 640, 512
FOR i% = 1 TO 1000
Line 336 ⟶ 387:
r = SQR(x%^2 + y%^2)
IF r >= 10 IF r <= 15 PLOT x%*2, y%*2
NEXT</
==={{header|FreeBASIC}}===
Pre calculate and plot 100 points to the console
<
#define Intrange(f,l) int(Rnd*(((l)+1)-(f))+(f))
Line 400 ⟶ 451:
print "done"
Sleep
</syntaxhighlight>
Console output:
<pre>
Line 427 ⟶ 478:
</pre>
==={{header|Yabasic}}===
{{trans|PureBasic}}
<syntaxhighlight lang="yabasic">open window 100, 100
clear window
for i = 1 to 100
repeat
x = ran(30)-15
y = ran(30)-15
r = sqr(x*x + y*y)
until 10 <= r and r <= 15
color 255, 0, 0
dot x+15, y+15
next i
end</syntaxhighlight>
=={{header|C}}==
<
#include <stdlib.h>
Line 463 ⟶ 530:
return 0;
}</
. . .
. . . .
Line 492 ⟶ 559:
. . . .
.
. </
=={{header|C sharp|C#}}==
<
using System.Diagnostics;
using System.Drawing;
Line 533 ⟶ 600:
}
}
}</
=={{header|C++}}==
[[File:constrained_rnd_pts_on_circle.png]]
<
#include <windows.h>
#include <list>
Line 603 ⟶ 670:
}
//--------------------------------------------------------------------------------------------------
</syntaxhighlight>
=={{header|Clojure}}==
<
(:import [java.awt Color Graphics Dimension]
[javax.swing JFrame JPanel]))
Line 624 ⟶ 691:
(.setDefaultCloseOperation JFrame/DISPOSE_ON_CLOSE)
.pack
.show))</
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
identification division.
program-id. circle.
Line 741 ⟶ 808:
end program circle.
</syntaxhighlight>
<pre>
Line 777 ⟶ 844:
=={{header|CoffeeScript}}==
<
NUM_POINTS = 100
MIN_R = 10
Line 805 ⟶ 872:
plot random_circle_points()
</syntaxhighlight>
The output may be a bit distorted, since even monospace fonts take more vertical space per character than horizontal space.
<syntaxhighlight lang="text">
> coffee foo.coffee
Line 839 ⟶ 906:
* * * * *
* * *
</syntaxhighlight>
=={{header|Common Lisp}}==
<
(loop with x with y with cnt = 0
with scr = (loop repeat 31 collect (loop repeat 31 collect " "))
Line 850 ⟶ 917:
(setf (elt (elt scr y) x) "@ ")
(incf cnt))
finally (mapc #'(lambda (row) (format t "~{~a~^~}~%" row)) scr)))</
=={{header|D}}==
This uses std.complex because D built-in complex numbers will be deprecated.
<
void main() {
Line 869 ⟶ 936:
writefln("%-(%s\n%)", table);
}</
{{out}}
<pre>
Line 901 ⟶ 968:
* * * *
** </pre>
=={{header|Delphi}}==
{{libheader| Winapi.Windows}}
{{libheader| System.SysUtils}}
{{libheader| Vcl.Graphics}}
{{libheader| Vcl.Controls}}
{{libheader| Vcl.Forms}}
{{libheader| Vcl.ExtCtrls}}
{{Trans|C#}}
<syntaxhighlight lang="delphi">
unit Main;
interface
uses
Winapi.Windows, System.SysUtils, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormPaint(Sender: TObject);
end;
var
Form1: TForm1;
Points: TArray<TPoint>;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ClientHeight := 600;
ClientWidth := 600;
end;
procedure TForm1.FormPaint(Sender: TObject);
var
i: integer;
p: TPoint;
index: integer;
begin
SetLength(Points, 404);
i := 0;
for var y := -15 to 15 do
for var x := -15 to 15 do
begin
if i >= 404 then
Break;
var c := Sqrt(x * x + y * y);
if (10 <= c) and (c <= 15) then
begin
inc(i);
points[i] := TPoint.Create(x, y);
end;
end;
var bm := TBitmap.create;
bm.SetSize(600, 600);
with bm.Canvas do
begin
Pen.Color := clRed;
Brush.Color := clRed;
Brush.Style := bsSolid;
Randomize;
for var count := 0 to 99 do
begin
repeat
index := Random(404);
p := points[index];
until (not p.IsZero);
points[index] := TPoint.Zero;
var cx := 290 + 19 * p.X;
var cy := 290 + 19 * p.Y;
Ellipse(cx - 5, cy - 5, cx + 5, cy + 5);
end;
end;
Canvas.Draw(0, 0, bm);
bm.Free;
end;
end.</syntaxhighlight>
=={{header|EasyLang}}==
[https://easylang.dev/show/#cod=bY0xDsIwFEP3nOKN0Igqv6hMzWGqtohIJYhPBMntUShsbLafZb8uYV2YYmJAnDNAxqNjnENMHIUDcqpx+R8rnsddE7tMQ8ZSaCj7ysIZcQweZYwzWqX0lcDn03rka6+350LvsNSdbpN1qvv1g07rsvVb05o3 Run it]
<syntaxhighlight>
while cnt < 100
x = randint 31 - 16
y = randint 31 - 16
r = sqrt (x * x + y * y)
if 10 <= r and r <= 15
cnt += 1
move 50 + x * 2 50 + y * 2
circle 1
.
.
</syntaxhighlight>
=={{header|EchoLisp}}==
Using the '''plot''' library. For a greater visual appeal, points are plotted as circles of random radius and color. The resulting image is at [http://www.echolalie.org/echolisp/images/circle.png].
<
(lib 'math)
(lib 'plot)
Line 921 ⟶ 1,086:
(plot-circle x y (random radius)))
(plot-edit))
</syntaxhighlight>
=={{header|Elixir}}==
===Algorithm 1: Generate random pairs===
{{works with|Elixir|1.1}}
<
defp generate_point(0, _, _, set), do: set
defp generate_point(n, f, condition, set) do
Line 948 ⟶ 1,113:
end
Random.circle</
'''Example output:'''
Line 986 ⟶ 1,151:
{{trans|Ruby}}
{{works with|Elixir|1.2}}
<
def circle do
range = -15..15
Line 999 ⟶ 1,164:
end
Constrain.circle</
{{out|Example}}
Line 1,040 ⟶ 1,205:
{{works with|Euphoria|4.0.3, 4.0.0 or later}}
This program generates the set of 404 possible points in the ring. It randomly chooses 100 pairs from the set. The 100 pairs are a subset of that set because duplicates are discarded.
<
sequence validpoints = {}
Line 1,104 ⟶ 1,269:
printf(1, "\nNumber of discarded coordinate pairs : %d", length(discardedpoints) )
printf(1, "\nNumber of randomly picked coordinate pairs : %d\n", length(rand100points) )
any_key()</
Output:
<pre>
Line 1,142 ⟶ 1,307:
Number of discarded coordinate pairs : 557
Number of randomly picked coordinate pairs : 100
Press Any Key to continue...</pre>Extra EuSDL code : <
for i = 1 to length(validpoints) do --simple each pixel output to screen surface
dummy=pixelColor(surface,validpoints[i][1]+18,validpoints[i][2]+18,#AA0202FF) --i is index number of each subsequence 'chunk'.
Line 1,161 ⟶ 1,326:
dummy=stringColor(surface,0,83,sprintf("Number of discarded coordinate pairs : %d", length(discardedpoints) ),#0202AAFF)
dummy=stringColor(surface,0,93,sprintf("Number of randomly picked coordinate pairs : %d", length(rand100points) ),#02AA02FF)</
[[File:Fuzzy_circle_Euphoria.png]] That particular program used a -16 to +16 square area, so more was discarded.
Line 1,167 ⟶ 1,332:
This version uses method 1 from the task description and just calculates 100 suitable points to plot.
The INTERACTIVE bit just permits this code in a .fsx file to be run with the interactive interpreter or compiled to an exe.
<
let main args =
let rnd = new System.Random()
Line 1,190 ⟶ 1,355:
[<EntryPoint>]
let main args = CirclePoints.main args
#endif</
An example of the output:
<pre>
Line 1,225 ⟶ 1,390:
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
math.statistics math.vectors random sequences strings ;
Line 1,231 ⟶ 1,396:
[ sum-of-squares 100 225 between? ] filter 100 sample
[ 15 v+n ] map 31 31 32 <matrix> [ matrix-set-nths ] keep
[ >string print ] each</
{{out}}
<pre>
Line 1,266 ⟶ 1,431:
=={{header|Falcon}}==
<
// Generate points in [min,max]^2 with constraint
function random_point (min, max, constraint)
Line 1,284 ⟶ 1,449:
>
end
</syntaxhighlight>
Example output:
<pre>
Line 1,315 ⟶ 1,480:
x x
x x x x
</pre>
=={{header|Forth}}==
{{works with|gforth|0.7.3}}
<br>
<syntaxhighlight lang=forth>#! /usr/bin/gforth
\ Constrained random points on a circle
require random.fs
\ initialize the random number generator with a time-dependent seed
utime drop seed !
\ generates a random integer in [-15,15]
: random-coord ( -- n )
31 random 15 -
;
\ generates a random point on the constrained circle
: random-point ( -- x y )
0 0
BEGIN
2drop
random-coord random-coord
2dup dup * swap dup * +
dup 100 >= swap 225 <= and
UNTIL
;
31 31 * CONSTANT SIZE
CREATE GRID SIZE cells allot GRID SIZE cells erase
\ get the address of point (x,y)
: point-addr ( x y -- addr )
15 + 31 * + 15 + cells GRID +
;
\ generate 100 random points and mark them in the grid
: gen-points ( -- )
100 0 ?DO
true random-point point-addr !
LOOP
;
\ prints the grid
: print-grid ( -- )
16 -15 ?DO
16 -15 ?DO
i j point-addr @
IF
42
ELSE
32
THEN
emit
LOOP
cr
LOOP
;
gen-points print-grid
bye</syntaxhighlight>
{{out}}
<pre>
*
*****
* ** **
* * * *
* **
***
* ** *
* *
* * **
*
*
* *
* * *
*
**
* *
* * * *
*
* **
* ** * **
* * *
* * ** *
** * * * * *
*
*** * *
** **
* *
</pre>
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<
implicit none
Line 1,394 ⟶ 1,654:
end do
end program</
Output
<pre>
Line 1,428 ⟶ 1,688:
=={{header|Frink}}==
<
count = 0
Line 1,443 ⟶ 1,703:
} while count < 100
g.show[]</
=={{header|gnuplot}}==
Line 1,449 ⟶ 1,709:
[[File:RingRandPntsGnu.png|right|thumb|Output RingRandPntsGnu.png]]
<
## Ring of random points 2/18/17 aev
reset
Line 1,477 ⟶ 1,737:
set output
unset print
</syntaxhighlight>
{{Output}}
<pre>
Line 1,485 ⟶ 1,745:
=={{header|Go}}==
'''Algorithm 1:'''
<
import (
Line 1,530 ⟶ 1,790:
}
fmt.Println(u, "unique points")
}</
'''Algorithm 2:'''
<
import (
Line 1,579 ⟶ 1,839:
}
fmt.Println(u, "unique points")
}</
{{out}}
<pre>
Line 1,619 ⟶ 1,879:
=={{header|Haskell}}==
Using [[Knuth shuffle#Haskell|Knuth Shuffle]]
<
import Control.Monad
import Control.Arrow
Line 1,633 ⟶ 1,893:
let canvas = foldl (\cs [x,y] -> replaceAt (31*(x+15)+y+15) "/ " cs ) blanco (take 100 pts)
-- show canvas
mapM_ (putStrLn.concat). takeWhile(not.null). unfoldr (Just . splitAt 31) $ canvas</
Output (added a trailing space per 'pixel'
<pre>*Main> task
Line 1,668 ⟶ 1,928:
=={{header|Hy}}==
<
(setv possible-points
(lfor
y (range -15 16)
[x y]))
(setv [xs ys] (zip #* (map (fn [_] (choice possible-points)) (range 100))))
; We can't use random.sample because that samples without replacement.
; #* is also known as unpack-iterable
(plt.plot xs ys "bo")
(plt.show)</
=={{header|Icon}} and {{header|Unicon}}==
Generate random points in the bounded by the outside edge. Reject any found out of the prescribed bounds and stop when the required numbers of points have been generated. [[File:Fuzzycircle-unicon.PNG|thumb|plot of 2000, 100, 120]]
<
procedure main(A) # points, inside r, outside r in pixels - default to task values
Line 1,708 ⟶ 1,972:
WDone()
end</
=={{header|J}}==
Line 1,714 ⟶ 1,978:
This version deals 100 distinct coordinates from the set of acceptable coordinates (much like dealing cards from a shuffled deck):
<
Example use (<code><nowiki>gen''</nowiki></code> generates the points, the rest of the example code deals with rendering them as a text array):
<
*
Line 1,747 ⟶ 2,011:
**
* * *
** * </
=={{header|Java}}==
<
public class FuzzyCircle {
Line 1,778 ⟶ 2,042:
}
}
}</
Output:
<pre>
Line 1,814 ⟶ 2,078:
=={{header|JavaScript}}==
JavaScript embedded in HTML, using canvas:
<
<body>
<canvas id="cv" width="320" height="320"></canvas>
Line 1,859 ⟶ 2,123:
}
</script></body></html></
=={{header|jq}}==
{{works with|jq}}
This solution uses a "generate and test" approach to find exactly 100 points within the specified annulus.
Since jq does not have a built-in PRNG, /dev/random is used instead. gnuplot and a bash or bash-like environment are also assumed.
<syntaxhighlight lang="bash">
#!/bin/bash
< /dev/random tr -cd '0-9' | fold -w 1 | jq -Mcnr '
# Output: a PRN in range(0;$n) where $n is .
def prn:
if . == 1 then 0
else . as $n
| (($n-1)|tostring|length) as $w
| [limit($w; inputs)] | join("") | tonumber
| if . < $n then . else ($n | prn) end
end;
def ss: map(.*.) | add;
# Input: [x,y]
# Emit . iff ss lies within the given bounds
def annulus($min; $max) : ss as $sum | select($min <= $sum and $sum <= $max);
limit(100;
repeat([((30 | prn) - 15), ((30 | prn) - 15)]
| select( annulus(100; 225)) ))
| "\(.[0]) \(.[1])"
' > rc-annulus.dat
</syntaxhighlight>
The plot can now be generated as a .png file using these gnuplot commands:
<syntaxhighlight lang="bash">
reset
set terminal pngcairo
set output 'rc-annulus.png'
set xrange [-20:20]
set yrange [-20:20]
plot "rc-annulus.dat" with points pt 1
</syntaxhighlight>
=={{header|Julia}}==
{{works with|Julia|0.6}}
This solution uses the "pick random x, y and cull" rather than the "calculate valid and choose randomly" approach.
<
canvas = falses(2hi + 1, 2hi + 1)
i = 0
Line 1,882 ⟶ 2,189:
end
printcircle(10, 15, 100)</
{{out}}
Line 1,920 ⟶ 2,227:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 1,936 ⟶ 2,243:
}
for (i in 0..30) println(points[i].joinToString(""))
}</
Sample output:
Line 1,970 ⟶ 2,277:
o o
</pre>
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
{def circ
{lambda {:cx :cy :r}
{div {@ style="position:absolute;
top: {- :cy :r}px; left: {- :cx :r}px;
width: {* 2 :r}px; height: {* 2 :r}px;
border: 1px solid #000; border-radius: :rpx;"}} }}
-> circ
{def fuzzy_circle
{lambda {:cx :cy :rmin :rmax :n}
{circ :cx :cy :rmax}
{circ :cx :cy :rmin}
{S.map {{lambda {:cx :cy :rmin :rmax :i}
{let { {:cx :cx} {:cy :cy}
{:rmin :rmin} {:rmax :rmax}
{:x {- {round {* {random} {* 2 :rmax}}} :rmax}}
{:y {- {round {* {random} {* 2 :rmax}}} :rmax}}
} {let { {:x {+ :cx :x }}
{:y {+ :cy :y }}
{:rr {+ {* :x :x} {* :y :y}}}
{:r2min {* :rmin :rmin}}
{:r2max {* :rmax :rmax}}
} {if {or {< :rr :r2min} {> :rr :r2max}}
then else {circ :x :y 2}}
}}} :cx :cy :rmin :rmax}
{S.serie 1 :n}} }}
-> fuzzy_circle
{fuzzy_circle 200 700 80 120 1000}
-> plots 1000 dots between the circles r=80 and r=120 centered at [200,700]
directly in the wiki page (out of any canvas or svg contexts) as it
can be seen in http://lambdaway.free.fr/lambdawalks/?view=fuzzy_circle
</syntaxhighlight>
=={{header|Liberty BASIC}}==
<
nomainwin
Line 2,001 ⟶ 2,345:
[quit]
close #w
end</
=={{header|Locomotive Basic}}==
<
20 FOR J=1 TO 100
30 X=INT(RND*30-15)
Line 2,013 ⟶ 2,357:
70 PLOT 320+10*X,200+10*Y:LOCATE 1,1:PRINT J
80 NEXT
90 CALL &BB06 ' wait for key press</
[[File:Points on a circle locomotive basic.png]]
=={{header|Lua}}==
Method 1, modified so that the 100 points must be unique..
<syntaxhighlight lang="lua">t, n = {}, 0
for y=1,31 do t[y]={} for x=1,31 do t[y][x]=" " end end
repeat
x, y = math.random(-15,15), math.random(-15,15)
rsq = x*x + y*y
if rsq>=100 and rsq<=225 and t[y+16][x+16]==" " then
t[y+16][x+16], n = "██", n+1
end
until n==100
for y=1,31 do print(table.concat(t[y])) end</syntaxhighlight>
{{out}}
<pre style="font-size:50%"> ██ ██
██ ████ ██ ██
████ ████ ██
██ ██ ████ ██ ██
██ ████ ██ ██
██ ██ ██ ██
██ ██ ██ ██ ██ ████
██ ████
██ ██
████ ██
██████
██
██ ██ ██
██ ██ ██
██ ██ ██
██
██
██ ██ ██
██
██████
██ ██ ██
████
██ ██
████ ██
████ ██████ ████ ██
██ ████ ██ ██ ██ ██ ██
████ ██ ████
██ ██
██ ████</pre>
=={{header|Maple}}==
<
i := 1:
while i < 100 do
Line 2,029 ⟶ 2,415:
end if:
end do:
plots:-pointplot(convert(a,list));</
=={{header|Mathematica}} / {{header|Wolfram Language}}==
This algorithm generates 500 pairs of random integers between +/- 15, picks out the ones that satisfy the inequality, and then takes the first 100 of those. It oversamples to reduce the chance of having less than 100 "candidates", which is not impossible, though extremely unlikely.
<
Show[{RegionPlot[10 <= Sqrt[x^2 + y^2] <= 15, {x, -16, 16}, {y, -16, 16}, Axes -> True], ListPlot[sample]}]</
=={{header|MATLAB}}==
Uses the Monte-Carlo method described above.
<
xCoordinates = [];
Line 2,070 ⟶ 2,456:
yCoordinates(numPoints+1:end) = [];
end</
Output:
<
>> plot(x,y,'.')</
[[File:Matlab-randomDisc-output.png]]
=={{header|Maxima}}==
<
local(goodp, random_int),
goodp(x, y):=block([r: sqrt(x^2+y^2)],
Line 2,093 ⟶ 2,479:
p: randomDisc(100)$
plot2d(['discrete, p], ['style, 'points]);</
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang
type Point = tuple[x, y: int]
Line 2,109 ⟶ 2,492:
for x in -15..15:
for y in -15..15:
if abs(complex(x.float, y.float)) in 10.0..15.0:
possiblePoints.add((x,y))
randomize()
for i in 0..100: world.inc possiblePoints.
for x in -15..15:
Line 2,119 ⟶ 2,502:
let key = (x, y)
if key in world and world[key] > 0:
stdout.write ' ' & $min(9, world[key])
else:
stdout.write
echo ""</
{{out}}
<pre>
1 1 1
1 1 1 1
1 2 1
1 1 1 1 1 1
1 1 1 1
1 1 1
1
1 1
1 1
1
1 1 1 1
1 2 2
1
1 1 2 1 1 2
1 1 1 1
1 </pre>
=={{header|OCaml}}==
<
let d = sqrt(x ** 2.0 +. y ** 2.0) in
10.0 <= d && d <= 15.0
Line 2,178 ⟶ 2,563:
g.(y).[x] <- 'o'
) points;
Array.iter print_endline g</
Line 2,210 ⟶ 2,595:
=={{header|PARI/GP}}==
<
my(v=vector(404),t=0,i=0,vx=vy=vector(100));
for(x=1,14,for(y=1,14,
Line 2,233 ⟶ 2,618:
);
plothraw(vx,vy)
};</
=={{header|Perl}}==
===Graphical output===
<
while (@points < 100) {
my ($x, $y) = (int(rand(31))-15, int(rand(31)) - 15);
Line 2,258 ⟶ 2,643:
print "@$_ pt\n" for @points;
print "%%EOF";</
Randomly generates points and reject ones not in the ring. Writes an EPS file.
===Plain-text output===
<
for $x (@range) {
Line 2,275 ⟶ 2,660:
push @matrix, ' ' x @range for 1..@range;
substr $matrix[15+$$_[1]], 15+$$_[0], 1, '*' for @sample;
print join(' ', split '', $_) . "\n" for @matrix;</
{{out}}
<pre> * * *
Line 2,308 ⟶ 2,693:
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">screen</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">),</span><span style="color: #000000;">31</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">y</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">r</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">31</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">rand</span><span style="color: #0000FF;">(</span><span style="color: #000000;">31</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)+</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">-</span><span style="color: #000000;">16</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">10</span> <span style="color: #008080;">and</span> <span style="color: #000000;">r</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">15</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">screen</span><span style="color: #0000FF;">[</span><span style="color: #000000;">x</span><span style="color: #0000FF;">][</span><span style="color: #000000;">y</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">'x'</span>
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">count</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">100</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">screen</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,356 ⟶ 2,744:
=={{header|PicoLisp}}==
<
(use (X Y)
(do 100
Line 2,368 ⟶ 2,756:
10 ) )
(set (nth Area (+ 16 X) (+ 16 Y)) "#") ) )
(mapc prinl Area) )</
Output:
<pre> #
Line 2,403 ⟶ 2,791:
=={{header|PL/I}}==
===version 1===
<syntaxhighlight lang="pl/i">
constrain: procedure options (main);
declare 1 point (100),
Line 2,431 ⟶ 2,819:
end;
end constrain;
</syntaxhighlight>
Output:
<pre>
Line 2,463 ⟶ 2,851:
</pre>
===version 2===
<
annulus: procedure options (main);
/* version 1 does not handle (0/15) etc. this does. */
Line 2,505 ⟶ 2,893:
Return(d);
End;
End annulus;</
'''output'''
<pre> *
Line 2,541 ⟶ 2,929:
=={{header|PowerShell}}==
{{works with|PowerShell|3}}
<
$MaxR2 = 15 * 15
Line 2,558 ⟶ 2,946:
}
ForEach ( $Y in -16..16 ) { ( -16..16 | ForEach { ( " ", "*" )[[int]$Points["$_,$Y"]] } ) -join '' }</
{{out}}
<pre> ***
Line 2,592 ⟶ 2,980:
=={{header|Prolog}}==
Works with SWI-Prolog
<
circle :-
Line 2,628 ⟶ 3,016:
send(D, display, C))),
send(D, open).
</syntaxhighlight>
[[FILE:Prolog-Circle.jpg ]]
=={{header|PureBasic}}==
<
StartDrawing(ImageOutput(0))
For i=1 To 100
Line 2,648 ⟶ 3,036:
OpenWindow(0,#PB_Ignore,#PB_Ignore,ImageWidth(0),ImageHeight(0),Title$,Flags)
ImageGadget(0,0,0,ImageWidth(0),ImageHeight(0),ImageID(0))
Repeat: Until WaitWindowEvent()=#PB_Event_CloseWindow</
[[File:PureBasic_Circle_plot.png|155px]]
=={{header|Python}}==
Note that the diagram shows the number of points at any given position (up to a maximum of 9 points).
<
>>> from random import choice
>>> world = defaultdict(int)
Line 2,696 ⟶ 3,084:
1 1 1
2 2 1
1 </
If the number of samples is increased to 1100:
<
>>> for x in range(-15,16):
Line 2,735 ⟶ 3,123:
2131181233 424
47414232164
4 </
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery"> [ 0 31 of ] is grid ( --> [ )
[ dup * ] is squared ( n --> n )
[ squared swap squared +
10 squared 15 squared 1+
within ] is inrange ( n n --> b )
[ 32 random 16 -
32 random 16 -
2dup inrange not while
2drop again ] is randxy ( --> n n )
[ 15 + swap 15 +
dip [ 2dup peek ]
bit | unrot poke ] is plot ( [ n n --> [ )
[ witheach
[ 31 times
[ dup
i^ bit & iff
[ $ "[]" ]
else
[ $ " " ]
echo$ ]
drop
cr ] ] is draw ( [ --> )
[ grid
swap times
[ randxy plot ]
draw ] is circle ( n --> )
100 circle</syntaxhighlight>
{{out}}
<pre>
[] [] []
[] [] []
[] [][] []
[] [] [] [][]
[] [] [][][][] [] []
[]
[] []
[] [][]
[]
[][] []
[] [] [] []
[] []
[]
[] []
[][] []
[][] []
[] [][][]
[] []
[] [] [] []
[]
[] []
[] []
[][][] []
[] [] [][] [] []
[] [][][] [] [] []
[] [] []
[] [][]
[] []
</pre>
Code check as per task discussion, as a dialogue in the Quackery shell.
<pre>/O> 10000 circle
...
[]
[][][][][][][][][][][]
[][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][] [][][][][][][][]
[][][][][][][] [][][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][][] [][][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][] [][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][] [][][][][][]
[][][][][][][] [][][][][][][]
[][][][][][][][] [][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][][][][][]
[][][][][][][][][][][][][][][]
[][][][][][][][][][][]
[]
Stack empty.
</pre>
=={{header|R}}==
<syntaxhighlight lang="r">
RMin <- 10
RMax <- 15
Line 2,757 ⟶ 3,256:
plot(X[Valid][1:NPts],Y[Valid][1:NPts], pch=19, cex=0.25, col="blue",
xlab="x",ylab="y",main="Fuzzy circle", xlim=c(-RMax,RMax), ylim=c(-RMax,RMax) )
</syntaxhighlight>
Example of solution
Line 2,764 ⟶ 3,263:
=={{header|Racket}}==
<
(require plot plot/utils)
Line 2,774 ⟶ 3,273:
#(15 15)))]
#:when (<= 10 (vmag xy) 15))
xy)))</
=={{header|Raku}}==
(formerly Perl 6)
{{works with|rakudo|2015.09}}
<syntaxhighlight lang="raku"
my @points = gather for @range X @range -> ($x, $y) {
Line 2,790 ⟶ 3,289:
for @range X @range -> ($x, $y) { %matrix{$y}{$x} = ' ' }
%matrix{.[1]}{.[0]} = '*' for @samples;
%matrix{$_}{@range}.join(' ').say for @range;</
{{out}}
<pre> *
Line 2,829 ⟶ 3,328:
At this point you would be justified in concluding that we are completely mad. <tt>:-)</tt>
<syntaxhighlight lang="raku"
-> [$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;
}
</syntaxhighlight>
{{out}}
<pre> * * *
Line 2,869 ⟶ 3,368:
===version 1===
This REXX version uses aspect adjustment for the plot of the (sparse) annulus.
<
parse arg pts LO HI . /*obtain optional args from the C.L. */
if pts=='' then pts= 100 /*Not specified? Then use the default.*/
Line 2,888 ⟶ 3,387:
end /*pts*/ /* [↑] maintain aspect ratio on X axis*/
/*stick a fork in it, we're all done. */
do y=-HI to HI; say @.y; end /*display the annulus to the terminal. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,926 ⟶ 3,425:
===version 2===
{{trans|REXX version 1}}
<
* show 100 random points of an annulus with radius 10 to 15
* 18.06.2014 Walter Pachl 'derived/simplified' from REXX version 1
Line 2,965 ⟶ 3,464:
Do y=-high To high
Say line.y
End</
'''output''' using default parameters
<pre>
Line 3,011 ⟶ 3,510:
===version 3===
<
* 19.06.2014 Walter Pachl alternate algorithm
* the idea: yl is a list of y coordinates which may have unused points
Line 3,088 ⟶ 3,587:
p.0=z
End
Return</
'''output''' using rexx fcaa 100 3 5 2
<pre>all points filled
Line 3,105 ⟶ 3,604:
=={{header|Ring}}==
<
load "guilib.ring"
Line 3,150 ⟶ 3,649:
}
label1 { setpicture(p1) show() }
</syntaxhighlight>
Output:
[[File:CalmoSoftDrawCircle.jpg]]
=={{header|RPL}}==
{{works with|HP|48G}}
« ERASE -15 15 DUP2 XRNG YRNG <span style="color:grey">@ set graphics boundaries</span>
DEG -19 SF 0 <span style="color:grey">@ set degrees mode, polar vector mode, count = 0</span>
'''DO''' RAND 5 * 10 + RAND 360 * →V2 <span style="color:grey">@ z = rand(10..15).exp(i.rand(360))</span>
C→R IP SWAP IP R→C <span style="color:grey">@ make z a complex with integer coordinates</span>
'''IF''' DUP ABS DUP 10 ≥ SWAP 15 ≤ AND <span style="color:grey">@ if 10 ≤ | z | ≤ 15</span>
'''THEN''' PIXON 1 + <span style="color:grey">@ then set pixel and increment count</span>
'''ELSE''' DROP '''END'''
'''UNTIL''' DUP 100 ≥ '''END'''
DROP { } PVIEW
-19 CF
» '<span style="color:blue">TASK</span>' STO
[[File:Constrained.png|alt=Screenshot of a HP-48G emulator, displaying constrained random points on a circle|HP-48G emulator screenshot]]
The circle is actually an ellipse because RPL display screens are not squared.
=={{header|Ruby}}==
Create the image with [[Raster graphics operations/Ruby]]
<
# choose a random radius and angle
angle = rand * 2.0 * Math::PI
Line 3,178 ⟶ 3,694:
pngfile = __FILE__
pngfile[/\.rb/] = ".png"
pixmap.save_as_png(pngfile)</
{{out}}
Line 3,213 ⟶ 3,729:
===algorithm 2:===
<
range = (-15..15).to_a
points = range.product(range).select {|i,j| r2.cover?(i*i + j*j)}
Line 3,220 ⟶ 3,736:
pt = Hash.new(" ")
points.sample(100).each{|ij| pt[ij] = " o"}
puts range.map{|i| range.map{|j| pt[[i,j]]}.join}</
{{out}}
Line 3,259 ⟶ 3,775:
=={{header|Run BASIC}}==
<
h = 320
dim canvas(w,h)
Line 3,283 ⟶ 3,799:
next x
render #g
#g "flush"</
=={{header|Rust}}==
<syntaxhighlight lang="rust">extern crate rand;
use rand::Rng;
Line 3,358 ⟶ 3,872:
precalculating_method(&mut rng);
}</
=={{header|Scala}}==
{{libheader|Scala}}<
import scala.math.hypot
import scala.swing.{MainFrame,Panel,SimpleSwingApplication}
Line 3,440 ⟶ 3,954:
contents = ui
}
}</
=={{header|Sidef}}==
{{trans|Perl}}
Generates an EPS file.
<
while (points.len < 100) {
var (x, y) = 2.of{
var r2 = (x**2 + y**2)
if ((r2 >= 100) && (r2 <= 225)) {
points.append([x, y])
}
}
print <<'HEAD'
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox 0 0 400 400
Line 3,466 ⟶ 3,980:
HEAD
points.each { |pt| say "#{pt.join(' ')} pt" }
print '%%EOF'
=={{header|Standard ML}}==
Works with PolyML. Plotting function from 'Draw a pixel' task. Uniform random generator: copy from [[Random_numbers#Standard_ML]]. x,y plotted scaled x 10 px
<
open Motif ;
Line 3,512 ⟶ 4,026:
in
List.take ( List.filter select uv , 1000 )
end ;</
call
> val scaledXY = map (fn (x,y)=>
Line 3,522 ⟶ 4,036:
{{trans|Rust}}
<
func generatePoint() -> (Int, Int) {
Line 3,580 ⟶ 4,094:
print("Precalculating method:")
precalculatingMethod()</
{{out}}
Line 3,651 ⟶ 4,165:
=={{header|SystemVerilog}}==
<
bit [39:0] bitmap [40];
Line 3,676 ⟶ 4,190:
end
endprogram</
Piping the output through sed to improve the contrast of the output:
Line 3,718 ⟶ 4,232:
=={{header|Tcl}}==
<
# Generate random point at specified distance from the centre
Line 3,751 ⟶ 4,265:
}
puts ""
}</
Example output:
<pre>
Line 3,786 ⟶ 4,300:
1
</pre>
=={{header|Wren}}==
{{libheader|DOME}}
<syntaxhighlight lang="wren">import "graphics" for Canvas, Color
import "dome" for Window
import "random" for Random
class Game {
static init() {
Window.title = "Constrained random points on a circle"
var width = 800
var height = 800
Window.resize(width, height)
Canvas.resize(width, height)
var rand = Random.new()
var count = 0
var max = 100 // set to 1000 to produce a much more pronounced annulus
while (true) {
var x = rand.int(-15, 16)
var y = rand.int(-15, 16)
var dist = (x*x + y*y).sqrt
if (10 <= dist && dist <= 15) {
// translate coordinates to fit in the window
Canvas.circlefill((x + 16) * 25, (y + 16) * 25, 2, Color.white)
count = count + 1
if (count == max) break
}
}
}
static update() {}
static draw(alpha) {}
}</syntaxhighlight>
=={{header|XPL0}}==
[[File:DonutXPL0.gif|right]]
<
int X, Y, C, R2;
[SetVid($13); \set 320x200x8 graphics mode
Line 3,801 ⟶ 4,349:
C:= ChIn(1); \wait for keystroke
SetVid(3); \restore normal text mode
]</
=={{header|zkl}}==
<
x:=(-15).random(16); y:=(-15).random(16);
if(not (100<=(x*x + y*y)<=225)) Void.Skip else T(x,y)
Line 3,813 ⟶ 4,361:
xy.walk(100).apply2(fcn([(x,y)],array){array[x+15 + N*(y+15)]="*"},array);
foreach n in ([0..30]){ array[n*N,30].concat().println(); }</
{{out}}
<pre>
Line 3,850 ⟶ 4,398:
=={{header|ZX Spectrum Basic}}==
{{trans|BBC_BASIC}}
<
20 LET x=RND*31-16
30 LET y=RND*31-16
40 LET r=SQR (x*x+y*y)
50 IF (r>=10) AND (r<=15) THEN PLOT 127+x*2,88+y*2
60 NEXT i</
|