builtin-programs/shapes/region.folk
# Creates an id "${p}:${index}" and assigns region.
# Extra regions can be used to create sensitive areas other pages can collect.
When /someone/ wishes /p/ adds region with /...options/ & /p/ has region /r/ {
lassign [region centroid $r] cx cy
set angle [region angle $r]
set defaults {
index 0 \
height 55 \
width 55 \
highlight false \
color red \
}
set index [dict get $options index]
set height [dict get $options height]
set width [dict get $options width]
set highlight [dict get $options highlight]
set color [dict get $options color]
set offset [dict_getdef $options offset {0 0}]
set offset [::process_offset $offset $r]
set center [vec2 add [list $cx $cy] [vec2 rotate $offset $angle]]
# compute points offset from $p
set hw [expr {$width / 2.0}]
set hh [expr {$height / 2.0}]
# compute points in table coordinates
set tablePoints [lmap v [list \
[list [expr {-$hw}] [expr {-$hh}]] \
[list [expr {$hw}] [expr {-$hh}]] \
[list [expr {$hw}] [expr {$hh}]] \
[list [expr {-$hw}] [expr {$hh}]] \
[list [expr {-$hw}] [expr {-$hh}]] \
] {
vec2 add $center [vec2 rotate $v $angle]
}]
set edges [list]
for {set i 0} {$i < [llength $tablePoints]} {incr i} {
if {$i > 0} { lappend edges [list [expr {$i - 1}] $i] }
}
lappend edges [list [expr {[llength $tablePoints] - 1}] [lindex $tablePoints 0]]
# Create new region in table points
set indexedRegion [region create $tablePoints $edges $angle]
Claim $p has indexedRegion with index $index region $indexedRegion
Claim "${p}:${index}" has region $indexedRegion
# debug: display dashed line around the points
if {$highlight} {
Wish region $indexedRegion has highlight $highlight with color $color
}
}
When /someone/ wishes region /r/ has highlight /highlighted/ with /...options/ {
set color [dict_getdef $options color white]
set thickness [dict_getdef $options thickness 2]
set layer [dict_getdef $options layer 0]
set dashed [dict_getdef $options dashed false]
set dashlength [dict_getdef $options dashlength 20]
set dashoffset [dict_getdef $options dashoffset 0]
if {$highlighted} {
set verts [region vertices $r]
set edges [region edges $r]
lappend verts [lindex $verts 0]
Wish to draw a dashed stroke with points $verts color $color width $thickness dashlength $dashlength dashoffset $dashoffset layer $layer
}
}
Claim $this has demo {
# How to use
# When builtin-programs/shapes/region.folk has demo /code/ & \
# $this has region /r/ {
# Claim $this has program code $code
# set angle [region angle $r]
# set pos [region bottom $r]
# Wish to draw text with position $pos scale 0.6 text $code radians $angle anchor topright
# }
When $this has region /r/ {
Wish region $r has highlight true with color yellow thickness 1 dashed true
Wish $this adds region with index 0 width 50 height 50 offset [list -250 0] highlight true color yellow
Wish $this draws text "Region 0" with offset [list -250 -50] scale 0.6 color yellow
Wish $this adds region with index 1 width 50 height 50 offset [list 250 0] highlight true color yellow
Wish $this draws text "Region 1" with offset [list 250 -50] scale 0.6 color yellow
}
}