2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,4 +1,5 @@
|
|||
Generate 100 <x,y> coordinate pairs such that x and y are integers sampled from the uniform distribution with the condition that <math>10 \leq \sqrt{ x^2 + y^2 } \leq 15 </math>. 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.
|
||||
;Task:
|
||||
Generate 100 <x,y> coordinate pairs such that x and y are integers sampled from the uniform distribution with the condition that <br><math>10 \leq \sqrt{ x^2 + y^2 } \leq 15 </math>. <br>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.
|
||||
|
||||
|
|
@ -6,3 +7,4 @@ There are several possible approaches to accomplish this. Here are two possible
|
|||
:<math>10 \leq \sqrt{ x^2 + y^2 } \leq 15 </math>.
|
||||
|
||||
2) Precalculate the set of all possible points (there are 404 of them) and select randomly from this set.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -1,17 +1,19 @@
|
|||
defmodule Random do
|
||||
defp generate_point(0, _, _, set), do: set
|
||||
defp generate_point(n, f, range, set) do
|
||||
defp generate_point(n, f, condition, set) do
|
||||
point = {x,y} = {f.(), f.()}
|
||||
if x*x + y*y in range and not Set.member?(set, point),
|
||||
do: generate_point(n-1, f, range, Set.put(set, point)),
|
||||
else: generate_point(n, f, range, set)
|
||||
if x*x + y*y in condition and not point in set,
|
||||
do: generate_point(n-1, f, condition, MapSet.put(set, point)),
|
||||
else: generate_point(n, f, condition, set)
|
||||
end
|
||||
|
||||
def circle do
|
||||
f = fn -> :rand.uniform(31) - 16 end
|
||||
points = generate_point(100, f, 10*10..15*15, HashSet.new)
|
||||
for x <- -15..15 do
|
||||
for y <- -15..15 do
|
||||
IO.write if Set.member?(points, {x,y}), do: "x", else: " "
|
||||
points = generate_point(100, f, 10*10..15*15, MapSet.new)
|
||||
range = -15..15
|
||||
for x <- range do
|
||||
for y <- range do
|
||||
IO.write if {x,y} in points, do: "x", else: " "
|
||||
end
|
||||
IO.puts ""
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
defmodule Constrain do
|
||||
defp precalculate(range, condition) do
|
||||
for x <- range, y <- range, x*x + y*y in condition, do: {x,y}
|
||||
end
|
||||
|
||||
def circle do
|
||||
range = -15..15
|
||||
all_points = precalculate(range, 10*10..15*15)
|
||||
IO.puts length(all_points)
|
||||
points = Enum.shuffle(all_points) |> Enum.take(100)
|
||||
Enum.each(-15..15, fn x ->
|
||||
IO.puts Enum.map(range, fn y -> if Enum.member?(points, {x,y}), do: "o ", else: " " end)
|
||||
r2 = 10*10..15*15
|
||||
all_points = for x <- range, y <- range, x*x+y*y in r2, do: {x,y}
|
||||
IO.puts "Precalculate: #{length(all_points)}"
|
||||
points = Enum.take_random(all_points, 100)
|
||||
Enum.each(range, fn x ->
|
||||
IO.puts Enum.map(range, fn y -> if {x,y} in points, do: "o ", else: " " end)
|
||||
end)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -7,28 +7,40 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type pt struct {
|
||||
x, y int
|
||||
}
|
||||
const (
|
||||
nPts = 100
|
||||
rMin = 10
|
||||
rMax = 15
|
||||
)
|
||||
|
||||
func main() {
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
// generate random points, accumulate 100 distinct points meeting condition
|
||||
m := make(map[int]pt) // key is buffer index
|
||||
for len(m) < 100 {
|
||||
p := pt{rand.Intn(31) - 15, rand.Intn(31) - 15}
|
||||
rs := p.x*p.x + p.y*p.y
|
||||
if 100 <= rs && rs <= 225 {
|
||||
m[(p.x+15)*2+(p.y+15)*31*2] = p
|
||||
rand.Seed(time.Now().Unix())
|
||||
span := rMax + 1 + rMax
|
||||
rows := make([][]byte, span)
|
||||
for r := range rows {
|
||||
rows[r] = bytes.Repeat([]byte{' '}, span*2)
|
||||
}
|
||||
u := 0 // count unique points
|
||||
min2 := rMin * rMin
|
||||
max2 := rMax * rMax
|
||||
for n := 0; n < nPts; {
|
||||
x := rand.Intn(span) - rMax
|
||||
y := rand.Intn(span) - rMax
|
||||
// x, y is the generated coordinate pair
|
||||
rs := x*x + y*y
|
||||
if rs < min2 || rs > max2 {
|
||||
continue
|
||||
}
|
||||
n++ // count pair as meeting condition
|
||||
r := y + rMax
|
||||
c := (x + rMax) * 2
|
||||
if rows[r][c] == ' ' {
|
||||
rows[r][c] = '*'
|
||||
u++
|
||||
}
|
||||
}
|
||||
// plot to buffer
|
||||
b := bytes.Repeat([]byte{' '}, 31*31*2)
|
||||
for i := range m {
|
||||
b[i] = '*'
|
||||
}
|
||||
// print buffer to screen
|
||||
for i := 0; i < 31; i++ {
|
||||
fmt.Println(string(b[i*31*2 : (i+1)*31*2]))
|
||||
for _, row := range rows {
|
||||
fmt.Println(string(row))
|
||||
}
|
||||
fmt.Println(u, "unique points")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,36 +4,45 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
type pt struct {
|
||||
x, y int
|
||||
}
|
||||
const (
|
||||
nPts = 100
|
||||
rMin = 10
|
||||
rMax = 15
|
||||
)
|
||||
|
||||
func main() {
|
||||
// generate possible points
|
||||
all := make([]pt, 0, 404)
|
||||
for x := -15; x <= 15; x++ {
|
||||
for y := -15; y <= 15; y++ {
|
||||
rs := x*x + y*y
|
||||
if 100 <= rs && rs <= 225 {
|
||||
all = append(all, pt{x, y})
|
||||
rand.Seed(time.Now().Unix())
|
||||
var poss []struct{ x, y int }
|
||||
min2 := rMin * rMin
|
||||
max2 := rMax * rMax
|
||||
for y := -rMax; y <= rMax; y++ {
|
||||
for x := -rMax; x <= rMax; x++ {
|
||||
if r2 := x*x + y*y; r2 >= min2 && r2 <= max2 {
|
||||
poss = append(poss, struct{ x, y int }{x, y})
|
||||
}
|
||||
}
|
||||
}
|
||||
if len(all) != 404 {
|
||||
panic(len(all))
|
||||
fmt.Println(len(poss), "possible points")
|
||||
span := rMax + 1 + rMax
|
||||
rows := make([][]byte, span)
|
||||
for r := range rows {
|
||||
rows[r] = bytes.Repeat([]byte{' '}, span*2)
|
||||
}
|
||||
// randomly order
|
||||
rp := rand.Perm(404)
|
||||
// plot 100 of them to a buffer
|
||||
b := bytes.Repeat([]byte{' '}, 31*31*2)
|
||||
for i := 0; i < 100; i++ {
|
||||
p := all[rp[i]]
|
||||
b[(p.x+15)*2+(p.y+15)*31*2] = '*'
|
||||
u := 0
|
||||
for n := 0; n < nPts; n++ {
|
||||
i := rand.Intn(len(poss))
|
||||
r := poss[i].y + rMax
|
||||
c := (poss[i].x + rMax) * 2
|
||||
if rows[r][c] == ' ' {
|
||||
rows[r][c] = '*'
|
||||
u++
|
||||
}
|
||||
}
|
||||
// print buffer to screen
|
||||
for i := 0; i < 31; i++ {
|
||||
fmt.Println(string(b[i*31*2 : (i+1)*31*2]))
|
||||
for _, row := range rows {
|
||||
fmt.Println(string(row))
|
||||
}
|
||||
fmt.Println(u, "unique points")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
$MinR2 = 10 * 10
|
||||
$MaxR2 = 15 * 15
|
||||
|
||||
$Points = @{}
|
||||
|
||||
While ( $Points.Count -lt 100 )
|
||||
{
|
||||
$X = Get-Random -Minimum -16 -Maximum 17
|
||||
$Y = Get-Random -Minimum -16 -Maximum 17
|
||||
$R2 = $X * $X + $Y * $Y
|
||||
|
||||
If ( $R2 -ge $MinR2 -and $R2 -le $MaxR2 -and "$X,$Y" -notin $Points.Keys )
|
||||
{
|
||||
$Points += @{ "$X,$Y" = 1 }
|
||||
}
|
||||
}
|
||||
|
||||
ForEach ( $Y in -16..16 ) { ( -16..16 | ForEach { ( " ", "*" )[[int]$Points["$_,$Y"]] } ) -join '' }
|
||||
|
|
@ -1,26 +1,26 @@
|
|||
/*REXX program generates 100 random points in an annulus: 10 ≤ √(x²≤y²) ≤ 15 */
|
||||
parse arg points low high . /*obtain optional args from the C.L. */
|
||||
/*REXX program generates 100 random points in an annulus: 10 ≤ √(x²≤y²) ≤ 15 */
|
||||
parse arg points low high . /*obtain optional args from the C.L. */
|
||||
if points=='' then points=100
|
||||
if low=='' then low=10; low2= low**2 /*define a shortcut for square.*/
|
||||
if high=='' then high=15; high2=high**2 /* " " " " " */
|
||||
if low=='' then low=10; low2= low**2 /*define a shortcut for squaring LOW. */
|
||||
if high=='' then high=15; high2=high**2 /* " " " " " HIGH.*/
|
||||
$=
|
||||
do x=-high; x2=x*x /*generate all possible annulus points.*/
|
||||
do x=-high; x2=x*x /*generate all possible annulus points.*/
|
||||
if x<0 & x2>high2 then iterate
|
||||
if x>0 & x2>high2 then leave
|
||||
do y=-high; s=x2+y*y
|
||||
if (y<0 & s>high2) | s<low2 then iterate
|
||||
if y>0 & s>high2 then leave
|
||||
$=$ x','y /*add a point─set to the $ list. */
|
||||
$=$ x','y /*add a point─set to the $ list. */
|
||||
end /*y*/
|
||||
end /*x*/
|
||||
|
||||
plotChar='Θ'; minY=high2; maxY=-minY; ap=words($); @.=
|
||||
|
||||
do j=1 for points /*define the x,y points [character O].*/
|
||||
parse value word($,random(1,ap)) with x ',' y /*pick a random point.*/
|
||||
@.y=overlay(plotChar, @.y, x+high+1) /*define: the point. */
|
||||
minY=min(minY,y); maxY=max(maxY,y) /*plot restricting. */
|
||||
do j=1 for points /*define the x,y points [character O].*/
|
||||
parse value word($,random(1,ap)) with x ',' y /*pick a random point in the annulus.*/
|
||||
@.y=overlay(plotChar, @.y, x+high+1) /*define: the data point. */
|
||||
minY=min(minY,y); maxY=max(maxY,y) /*perform the plot point restricting. */
|
||||
end /*j*/
|
||||
/* [↓] only show displayable section. */
|
||||
do y=minY to maxY; say @.y; end /*display the annulus to the terminal. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
/* [↓] only show displayable section. */
|
||||
do y=minY to maxY; say @.y; end /*display the annulus to the terminal. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -1,26 +1,26 @@
|
|||
/*REXX program generates 100 random points in an annulus: 10 ≤ √(x²≤y²) ≤ 15 */
|
||||
parse arg points low high . /*obtain optional args from the C.L. */
|
||||
/*REXX program generates 100 random points in an annulus: 10 ≤ √(x²≤y²) ≤ 15 */
|
||||
parse arg points low high . /*obtain optional args from the C.L. */
|
||||
if points=='' then points=100
|
||||
if low=='' then low=10; low2= low**2 /*define a square shortcut.*/
|
||||
if high=='' then high=15; high2=high**2 /* " " " " */
|
||||
if low=='' then low=10; low2= low**2 /*define a shortcut for squaring LOW. */
|
||||
if high=='' then high=15; high2=high**2 /* " " " " " HIGH.*/
|
||||
$=
|
||||
do x=-high; x2=x*x /*generate all possible annulus points.*/
|
||||
do x=-high; x2=x*x /*generate all possible annulus points.*/
|
||||
if x<0 & x2>high2 then iterate
|
||||
if x>0 & x2>high2 then leave
|
||||
do y=-high; s=x2+y*y
|
||||
if (y<0 & s>high2) | s<low2 then iterate
|
||||
if y>0 & s>high2 then leave
|
||||
$=$ x','y /*add a point─set to the $ list. */
|
||||
$=$ x','y /*add a point─set to the $ list. */
|
||||
end /*y*/
|
||||
end /*x*/
|
||||
|
||||
plotChar='Θ'; minY=high2; maxY=-minY; ap=words($); @.=
|
||||
|
||||
do j=1 for points /*define the x,y points [character Θ].*/
|
||||
parse value word($,random(1,ap)) with x ',' y /*pick a random point.*/
|
||||
@.y=overlay(plotChar, @.y, 2*x+2*high+1) /*define: the point. */
|
||||
minY=min(minY,y); maxY=max(maxY,y) /*plot restricting. */
|
||||
do j=1 for points /*define the x,y points [character O].*/
|
||||
parse value word($,random(1,ap)) with x ',' y /*pick a random point in the annulus.*/
|
||||
@.y=overlay(plotChar, @.y, 2*x+2*high+1) /*define: the data point. */
|
||||
minY=min(minY,y); maxY=max(maxY,y) /*perform the plot point restricting. */
|
||||
end /*j*/
|
||||
/* [↓] only show displayable section. */
|
||||
do y=minY to maxY; say @.y; end /*display the annulus to the terminal. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
/* [↓] only show displayable section. */
|
||||
do y=minY to maxY; say @.y; end /*display the annulus to the terminal. */
|
||||
/*stick a fork in it, we're all done. */
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
10 FOR i=1 TO 1000
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue