June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,43 +1,42 @@
using Color, Images, FixedPointNumbers
using Images, FileIO
const W = 512
const W0 = W>>1
const H = 512
const H0 = H>>1
const N = iceil(1.0*W*H)
const SIDESTICK = false
function main(h::Integer, w::Integer, side::Bool=false)
W0 = w >> 1
H0 = h >> 1
@inline function motecolor(x::Integer, y::Integer)
h = clamp(180 * (atan2(y - H0, x - W0) / π + 1.0), 0.0, 360.0)
return HSV(h, 0.5, 0.5)
end
function motecolor(x::Int, y::Int)
h = clamp(180*(atan2(y-H0, x-W0)/pi + 1.0), 0.0, 360.0)
return HSV(h, 0.5, 0.5)
end
img = Image(zeros(RGB{Ufixed8}, H, W))
img["x", W0, "y", H0] = RGB(1, 1, 1)
isfree = trues(W, H)
isfree[W0, H0] = false
for i in 1:N
x = rand(1:W)
y = rand(1:H)
isfree[x, y] || continue
mc = motecolor(x, y)
for j in 1:10^3
xp = x + rand(-1:1)
yp = y + rand(-1:1)
iscontained = 0 < xp <= W && 0 < yp <= H
if iscontained && isfree[xp, yp]
x = xp
y = yp
continue
else
if SIDESTICK || (iscontained && !isfree[xp, yp])
isfree[x, y] = false
img["x", x, "y", y] = mc
img = zeros(RGB{N0f8}, h, w)
img[H0, W0] = RGB(1, 1, 1)
free = trues(h, w)
free[H0, W0] = false
for i in eachindex(img)
x = rand(1:h)
y = rand(1:w)
free[x, y] || continue
mc = motecolor(x, y)
for j in 1:1000
xp = x + rand(-1:1)
yp = y + rand(-1:1)
contained = checkbounds(Bool, img, xp, yp)
if contained && free[xp, yp]
x, y = xp, yp
continue
else
if side || (contained && !free[xp, yp])
free[x, y] = false
img[x, y] = mc
end
break
end
break
end
end
return img
end
imwrite(img, "brownian_tree.png")
imgnoside = main(256, 256)
imgwtside = main(256, 256, true)
save("data/browniantree_noside.jpg", imgnoside)
save("data/browniantree_wtside.jpg", imgwtside)

View file

@ -59,14 +59,9 @@ for ^particlenum -> $progress {
}
}
set($x,$y);
display if $progress %% 50;
if $spawnradius < mid && abs(($x|$y) - mid) > $spawnradius - 5 {
$spawnradius = $spawnradius + 1;
}
}
say "";
display;
say "";
say "time elapsed: ", (now - BEGIN { now }).Num.fmt("%.2f"), " seconds";
say "";

View file

@ -0,0 +1,102 @@
# Project : Brownian tree
# Date : 2018/02/16
# Author : Gal Zsolt (~ CalmoSoft ~)
# Email : <calmosoft@gmail.com>
load "stdlib.ring"
load "guilib.ring"
paint = null
new qapp
{
win1 = new qwidget() {
setwindowtitle("")
setgeometry(100,100,800,600)
label1 = new qlabel(win1) {
setgeometry(10,10,800,600)
settext("")
}
new qpushbutton(win1) {
setgeometry(150,500,100,30)
settext("draw")
setclickevent("draw()")
}
show()
}
exec()
}
func draw
p1 = new qpicture()
color = new qcolor() {
setrgb(0,0,255,255)
}
pen = new qpen() {
setcolor(color)
setwidth(1)
}
paint = new qpainter() {
begin(p1)
color = new qcolor()
color.setrgb(255,0,0,255)
pen = new qpen() {
setcolor(color)
setwidth(1)}
setpen(pen)
browniantree()
endpaint()
}
label1 { setpicture(p1) show() }
return
func browniantree()
numparticles = 3000
canvas = newlist(210,210)
canvas[randomf() * 100][randomf() * 200] = 1
for i = 1 to numparticles
x = floor((randomf() * 199)) + 1
y = floor((randomf() * 199)) + 1
if x = 1
x = 2
ok
if y = 1
y = 2
ok
while canvas[x+1][y+1]+canvas[x][y+1]+canvas[x+1][y]+canvas[x-1][y-1]+canvas[x-1][y]+canvas[x][y-1] = 0
x = x + floor((randomf() * 2)) + 1
y = y + floor((randomf() * 2)) + 1
if x = 1
x = 2
ok
if y = 1
y = 2
ok
if x < 1 or x > 200 or y < 1 or y > 200
x = floor((randomf() * 199)) + 1
y = floor((randomf() * 199)) + 1
if x = 1
x = 2
ok
if y = 1
y = 2
ok
ok
end
canvas[x][y] = 1
paint.drawpoint(x,y)
paint.drawpoint(x,y+1)
paint.drawpoint(x,y+2)
next
func randomf()
decimals(10)
str = "0."
for i = 1 to 10
nr = random(9)
str = str + string(nr)
next
return number(str)

View file

@ -0,0 +1,78 @@
const size = 100
const mid = size>>1
const particlenum = 1000
var map = []
var spawnradius = 5
func set(x, y) {
map[x][y] = 1
}
func get(x, y) {
map[x][y] \\ 0
}
set(mid, mid)
var blocks = [
" ",
"\N{UPPER HALF BLOCK}",
"\N{LOWER HALF BLOCK}",
"\N{FULL BLOCK}"
]
func block(a, b) {
blocks[2*b + a]
}
func display {
0..size `by` 2 -> map {|y|
0..size -> map {|x|
if ([x, y].all { .-mid < spawnradius }) {
block(get(x, y), get(x, y+1))
} else { " " }
}.join
}.join("\n").say
}
for progress in (^particlenum) {
var (x=0, y=0)
var reset = {
do {
(x, y) = (
(mid-spawnradius .. mid+spawnradius -> pick),
[mid-spawnradius, mid+spawnradius] -> pick
)
(x, y) = (y, x) if (1.rand < 0.5)
} while(get(x, y))
}
reset.run
while ([[-1, 0, 1]]*2 -> cartesian.any {|pair|
get(x+pair[0], y+pair[1])
} -> not) {
x = [x-1, x, x+1].pick
y = [y-1, y, y+1].pick
if (1.rand < 0.25) {
x = (x >= mid ? (x-1) : (x+1))
y = (y >= mid ? (y-1) : (y+1))
}
if ([x,y].any { .-mid > spawnradius }) {
reset.run
}
}
set(x, y)
display() if (progress %% 50)
if ((spawnradius < mid) && [x,y].any { .-mid > spawnradius-5 }) {
++spawnradius
}
}
display()