lambdaway
::
textanddraw
6
|
list
|
login
|
load
|
|
_h1 text & lines _p Writing the following words and s-expressions: {pre This is a first graph '{boxdraw 2 #f00 5,5 150,100 150,200 50,250 50,200} followed by this one '{boxdraw 4 #0f0 5,5 150,100 150,50 50,50 } and this one '{boxdraw 6 #00f 5,5 200,100 200,100 50,200} and the Hilbert curve '{boxdraw 1 #888 {turtle2 5 5 0 {left 8 4}}} } _p displays a text with boxes containing graphs This is a first graph {boxdraw 2 #f00 5,5 150,100 150,200 50,250 50,200} followed by this one {boxdraw 4 #0f0 5,5 150,100 150,50 50,50 } and this one {boxdraw 6 #00f 5,5 200,100 200,100 50,200} and the Hilbert curve {boxdraw 1 #0ff {turtle2 5 5 0 {left 8 4}}} _h2 code {pre '{def boxdraw {def boxdraw.in {lambda {:graph} {let { {:size {A.get 0 :graph}} {:color {A.get 1 :graph}} {:graph {A.rest {A.rest :graph}}} {:xy {fromPtoXY {A.rest {A.rest :graph}}}} } {let { {:size :size} {:color :color} {:w {max {fromAtoS {A.first :xy}}}} {:h {max {fromAtoS {A.last :xy}}}} {:graph :graph} } {{box :w :h} {draw :size :color {fromAtoS :graph}}} }}}} {lambda {:s} {boxdraw.in {A.new :s}}}} -> {def boxdraw {def boxdraw.in {lambda {:graph} {let { {:size {A.get 0 :graph}} {:color {A.get 1 :graph}} {:graph {A.rest {A.rest :graph}}} {:xy {fromPtoXY {A.rest {A.rest :graph}}}} } {let { {:size :size} {:color :color} {:w {max {fromAtoS {A.first :xy}}}} {:h {max {fromAtoS {A.last :xy}}}} {:graph :graph} } {{box :w :h} {draw :size :color {fromAtoS :graph}}} }}}} {lambda {:s} {boxdraw.in {A.new :s}}}} '{def box {lambda {:w :h} svg {@ width="{+ :w 5}" height="{+ :h 5}" style="border:1px solid #ccc;"}}} -> {def box {lambda {:w :h} svg {@ width="{+ :w 5}" height="{+ :h 5}" style="border:1px solid #ccc;"}}} '{def draw {lambda {:size :color :p} {polyline {@ points=":p" stroke-width=":size" stroke=":color" fill="transparent"}}}} -> {def draw {lambda {:size :color :p} {polyline {@ points=":p" stroke-width=":size" stroke=":color" fill="transparent"}}}} '{def split {lambda {:p} {A.new {S.replace ([\d]*),([\d]*) by $1 $2 in :p}}}} -> {def split {lambda {:p} {A.new {S.replace ([\d]*),([\d]*) by $1 $2 in :p}}}} '{def fromPtoXY {def fromPtoXY.r {lambda {:x :y :s} {if {A.empty? :s} then {A.new :x :y} else {let { {:x :x} {:y :y} {:s :s} {:foo {split {A.first :s}}} } {fromPtoXY.r {A.addlast! {A.first :foo} :x} {A.addlast! {A.last :foo} :y} {A.rest :s}} }}}} {lambda {:s} {fromPtoXY.r {A.new} {A.new} :s}}} -> {def fromPtoXY {def fromPtoXY.r {lambda {:x :y :s} {if {A.empty? :s} then {A.new :x :y} else {let { {:x :x} {:y :y} {:s :s} {:foo {split {A.first :s}}} } {fromPtoXY.r {A.addlast! {A.first :foo} :x} {A.addlast! {A.last :foo} :y} {A.rest :s}} }}}} {lambda {:s} {fromPtoXY.r {A.new} {A.new} :s}}} '{def fromAtoS {lambda {:a} {if {A.empty? :a} then else {A.first :a} {fromAtoS {A.rest :a}}}}} -> {def fromAtoS {lambda {:a} {if {A.empty? :a} then else {A.first :a} {fromAtoS {A.rest :a}}}}} } _p and the [[Hilbert|?view=hilbert]] curve {pre '{def left {lambda {:d :n} {if {< :n 1} then else T90 {right :d {- :n 1}} M:d T-90 {left :d {- :n 1}} M:d {left :d {- :n 1}} T-90 M:d {right :d {- :n 1}} T90}}} -> {def left {lambda {:d :n} {if {< :n 1} then else T90 {right :d {- :n 1}} M:d T-90 {left :d {- :n 1}} M:d {left :d {- :n 1}} T-90 M:d {right :d {- :n 1}} T90}}} '{def right {lambda {:d :n} {if {< :n 1} then else T-90 {left :d {- :n 1}} M:d T90 {right :d {- :n 1}} M:d {right :d {- :n 1}} T90 M:d {left :d {- :n 1}} T-90}}} -> {def right {lambda {:d :n} {if {< :n 1} then else T-90 {left :d {- :n 1}} M:d T90 {right :d {- :n 1}} M:d {right :d {- :n 1}} T90 M:d {left :d {- :n 1}} T-90}}} } _p More to see for instance in [[plot]], [[fractal_tree]], ... _p {i alain marty | 2022/06/07} _p [[HN|https://news.ycombinator.com/item?id=31637910]] {script // it's nothing but turtle returning "x,y" instead of "x y" // should be fixed to avoid repetition LAMBDATALK.DICT['turtle2'] = function () { var draw = function(str) { // {turtle x0 y0 a0 M100 T90 M50 T-45 ...} var args = str.split(' '); var x0 = parseFloat(args[0]), y0 = parseFloat(args[1]), a0 = parseFloat(args[2]), poly = []; poly.push( [x0, y0, a0] ); for (var i=3; i < args.length; i++) { var act = args[i].charAt(0), val = parseFloat(args[i].substring(1)); if (act === 'M') { var p = poly[poly.length-1], a = p[2] * Math.PI / 180.0, x = p[0] + val * Math.sin(a), y = p[1] + val * Math.cos(a); poly.push( [x,y,p[2]] ) } else { var p = poly.pop(); poly.push( [p[0],p[1],p[2]+val] ) } } for (var pol = '', i=0; i < poly.length; i++) pol += Math.round(poly[i][0]) + ',' + Math.round(poly[i][1]) + ' '; return pol }; return draw( LAMBDATALK.supertrim(arguments[0]) ); }; }
lambdaway v.20211111