Paste: digits-viz

Author: mrjbq7
Mode: factor
Date: Fri, 19 Jul 2024 16:40:12
Plain Text |
USING: accessors arrays calendar colors kernel math
math.constants math.functions math.parser math.primes.erato.fast
math.vectors opengl processing.shapes sequences timers ui
ui.gadgets ui.gadgets.cartesian ;
IN: digits-viz

TUPLE: turtle pdown direction position lines ;

: <turtle> ( -- turtle )
    t pi 2 / { 0.0 0.0 } clone V{ } clone turtle boa ;

<PRIVATE
: line ( turtle a b -- turtle )
    pick pdown>> [
        2array '[ _ suffix! ] change-lines
    ] [ 2drop ] if ; inline
PRIVATE>

: fwd ( turtle steps -- turtle )
    over direction>> [ cos * ] [ sin * ] 2bi 2array
    over position>> [ v+ ] keep [ line ] keepd >>position ;

: trn ( turtle rads -- turtle )
    over direction>> + >>direction ; inline

: pdn ( turtle -- turtle ) t >>pdown ; inline
: pup ( turtle -- turtle ) f >>pdown ; inline

CONSTANT: base 10
CONSTANT: scale 0.1

TUPLE: digits-viz < cartesian turtle digits timer ;

M: digits-viz graft*
    [ timer>> start-timer ] [ call-next-method ] bi ;

M: digits-viz ungraft*
    [ timer>> stop-timer ] [ call-next-method ] bi ;

: init-digits ( -- digits )
    100 sieve V{ } [ base >base-digits append! ] reduce ;

: next-line ( digits-viz -- )
    dup digits>> [
        unclip [ >>digits ] dip
        over turtle>> swap
        base / pi 2 * * trn 10 fwd drop
    ] unless-empty relayout-1 ;

: update-range ( digits-viz -- digits-viz )
    dup pref-dim [ [ 0 swap - ] keep 2array scale 2 / v*n ] map
    first2 [ x-range ] dip y-range ;

: draw-digits ( digits-viz -- )
    COLOR: black gl-color
    turtle>> lines>> [ first2 draw-line* ] each ;

: <digits-viz> ( -- gadget )
    digits-viz new init-cartesian
        { 800 800 } >>pdim
        <turtle> >>turtle
        init-digits >>digits
        dup '[ _ next-line ] f 250 milliseconds <timer> >>timer
        dup '[ COLOR: white gl-clear _ update-range draw-digits ] >>action ;

MAIN-WINDOW: digits-viz-window
    { { title "Digits Visualization" } }
    <digits-viz> >>gadgets ;

New Annotation

Summary:
Author:
Mode:
Body: