From a798dca8ca19c2601413e85a316f73dd857a2c14 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Fri, 13 Oct 2023 19:37:14 -0400 Subject: fill,shapes,circle,connections: Breaking: Fix shapes Shapes all now work by wishes; connections has been ported to use these wishes. --- virtual-programs/connections.folk | 8 +-- virtual-programs/display/circle.folk | 4 +- virtual-programs/display/fill.folk | 11 ++-- virtual-programs/shapes.folk | 99 +++++++++++++----------------------- 4 files changed, 50 insertions(+), 72 deletions(-) (limited to 'virtual-programs') diff --git a/virtual-programs/connections.folk b/virtual-programs/connections.folk index c66fba1d..4e002910 100644 --- a/virtual-programs/connections.folk +++ b/virtual-programs/connections.folk @@ -17,8 +17,8 @@ When /anyone/ wishes /source/ is connected to /sink/ & \ set angle [expr {atan2(-[lindex $direction 1], [lindex $direction 0]) - 3.14159/2}] set color grey - Display::stroke [list $source $sink ] 2 $color - shape 3 $c 30 $angle $color true + Wish to draw a stroke with points [list $source $sink] width 2 color $color + Wish to draw a shape with sides 3 center $c radius 30 radians $angle color $color filled true } set speed 75 @@ -40,7 +40,7 @@ When /anyone/ wishes /source/ is dynamically connected to /sink/ & \ lassign [vec2 scale [vec2 add $source $sink] 0.5] cx cy - Display::stroke [list $source $sink ] 1 white + Wish to draw a stroke with points [list $source $sink] width 1 color white When the clock time is /t/ { @@ -50,7 +50,7 @@ When /anyone/ wishes /source/ is dynamically connected to /sink/ & \ for {set p $offset} {$p < $distance} {incr p $spacing} { set c [vec2 add $source [vec2 scale $direction $p]] set s [expr {min($maxsize, 0.20*min($p, $distance - $p))}] - shape 3 $c $s $angle white true + Wish to draw a shape with sides 3 center $c radius $s radians $angle color white filled true } } } diff --git a/virtual-programs/display/circle.folk b/virtual-programs/display/circle.folk index 2951f3a9..03e4def7 100644 --- a/virtual-programs/display/circle.folk +++ b/virtual-programs/display/circle.folk @@ -20,12 +20,12 @@ Wish the GPU compiles pipeline "circle" { When /someone/ wishes to draw a circle with /...options/ { set center [dict_getdef $options center ""] - if {center eq ""} { set center [list [dict get $options x] [dict get $options y]] } + if {$center eq ""} { set center [list [dict get $options x] [dict get $options y]] } set radius [dict get $options radius] set thickness [dict get $options thickness] set color [getColor [dict get $options color]] set filled [dict_getdef $options filled false] Wish the GPU draws pipeline "circle" with arguments \ - [list $center $radius $thickness $color $filled] + [list $center $radius $thickness $color [expr {$filled eq false ? 0 : 1}]] } diff --git a/virtual-programs/display/fill.folk b/virtual-programs/display/fill.folk index ae3626f4..f3057068 100644 --- a/virtual-programs/display/fill.folk +++ b/virtual-programs/display/fill.folk @@ -15,6 +15,7 @@ When /someone/ wishes to draw a triangle with /...options/ { } When /someone/ wishes to draw a quad with /...options/ { dict with options { + if {![info exists layer]} { set layer 0 } Wish the GPU draws pipeline "fillTriangle" with arguments \ [list $p1 $p2 $p3 [getColor $color]] layer $layer Wish the GPU draws pipeline "fillTriangle" with arguments \ @@ -29,9 +30,13 @@ When /someone/ wishes to draw a polygon with /...options/ { if {$num_points < 3} { error "At least 3 points are required to form a polygon." } elseif {$num_points == 3} { - eval fillTriangle $points $color + Wish to draw a triangle with \ + p0 [lindex $points 0] p1 [lindex $points 1] p2 [lindex $points 2] \ + color $color } elseif {$num_points == 4} { - eval fillQuad $points $color + Wish to draw a quad with \ + p0 [lindex $points 0] p1 [lindex $points 1] p2 [lindex $points 2] p3 [lindex $points 3] \ + color $color } else { # Get the first point in the list as the "base" point of the triangles set p0 [lindex $points 0] @@ -40,7 +45,7 @@ When /someone/ wishes to draw a polygon with /...options/ { set p1 [lindex $points $i] set p2 [lindex $points [expr {$i+1}]] Wish the GPU draws pipeline "fillTriangle" with arguments \ - [list $p0 $p1 $p2 $color] + [list $p0 $p1 $p2 [getColor $color]] } } } diff --git a/virtual-programs/shapes.folk b/virtual-programs/shapes.folk index 08106475..f01ff965 100644 --- a/virtual-programs/shapes.folk +++ b/virtual-programs/shapes.folk @@ -1,7 +1,14 @@ -# numPoints 2 => line -# numPoints 3 => triangle -# numPoints 4 => square -proc shape {numPoints c r {angle 0} {color white} {filled false} args} { +# sides 2 => line +# sides 3 => triangle +# sides 4 => square +When /someone/ wishes to draw a shape with /...options/ { + set numPoints [dict get $options sides] + set c [dict get $options center] + set r [dict get $options radius] + set radians [dict_getdef $options radians 0] + set color [dict_getdef $options color white] + set filled [dict_getdef $options filled false] + set p [list 0 0] set center $p set points [list $p] @@ -19,24 +26,28 @@ proc shape {numPoints c r {angle 0} {color white} {filled false} args} { set points [lmap v $points { set v [vec2 sub $v $center] - set v [vec2 rotate $v $angle] + set v [vec2 rotate $v $radians] set v [vec2 add $v $c] set v }] if {$filled} { - Display::fillPolygon $points $color + Wish to draw a polygon with points $points color $color } else { - Display::stroke $points 1 $color + Wish to draw a stroke with points $points width 1 color $color } } - -When /someone/ wishes /p/ draws a /color/ /shape/ offset /offsetVector/ & /p/ has region /r/ { +set shapes [dict create triangle 3 square 4 pentagon 5 hexagon 6 \ + septagon 7 octagon 8 nonagon 9] +When /someone/ wishes /p/ draws /a/ /shape/ with /...options/ & /p/ has region /r/ { lassign [region centroid $r] x y set width [region width $r] set height [region height $r] - lassign $offsetVector offsetX offsetY + lassign [dict_getdef $options offset {0 0}] offsetX offsetY + set radius [dict_getdef $options radius 50] + set color [dict_getdef $options color white] + set filled [dict_getdef $options filled false] if {$offsetX != 0} { set x [expr {$x + $offsetX}] @@ -48,74 +59,36 @@ When /someone/ wishes /p/ draws a /color/ /shape/ offset /offsetVector/ & /p/ ha set angle [region angle $r] set p [list $x $y] - # puts "drawing a $shape at $x $y with width $width and height $height" - set radius 50 - switch -nocase $shape { - triangle { - shape 3 $p $radius $angle $color - } - square { - shape 4 $p $radius $angle $color - } - pentagon { - shape 5 $p $radius $angle $color - } - hexagon { - shape 6 $p $radius $angle $color - } - septagon { - shape 7 $p $radius $angle $color - } - octagon { - shape 8 $p $radius $angle $color - } - nonagon { - shape 9 $p $radius $angle $color - } - circle { - Display::circle $x $y $radius 5 $color - } - default { - shape 2 $p $radius $angle $color - } + if {$shape eq "circle"} { + Wish to draw a circle with center $p radius $radius thickness 5 color $color filled $filled + } elseif {[dict exists $shapes $shape]} { + Wish to draw a shape with sides [dict get $shapes $shape] \ + center $p radius $radius radians $angle color $color filled $filled + } else { + Wish to draw a shape with sides 2 \ + center $p radius $radius radians $angle color $color filled $filled } } -set defaultColor white - -When /someone/ wishes /p/ draws a /color/ /shape/ & /p/ has region /r/ { - Wish $p draws a $color $shape offset {0 0} -} - -When /someone/ wishes /p/ draws a /shape/ & /p/ has region /r/ { - Wish $p draws a $defaultColor $shape -} - -When /someone/ wishes /p/ draws a /shape/ offset /offsetVector/ { - Wish $p draws a $defaultColor $shape offset $offsetVector -} - Claim $this has demo { Wish $this draws a circle - Wish $this draws a skyblue triangle - Wish $this draws a green triangle offset {280 0} - Wish $this draws a gold pentagon offset {200 0} - Wish $this draws a red octagon offset {250 80} + Wish $this draws a triangle with color skyblue + Wish $this draws a triangle with color green offset {280 0} + Wish $this draws a pentagon with color gold offset {200 0} + Wish $this draws an octagon with color red offset {250 80} When the clock time is /t/ { set offsetVector [list [sin $t] [cos $t]] set offsetVector [::vec2::scale $offsetVector 105] - Wish $this draws a palegoldenrod circle offset $offsetVector + Wish $this draws a circle with color palegoldenrod offset $offsetVector } # This toggles a square between filled and unfilled - # TODO cwervo: Support `Wish $this draws a circle with options /optionsDict/` to make this cleaner - # optionsDisct: {size number?: 50, filled boolean?: false, color string?: white, thickness string? white } When $this has region /r/ & the clock time is /t/ { lassign [region centroid $r] x y - set fill [expr {round(sin($t) * 2) % 2 == 0 ? true : false}] + set fill [expr {round(sin($t) * 2) % 2 == 0}] set y [- $y 150] - shape 4 [list [- $x 100] $y] 0 60 white $fill + Wish to draw a shape with sides 4 center [list [- $x 100] $y] radius 60 color white filled $fill } Wish $this is outlined white -- cgit v1.2.3