How to resolve the algorithm Constrained random points on a circle step by step in the Tcl 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 Tcl 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 Tcl programming language
Source code in the tcl programming language
package require Tcl 8.5
# Generate random point at specified distance from the centre
proc getPoint {range from to} {
set r2 [expr {$range / 2}]
set f2 [expr {$from ** 2}]
set t2 [expr {$to ** 2}]
while 1 {
set x [expr {int($range * rand())}]
set y [expr {int($range * rand())}]
set d2 [expr {($x-$r2)**2 + ($y-$r2)**2}]
if {$d2 >= $f2 && $d2 <= $t2} {
return [list $y $x]
}
}
}
# Make somewhere to store the counters
set ary [lrepeat 31 [lrepeat 31 0]]
# Generate 100 random points
for {set i 0} {$i < 100} {incr i} {
set location [getPoint 31 10 15]
# Increment the counter for the point
lset ary $location [expr {1 + [lindex $ary $location]}]
}
# Simple renderer
foreach line $ary {
foreach c $line {
puts -nonewline [expr {$c == 0 ? " " : $c > 9 ? "X" : $c}]
}
puts ""
}
You may also check:How to resolve the algorithm Shell one-liner step by step in the min programming language
You may also check:How to resolve the algorithm Associative array/Iteration step by step in the Maxima programming language
You may also check:How to resolve the algorithm Start from a main routine step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the BASIC programming language
You may also check:How to resolve the algorithm FASTA format step by step in the ALGOL W programming language