How to center a circle?

Liste des GroupesRevenir à cl tcl 
Sujet : How to center a circle?
De : hgiese (at) *nospam* ratiosoft.com (Helmut Giese)
Groupes : comp.lang.tcl
Date : 16. Oct 2024, 14:15:15
Autres entêtes
Organisation : ratiosoft
Message-ID : <8vdvgjdo7cn84v1nl035oe222d2m82q99k@4ax.com>
User-Agent : Forte Free Agent 1.93/32.576 English (American)
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

Date Sujet#  Auteur
16 Oct 24 * How to center a circle?12Helmut Giese
16 Oct 24 +* Re: How to center a circle?7Ralf Fassel
16 Oct 24 i`* Re: How to center a circle?6Helmut Giese
16 Oct 24 i +* Re: How to center a circle?4Ralf Fassel
16 Oct 24 i i`* Re: How to center a circle?3Helmut Giese
17 Oct 24 i i +- Re: How to center a circle?1et99
17 Oct 24 i i `- Re: How to center a circle?1Ralf Fassel
16 Oct 24 i `- Re: How to center a circle?1Rich
17 Oct 24 `* Re: How to center a circle?4Helmut Giese
17 Oct 24  +- Re: How to center a circle?1saito
18 Oct 24  `* Re: How to center a circle?2Shaun Deacon
18 Oct 24   `- Re: How to center a circle?1Helmut Giese

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal