June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -0,0 +1,113 @@
|
|||
identification division.
|
||||
program-id. circle.
|
||||
environment division.
|
||||
input-output section.
|
||||
file-control.
|
||||
select plot-file assign "circle.txt".
|
||||
data division.
|
||||
file section.
|
||||
fd plot-file report plot.
|
||||
working-storage section.
|
||||
1 binary.
|
||||
2 seed pic 9(18).
|
||||
2 x pic s9(4).
|
||||
2 y pic s9(4).
|
||||
2 i pic 9(4).
|
||||
2 dot-count pic 9(4) value 0.
|
||||
2 dot-count-save pic 9(4) value 0.
|
||||
2 temp-points.
|
||||
3 pic s9(4) occurs 2.
|
||||
2 xy-table.
|
||||
3 point-pair occurs 0 to 404 depending dot-count.
|
||||
4 x-point pic s9(4).
|
||||
4 y-point pic s9(4).
|
||||
1 plot-table value all "0".
|
||||
2 occurs 31.
|
||||
3 dot pic 9 occurs 31.
|
||||
1 cur-date-time.
|
||||
2 yyyymmdd pic 9(8).
|
||||
2 hh pic 9(2).
|
||||
2 mm pic 9(2).
|
||||
2 ss pic 9(2).
|
||||
1 plot-work.
|
||||
2 plot-item pic xb occurs 31.
|
||||
report section.
|
||||
rd plot.
|
||||
1 plot-line type de.
|
||||
2 line plus 1.
|
||||
3 column is 1 source is plot-work pic x(62).
|
||||
procedure division.
|
||||
begin.
|
||||
perform compute-seed
|
||||
perform find-all-valid-points
|
||||
perform shuffle-point-pairs
|
||||
perform select-100-dots
|
||||
perform print-dots
|
||||
stop run
|
||||
.
|
||||
|
||||
find-all-valid-points.
|
||||
perform varying x from -15 by 1 until x > +15
|
||||
perform varying y from -15 by 1 until y > +15
|
||||
if (function sqrt (x ** 2 + y ** 2))
|
||||
>= 10 and <= 15
|
||||
then
|
||||
move 1 to dot (x + 16 y + 16)
|
||||
add 1 to dot-count
|
||||
compute x-point (dot-count) = x + 16
|
||||
compute y-point (dot-count) = y + 16
|
||||
end-if
|
||||
end-perform
|
||||
end-perform
|
||||
display "Total points: " dot-count
|
||||
.
|
||||
|
||||
shuffle-point-pairs.
|
||||
move dot-count to dot-count-save
|
||||
compute i = function random (seed) * dot-count + 1
|
||||
perform varying dot-count from dot-count by -1
|
||||
until dot-count < 2
|
||||
move point-pair (i) to temp-points
|
||||
move point-pair (dot-count) to point-pair (i)
|
||||
move temp-points to point-pair (dot-count)
|
||||
compute i = function random * dot-count + 1
|
||||
end-perform
|
||||
move dot-count-save to dot-count
|
||||
.
|
||||
|
||||
select-100-dots.
|
||||
perform varying i from 1 by 1
|
||||
until i > 100
|
||||
compute x = x-point (i)
|
||||
compute y = y-point (i)
|
||||
move 2 to dot (x y)
|
||||
end-perform
|
||||
.
|
||||
|
||||
print-dots.
|
||||
open output plot-file
|
||||
initiate plot
|
||||
perform varying y from 1 by 1 until y > 31
|
||||
move spaces to plot-work
|
||||
perform varying x from 1 by 1 until x > 31
|
||||
if dot (x y) = 2
|
||||
move "o" to plot-item (x)
|
||||
end-if
|
||||
end-perform
|
||||
generate plot-line
|
||||
end-perform
|
||||
terminate plot
|
||||
close plot-file
|
||||
.
|
||||
|
||||
compute-seed.
|
||||
unstring function current-date into
|
||||
yyyymmdd hh mm ss
|
||||
compute seed =
|
||||
(function integer-of-date (yyyymmdd) * 86400)
|
||||
compute seed = seed
|
||||
+ (hh * 3600) + (mm * 60) + ss
|
||||
compute seed = function mod (seed 32768)
|
||||
.
|
||||
|
||||
end program circle.
|
||||
|
|
@ -1,18 +1,19 @@
|
|||
const LO = 10
|
||||
const HI = 15
|
||||
const GOAL = 100
|
||||
|
||||
canvas = falses(2HI+1, 2HI+1)
|
||||
i = 0
|
||||
|
||||
while i < GOAL
|
||||
x = rand(-HI:HI)
|
||||
y = rand(-HI:HI)
|
||||
LO^2-1 < x^2 + y^2 < HI^2+1 || continue
|
||||
i += 1
|
||||
canvas[x+HI+1, y+HI+1] = true
|
||||
function printcircle(lo::Integer, hi::Integer, ndots::Integer; pad::Integer = 2)
|
||||
canvas = falses(2hi + 1, 2hi + 1)
|
||||
i = 0
|
||||
while i < ndots
|
||||
x, y = rand(-hi:hi, 2)
|
||||
if lo ^ 2 - 1 < x ^ 2 + y ^ 2 < hi ^ 2 + 1
|
||||
canvas[x + hi + 1, y + hi + 1] = true
|
||||
i += 1
|
||||
end
|
||||
end
|
||||
# print
|
||||
for i in 1:(2hi + 1)
|
||||
row = map(j -> j ? "\u25cf " : " ", canvas[i, :])
|
||||
println(" " ^ pad, join(row))
|
||||
end
|
||||
return canvas
|
||||
end
|
||||
|
||||
for i in 1:(2HI+1)
|
||||
println(" ", join(map(j -> j ? "\u25cf " : " ", canvas[i,:])))
|
||||
end
|
||||
printcircle(10, 15, 100)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,73 @@
|
|||
#![feature(inclusive_range_syntax)]
|
||||
|
||||
extern crate rand;
|
||||
|
||||
use rand::Rng;
|
||||
|
||||
const POINTS_N: usize = 100;
|
||||
|
||||
fn generate_point<R: Rng>(rng: &mut R) -> (i32, i32) {
|
||||
loop {
|
||||
let x = rng.gen_range(-15, 16); // exclusive
|
||||
let y = rng.gen_range(-15, 16);
|
||||
|
||||
let r2 = x * x + y * y;
|
||||
if r2 >= 100 && r2 <= 225 {
|
||||
return (x, y);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn filtering_method<R: Rng>(rng: &mut R) {
|
||||
let mut rows = [[" "; 62]; 31];
|
||||
|
||||
// Generate points
|
||||
for _ in 0..POINTS_N {
|
||||
let (x, y) = generate_point(rng);
|
||||
rows[(y + 15) as usize][(x + 15) as usize * 2] = "*";
|
||||
}
|
||||
|
||||
// draw the points
|
||||
for row in &rows {
|
||||
println!("{}", row.concat());
|
||||
}
|
||||
}
|
||||
|
||||
fn precalculating_method<R: Rng>(rng: &mut R) {
|
||||
// Generate all possible points
|
||||
let mut possible_points = Vec::with_capacity(404);
|
||||
for y in -15..=15 {
|
||||
for x in -15..=15 {
|
||||
let r2 = x * x + y * y;
|
||||
if r2 >= 100 && r2 <= 225 {
|
||||
possible_points.push((x, y));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A truncated Fisher-Yates shuffle
|
||||
let len = possible_points.len();
|
||||
for i in (len - POINTS_N..len).rev() {
|
||||
let j = rng.gen_range(0, i + 1);
|
||||
possible_points.swap(i, j);
|
||||
}
|
||||
|
||||
// turn the selected points into "pixels"
|
||||
let mut rows = [[" "; 62]; 31];
|
||||
for &(x, y) in &possible_points[len - POINTS_N..] {
|
||||
rows[(y + 15) as usize][(x + 15) as usize * 2] = "*";
|
||||
}
|
||||
|
||||
// draw the "pixels"
|
||||
for row in &rows {
|
||||
println!("{}", row.concat());
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rng = rand::weak_rng();
|
||||
|
||||
filtering_method(&mut rng);
|
||||
|
||||
precalculating_method(&mut rng);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue