Helmut Giese
2024-10-16 13:15:15 UTC
Hello out there,
I always considered me smart enough to center a circle on a square
canvas - but I was proven wrong which the following program shows. If
you call it normally it uses TkPath, if it sees any argument it uses
Tk. The outcome seems to be the same in both cases - except for the
rugged outline in the Tk version. The added retangle shows it even
more clearly.
---
package require Tk
package require tkpath
foreach ch [winfo children "."] {destroy $ch}
set useTK 0
if {$argc} {
set useTK 1
}
if {$useTK} {
set c [canvas .c -background gray50 -width 200 -height 200]
} else {
set c [tkp::canvas .c -background gray50 -width 200 -height 200]
}
pack $c -padx 10 -pady 10
set width [$c cget -width]
set height [$c cget -height]
set x1 [expr {$width * 0.04}]
set y1 $x1
set x2 [expr {$width - $width * 0.04}]
set y2 $x2
if {$useTK} {
$c create oval $x1 $y1 $x2 $y2 -fill white -outline white
puts "width: $width, x1/y1 - x2/y2: $x1/$y1 - $x2/$y2"
} else {
set xc [expr {$x1 + ($x2 - $x1) / 2.0}]
set yc [expr {$y1 + ($y2 - $y1) / 2.0}]
set r [expr {($x2 - $x1) / 2.0}]
$c create circle $xc $yc -r $r -fill white -stroke white
puts "width: $width, xc/yc: $xc/$yc, r: $r"
}
$c create rectangle $x1 $y1 $x2 $y2 -outline red
---
On a canvas 200 px wide and high it draws a circle at 100/100 and
still it isn't centered. What did I do wrong?
Any enlightenment will be highly appreciated.
Helmut
I always considered me smart enough to center a circle on a square
canvas - but I was proven wrong which the following program shows. If
you call it normally it uses TkPath, if it sees any argument it uses
Tk. The outcome seems to be the same in both cases - except for the
rugged outline in the Tk version. The added retangle shows it even
more clearly.
---
package require Tk
package require tkpath
foreach ch [winfo children "."] {destroy $ch}
set useTK 0
if {$argc} {
set useTK 1
}
if {$useTK} {
set c [canvas .c -background gray50 -width 200 -height 200]
} else {
set c [tkp::canvas .c -background gray50 -width 200 -height 200]
}
pack $c -padx 10 -pady 10
set width [$c cget -width]
set height [$c cget -height]
set x1 [expr {$width * 0.04}]
set y1 $x1
set x2 [expr {$width - $width * 0.04}]
set y2 $x2
if {$useTK} {
$c create oval $x1 $y1 $x2 $y2 -fill white -outline white
puts "width: $width, x1/y1 - x2/y2: $x1/$y1 - $x2/$y2"
} else {
set xc [expr {$x1 + ($x2 - $x1) / 2.0}]
set yc [expr {$y1 + ($y2 - $y1) / 2.0}]
set r [expr {($x2 - $x1) / 2.0}]
$c create circle $xc $yc -r $r -fill white -stroke white
puts "width: $width, xc/yc: $xc/$yc, r: $r"
}
$c create rectangle $x1 $y1 $x2 $y2 -outline red
---
On a canvas 200 px wide and high it draws a circle at 100/100 and
still it isn't centered. What did I do wrong?
Any enlightenment will be highly appreciated.
Helmut