diff options
| author | Omar Rizwan <omar@omar.website> | 2023-09-25 21:49:26 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-25 21:49:26 +0000 |
| commit | b48914b37f5802aa9a02fc9b2b6a5a6777a6cfa8 (patch) | |
| tree | e8982ee3ba697a019780c3e28d684d5362ac80a3 /lib | |
| parent | Merge branch 'osnr/peering-with-prefix-match' (diff) | |
| parent | math/region move: Allow unitless scaling again, convert % to that (diff) | |
| download | folk-b48914b37f5802aa9a02fc9b2b6a5a6777a6cfa8.tar.gz folk-b48914b37f5802aa9a02fc9b2b6a5a6777a6cfa8.zip | |
Merge pull request #90 from FolkComputer/fix/region-math
Some small fixes to region math
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/math.tcl | 46 |
1 files changed, 24 insertions, 22 deletions
diff --git a/lib/math.tcl b/lib/math.tcl index 23efc57b..d3869358 100644 --- a/lib/math.tcl +++ b/lib/math.tcl @@ -222,6 +222,7 @@ namespace eval ::region { if {[llength $args] == 1} { set args [list width [lindex $args 0] height [lindex $args 0]] } + set sxp 1; set syp 1 foreach {dim value} $args { set theta [angle $r] set c [centroid $r] @@ -230,37 +231,36 @@ namespace eval ::region { error "region scale: Invalid scale value $value" } - set sxp 1; set syp 1 if {$dim eq "width"} { if {$unit eq "px"} { - set sxp [/ $value [width $r]] + set sxp [* $sxp [/ $value [width $r]]] } elseif {$unit eq "%"} { - set sxp [* $value 0.01] + set sxp [* $sxp $value 0.01] } elseif {$unit eq ""} { - set sxp $value + set sxp [* $sxp $value] } } elseif {$dim eq "height"} { if {$unit eq "px"} { - set syp [/ $value [height $r]] + set syp [* $syp [/ $value [height $r]]] } elseif {$unit eq "%"} { - set syp [* $value 0.01] + set syp [* $syp [* $value 0.01]] } elseif {$unit eq ""} { - set syp $value + set syp [* $syp $value] } } else { error "region scale: Invalid dimension $dim" } - - # TODO: Optimize - set r [mapVertices v $r { - set v [vec2 sub $v $c] - set v [vec2 rotate $v [* -1 $theta]] - set v [vec2 scale $v $sxp $syp] - set v [vec2 rotate $v $theta] - set v [vec2 add $v $c] - set v - }] } + + # TODO: Optimize + set r [mapVertices v $r { + set v [vec2 sub $v $c] + set v [vec2 rotate $v [* -1 $theta]] + set v [vec2 scale $v $sxp $syp] + set v [vec2 rotate $v $theta] + set v [vec2 add $v $c] + set v + }] set r } @@ -273,6 +273,11 @@ namespace eval ::region { if {![regexp {([0-9\.]+)(px|%)?} $distance -> distance unit]} { error "region move: Invalid distance $distance" } + if {$direction ne "left" && $direction ne "right" && + $direction ne "up" && $direction ne "down"} { + error "region move: Invalid direction $direction" + } + if {$unit eq "%"} { set distance [* $distance 0.01] set unit "" @@ -280,9 +285,9 @@ namespace eval ::region { if {$unit eq ""} { # Convert to pixels if {$direction eq "left" || $direction eq "right"} { - set distance [expr {[width $r] * $distance * 0.01}] + set distance [expr {[width $r] * $distance}] } elseif {$direction eq "up" || $direction eq "down"} { - set distance [expr {[height $r] * $distance * 0.01}] + set distance [expr {[height $r] * $distance}] } } set dxp [if {$direction eq "left"} {- $distance} \ @@ -291,9 +296,6 @@ namespace eval ::region { set dyp [if {$direction eq "up"} {- $distance} \ elseif {$direction eq "down"} {+ $distance} \ else {+ 0}] - if {$dxp == 0 && $dyp == 0} { - error "region move: Invalid direction $direction" - } set dv [vec2 rotate [list $dxp $dyp] $theta] set r [mapVertices v $r {vec2 add $v $dv}] } |
