Skip to content

Instantly share code, notes, and snippets.

@ne1uno
Last active May 24, 2022 18:25
Show Gist options
  • Save ne1uno/b9c13330f563462038a75e4710743b64 to your computer and use it in GitHub Desktop.
Save ne1uno/b9c13330f563462038a75e4710743b64 to your computer and use it in GitHub Desktop.
draw random polka dots in red-lang
draw some random polka dots, could be flashing light hazard.
https://en.wikipedia.org/wiki/Polka_dot
https://www.bing.com/images/search?q=pokadots
https://www.bing.com/images/search?q=polkadots
not quite there yet, needs a heuristic color selection.
random isn't always complementary.
dot distribution looks too much like a plain regular grid
ideas and maybe source from all the usual suspects
thanks to all their freely available rebol/red code
to run these scripts, get red-lang, under 2 mb all included cross platform
interpreter/compiler at https://www.red-lang.org/p/download.html
more code examples at https://rosettacode.org/wiki/Category:Red
Red [
Title: "polkadot demo"
Author: "ne1"
Date: "2022-5-24"
;icon: .ico
rights: "any [MIT BSD your-fav]"
version: 1.1.4.12 ;windows property version when compiled
Product: "do a red-lang polkadot demo" ; product name or version blank
;Product_version: "a red-lang mandala prv " ;nope
Needs: 'View ;
]
;may not work the same in much earlier red-lang versions, you're on your own
encap: pick ['interpreted 'compiled 'encapped] 1 ;123 to fix no-wait when compile/encap
; hard to detect reliably
;encapped really both
init-pos: 100x15 ;win10 left side taskbar
screen-size: 550x650
pane-size: screen-size * 95% ; x&y * n%
base-size: as-pair pane-size/x * 95% pane-size/y * 85%
; one last tweak
screen-size: screen-size - 40x40
; print [base-size screen-size]
random/seed now/time/precise ;red probably doesn't seed by default
; off is faster?
; default on prevents draw button working?
; works either way now...
;system/view/auto-sync?: off
bgcolor: beige
fgcolor: teal
ic-rate: 0:0:03 ;initial canvas redraw rate, 1 in seconds? or as time 0:0:05
pd-prev: []
polkadots: function [ "make some polkadots"
/prev "show previous instead"
][
EYscrn: (until [40 < r: random 60] r) + 80
alph: random to-integer (EYscrn / 3)
;overlap amount, need 0 bias occasional 25%
overlap: random/only [17 23 31 37] ;prime
;another problem, need to decide pattern first, then stick with it
;randomizing location of dots all the way down not great
;if bcolor same as background, no outline.
;sometimes color same as outline too, probably could check sanity
;
bcolor: random 255.255.255 - 1.1.1 ;needs work, -0/1.-0/1.-0/1
outline: either 2 < random 5 [random 255.255.255 - 1.1.1][bcolor]
;sparsity
density: 10 + random random 20
;mixed sized bias 1 or 2
mixed: random/only [1 2 1 2 3 5 1 2]
; multiple colors or just 1, needs easier way to follow dots
; FIXME also should be multi on one dot size or any dot any color too
multi: copy []
repeat n mixed [
append multi random 255.255.255 - 1.1.1
]
;could have radial or zone color changes, circular to a point or diagonal
;zigzag
;other shapes than circle
;make look like fabric, bunched in spots
;needs to create list of sizes if mixed, not random each time
dots: copy []
repeat n mixed [
append dots 4 + random 2 * to-integer (alph / 3)
]
offset: 10x20
sort dots ;by x default. set interleave to max dot
offadd: (last dots) * 3 ; stagger alternate rows
flip: random/only [#[true] #[true] #[false]] ; alternate rows more often
fliped: true
;won't entirely restore, good enough for now
either prev [
if 2 = length? pd-prev [
set [
EYscrn alph overlap multi bcolor outline density dots offadd flip
] :pd-prev/1
]
][
append/only pd-prev reduce [
EYscrn alph overlap multi bcolor outline density dots offadd flip
]
;not sure this is optimal
if 2 < length? pd-prev [remove pd-prev]
]
blk: copy reduce ['pen (multi/1) 'fill-pen (bcolor) ]
pd/color: outline
;step size of min dots size makes more sense. avoiding range/step
;need scriptable via params so can repeat pattern
repeat i EYscrn [
if 0 <> modulo i overlap [continue]
repeat j EYscrn [
either 0 <> modulo j overlap [continue][
if flip [
offset: either fliped [ ;indent alternate rows
offset + offadd
][
offset - offadd
]
fliped: not fliped
]
]
;can't be grid like, also can't often be sparse
; many popular patterns are complete & repeat
if 11 < random density [continue]
n: either n: index? find dots random/only dots [n][1]
repend blk compose/deep [ ;/only
;random time change color
;may want color zone, also same size same color
;not sure why sometime not filled
'pen multi/:n
'circle (offset + as-pair (i * 3) (j * 3))
dots/:n ;
]
]]
repend blk ['pen (black) 'text (as-pair 400 500) "polkadots" ]
] ;
#define_gview__ ;
gview: [
title "polkadot demo"
size screen-size
backdrop bgcolor
style ccanvas: base base-size ivory draw [] ;snow fail?
style bbutton: button font-color fgcolor 85
pad 5x0
bbutton " draw " [pd/draw: compose/deep [(polkadots)]]
bbutton "previous" [pd/draw: compose/deep [(polkadots/prev)]]
H5 " random polka dots "
bbutton "stop" [ ; flip start/stop
either face/text = "stop" [
face/text: "start"
pd/rate: none
;face/enabled?: false
][
face/text: "stop"
pd/rate: ic-rate
;face/enabled?: true
]
]
return
;pad 250x0 rate-sld: slider 175x35 50%
pd: ccanvas rate ic-rate
on-time [pd/draw: compose/deep [(polkadots)]]
]
#define_endview__ ;for geany bookmark sidebar c-lexer
; either encap = 'interpreted [
view/no-wait/flags/options/tight layout gview ['resize][offset: init-pos]
; ][
;set all false when encap so can have no-wait interpreted
; rate timer & buttons fail
;view/flags/options layout gview ['resize][offset: init-pos]
; ]
;start away from usually docked left side 100px taskbar
halt ;unfortunate grabs focus, /no-wait changed to exit recently?
comment {
todo
add clear/save
add screen transitions
ellipses, circles, other shapes or images patterns
add thickness of outline
add parameter sliders for density, multiples etc
with checkbox to randomize only those parameters
change previous to forward backward through last dozen
full screensaver mode
rate slider reactor, maybe start on change too
polkadot.ico
popular themes, white on blue or red, small dense, large sparse
make seamless image for background
not too interested in finishing all the todo at the moment, maybe later
system/view/auto-sync? and /no-wait interfere greatly with draw button?
was missing layout in view statement
rate anomaly solved w/time format and layout
probably should clear draw instead of overwriting? uses not much memory
maybe generate one 100x100 block and repeat as pattern
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment