How to resolve the algorithm Constrained random points on a circle step by step in the Prolog programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Constrained random points on a circle step by step in the Prolog programming language
Table of Contents
Problem Statement
Generate 100 <x,y> coordinate pairs such that x and y are integers sampled from the uniform distribution with the condition that
10 ≤
x
2
y
2
≤ 15
{\displaystyle 10\leq {\sqrt {x^{2}+y^{2}}}\leq 15}
. Then display/plot them. The outcome should be a "fuzzy" circle. The actual number of points plotted may be less than 100, given that some pairs may be generated more than once. There are several possible approaches to accomplish this. Here are two possible algorithms.
- Generate random pairs of integers and filter out those that don't satisfy this condition:
- Precalculate the set of all possible points (there are 404 of them) and select randomly from this set.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Constrained random points on a circle step by step in the Prolog programming language
Source code in the prolog programming language
:- use_module(library(clpfd)).
circle :-
bagof([X,Y], init(X,Y), BL),
length(BL, N),
length(L, 100),
maplist(choose(BL, N), L),
draw_circle(L).
% point selection
choose(BL, N, V) :-
I is random(N),
nth0(I, BL, V).
% to find all couples of numbers verifying
% 100 <= x^2 + y^2 <= 225
init(X1, Y1) :-
X in -15..15,
Y in -15..15,
X*X + Y*Y #>= 100,
X*X + Y*Y #=< 225,
label([X,Y]),
X1 is 10 * X + 200,
Y1 is 10 * Y + 200.
draw_circle(L) :-
new(D, window('Circle')),
send(D, size,size(400,400)),
forall(member([X,Y], L),
( new(C, circle(4)),
send(C, fill_pattern, colour(@default, 0, 0, 0)),
send(C, center(point(X,Y))),
send(D, display, C))),
send(D, open).
You may also check:How to resolve the algorithm Pangram checker step by step in the Smalltalk programming language
You may also check:How to resolve the algorithm Record sound step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/Break step by step in the TorqueScript programming language
You may also check:How to resolve the algorithm Generic swap step by step in the D programming language
You may also check:How to resolve the algorithm Pragmatic directives step by step in the Raku programming language