From c7f6843ea2dfe31dbd4e3edfd09e5ad230e2af24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Cuervo?= Date: Mon, 3 Apr 2023 13:17:40 -0400 Subject: Add circle to pi Display, center shapes correctly --- pi/pi.tcl | 4 ++++ play/Example_Domain.html | 46 ++++++++++++++++++++++++++++++++++++++++++++ play/wget.tcl | 35 +++++++++++++++++++++++++++++++++ virtual-programs/shapes.folk | 35 ++++++++++++++++++--------------- 4 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 play/Example_Domain.html create mode 100644 play/wget.tcl diff --git a/pi/pi.tcl b/pi/pi.tcl index d201c333..dee779e7 100644 --- a/pi/pi.tcl +++ b/pi/pi.tcl @@ -21,6 +21,10 @@ namespace eval Display { uplevel [list Wish display runs [list Display::stroke $points $width $color]] } + proc circle {x y radius thickness color} { + uplevel [list Wish display runs [list Display::circle $x $y $radius $thickness $color]] + } + proc text {fb x y fontSize text {radians 0}} { uplevel [list Wish display runs [list Display::text $fb $x $y $fontSize $text $radians]] } diff --git a/play/Example_Domain.html b/play/Example_Domain.html new file mode 100644 index 00000000..5a9b52fc --- /dev/null +++ b/play/Example_Domain.html @@ -0,0 +1,46 @@ + + + + Example Domain + + + + + + + + +
+

Example Domain

+

This domain is for use in illustrative examples in documents. You may use this + domain in literature without prior coordination or asking for permission.

+

More information...

+
+ + diff --git a/play/wget.tcl b/play/wget.tcl new file mode 100644 index 00000000..65e03076 --- /dev/null +++ b/play/wget.tcl @@ -0,0 +1,35 @@ +if {0} { + example syntax: Wish $this downloads "URL" + + When someone wishes for a page to be downloaded we check to see if there's + $title.html in the current directory. If there is, we don't download it. + + If there isn't, we download the page and save it as $title.html +} + + +# proc called findTitle that searches a string of HTML for the title using regexp and returns the text of the title +proc findTitle {webpage} { + set title [regexp -inline -all -line -lineanchor {(.*)} $webpage] + return [lindex $title 1] +} + +# proc called getWebpage that takes a URL, uses exec & wget to print out the content as a string +proc getWebpage {URL} { + set webpage [exec wget -q -O - $URL] + return $webpage +} + +# proc called download that takes a URL, uses getWebpage to download the content to a file +proc download {URL} { + set webpage [getWebpage $URL] + set title [string map {" " "_"} [findTitle $webpage]] + puts "title: $title" + puts "writing to file $title.html" + set file [open $title.html w] + puts $file $webpage + close $file +} + +# this works! downloads example.com to Example_Domain.html +download example.com \ No newline at end of file diff --git a/virtual-programs/shapes.folk b/virtual-programs/shapes.folk index 176f765b..f5bbaf41 100644 --- a/virtual-programs/shapes.folk +++ b/virtual-programs/shapes.folk @@ -40,11 +40,10 @@ proc regionToInnerRect {region} { lassign $vertices a b c d lassign $a aX aY lassign $c cX cY - set shrinkConstant 0.65 - set width [expr {($cX - $aX) * $shrinkConstant}] - set height [expr {($cY - $aY) * $shrinkConstant}] - set x [expr {$aX + $width * 0.25}] - set y [expr {$aY + $height * 0.25}] + set width [expr {($cX - $aX)}] + set height [expr {($cY - $aY)}] + set x [expr {$aX + $width * 0.5}] + set y [expr {$aY + $height * 0.5}] return [list $x $y $width $height] } @@ -53,7 +52,7 @@ proc regionToInnerRect {region} { # numPoints 2 => line # numPoints 3 => triangle # numPoints 4 => square -proc shape {numPoints x y r {color white} args} { +proc shape {numPoints x y r {color white} {filled false} args} { set start [list $x $y] set points [list $start] set i 0 @@ -64,13 +63,18 @@ proc shape {numPoints x y r {color white} args} { set y [expr {$y + $r * sin($angle)}] lappend points [list $x $y] } - polyline $points 5 $color + if {$filled} { + polyline $points 5 $color + } else { + polyline $points 10 $color + } } When /someone/ wishes /p/ draws a /color/ /shape/ offset /offsetVector/ & /p/ has region /r/ { lassign [regionToInnerRect $r] x y width height lassign $offsetVector offsetX offsetY - + + set filled false if {$offsetX != 0} { set x [expr {$x + $offsetX}] } @@ -81,21 +85,22 @@ When /someone/ wishes /p/ draws a /color/ /shape/ offset /offsetVector/ & /p/ ha puts "drawing $shape at $x $y" set adjustedWidth [expr {$width * 0.25}] - set x [expr { $x * 1.3}] - set y [expr { $y * 1.25}] + set x [expr { $x + $offsetX}] + set y [expr { $y + $offsetY}] + switch -nocase $shape { triangle { - shape 3 $x $y $adjustedWidth $color + shape 3 $x $y $adjustedWidth $color $filled } square { - shape 4 $x $y $adjustedWidth $color + shape 4 $x $y $adjustedWidth $color $filled } # TODO: cwervo - fix magic numbers here circle { - shape 10 [expr {$x * 0.98}] $y [expr {$adjustedWidth * 0.4}] $color + shape 10 [expr {$x * 0.98}] $y [expr {$adjustedWidth * 0.4}] $color $filled } default { - shape 5 $x $y $adjustedWidth $color + shape 5 $x $y $adjustedWidth $color $filled } } } @@ -112,4 +117,4 @@ When /someone/ wishes /p/ draws a /shape/ offset /offsetVector/ { Wish $p draws a white $shape offset $offsetVector } -# --- TODO: Wish table has ... ---- \ No newline at end of file +# --- TODO: Wish table has ... ---- -- cgit v1.2.3