• How to center a circle?

    From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Wed Oct 16 15:15:15 2024
    From Newsgroup: comp.lang.tcl

    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
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Oct 16 15:29:17 2024
    From Newsgroup: comp.lang.tcl

    * Helmut Giese <hgiese@ratiosoft.com>
    --<snip-snip>--
    | 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.

    Looks pretty centered to me.. although adding +1 to the x1 and x2 coords
    makes it even 'more' centered :-) (TK version, since I don't have tkpath at hand).

    set x1 [expr {$width * 0.04 +1}]
    ...
    set x2 [expr {$width - $width * 0.04 +1}]

    Could be related to the origin of the coordinate system, and how items
    are plotted given their coords...

    R'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Wed Oct 16 17:00:20 2024
    From Newsgroup: comp.lang.tcl

    Hello Ralf,
    Looks pretty centered to me.. although adding +1 to the x1 and x2 coords >makes it even 'more' centered :-) (TK version, since I don't have tkpath at >hand).
    looking at my monitor this sounds incredible - it is so obvious. Ok, I
    made a mistake: I didn't tell platform and version: I am on Windows
    10 and run Tcl 8.6.10.
    If I knew a site where I could upload a screen shot everybody would
    see what I am talking about.
    Thanks
    Helmut
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Wed Oct 16 18:36:36 2024
    From Newsgroup: comp.lang.tcl

    * Helmut Giese <hgiese@ratiosoft.com>
    | >Looks pretty centered to me.. although adding +1 to the x1 and x2 coords
    | >makes it even 'more' centered :-) (TK version, since I don't have tkpath at | >hand).
    | looking at my monitor this sounds incredible - it is so obvious. Ok, I
    | made a mistake: I didn't tell platform and version: I am on Windows
    | 10 and run Tcl 8.6.10.

    Same results here on Linux and Windows 10 with tk 8.6.15...

    | If I knew a site where I could upload a screen shot everybody would
    | see what I am talking about.

    Select any of
    https://www.google.com/search?q=public+upload+site
    ?

    R'
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Wed Oct 16 17:08:25 2024
    From Newsgroup: comp.lang.tcl

    Helmut Giese <hgiese@ratiosoft.com> wrote:
    If I knew a site where I could upload a screen shot everybody would
    see what I am talking about.

    Try:

    https://0x0.st
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Wed Oct 16 19:57:59 2024
    From Newsgroup: comp.lang.tcl

    Hello Ralf,
    Select any of
    https://www.google.com/search?q=public+upload+site
    ?
    this was very helpful. The screen shots are now on https://imgbb.com/
    with the URLs:
    TkPath: https://ibb.co/gg6XsRm
    Tk: https://ibb.co/LkGg9mv
    Apart from the rugged edge in Tk they look pretty identical to me.
    And to me the left and top edges are significant smaller than their
    right and bottom counter parts - about less than half I would say.

    Am I crazy? Does my monitor play tricks with me? I know that I don't
    have perfect eye sight - that's why I am wearing glasses since the age
    of ten and so far it has helped me lead a normal life.

    A very confused
    Helmut
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From et99@et99@rocketship1.me to comp.lang.tcl on Wed Oct 16 18:58:18 2024
    From Newsgroup: comp.lang.tcl

    On 10/16/2024 10:57 AM, Helmut Giese wrote:

    Am I crazy? Does my monitor play tricks with me? I know that I don't
    have perfect eye sight - that's why I am wearing glasses since the age
    of ten and so far it has helped me lead a normal life.

    A very confused
    Helmut

    I'm on windows 10 and they look completely symmetrical to me. A circle enclosed in a box. I see the same from an android phone.

    Do you perchance have more than a few diopters of cylinder (astigmatism) in your glasses? That's the only thing other than it being your computer/monitor that comes to mind :)


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Ralf Fassel@ralfixx@gmx.de to comp.lang.tcl on Thu Oct 17 10:55:00 2024
    From Newsgroup: comp.lang.tcl

    * Helmut Giese <hgiese@ratiosoft.com>
    | The screen shots are now on https://imgbb.com/
    | with the URLs:
    | TkPath: https://ibb.co/gg6XsRm
    | Tk: https://ibb.co/LkGg9mv
    | Apart from the rugged edge in Tk they look pretty identical to me.
    | And to me the left and top edges are significant smaller than their
    | right and bottom counter parts - about less than half I would say.

    Ok, this is the same what I see. I was not sure whether this small
    difference already counts as 'big' :-p

    As I said, they get 'more centered' if you adjust the coordinates by +1:

    set x1 [expr {$width * 0.04 +1}]
    ...
    set x2 [expr {$width - $width * 0.04 +1}]

    I can only guess as what might be the reason, but my €0.01 would go to a combination of coordinate origin, rounding effects, DPI of the display,
    phase of the moon, and of course currently the comet.

    R', just kidding for the last two... ;-)
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Thu Oct 17 19:45:11 2024
    From Newsgroup: comp.lang.tcl

    I want to thank all of you for responding.
    Well, what's the conclusion? If you all say that the circle is
    centered who am I to disagree.
    It is somewhat troublesome that apparently I cannot trust my own eyes
    but, as I said, who am I to disagree.
    @et99: Yes, I have always had rather strong glasses but so far I had
    never have any reason to mistrust my perception. Ok, it sems that
    there always is a first time.
    Maybe I should attribute it to age - not a very pleasant thought
    either. Well ...
    Many thanks again
    Helmut
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From saito@saitology9@gmail.com to comp.lang.tcl on Thu Oct 17 14:43:01 2024
    From Newsgroup: comp.lang.tcl

    On 10/17/2024 1:45 PM, Helmut Giese wrote:
    I want to thank all of you for responding.
    Well, what's the conclusion? If you all say that the circle is
    centered who am I to disagree.
    It is somewhat troublesome that apparently I cannot trust my own eyes
    but, as I said, who am I to disagree.

    They are identical for me as well, and the Tk verson has rugged edges,
    which you noted. I suspect the difference is somewhere between your
    monitor and the scaling factor Windows uses.

    If you want to see the circle "inside" the rectangle with no edges
    touching, I would recommend a slight change: increment the starting
    position by 1, but also reduce the size of the circle by 1.

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Shaun Deacon@sdeacon@us.socionext.com to comp.lang.tcl on Fri Oct 18 09:28:00 2024
    From Newsgroup: comp.lang.tcl

    Helmut Giese wrote:
    I want to thank all of you for responding.
    Well, what's the conclusion? If you all say that the circle is
    centered who am I to disagree.
    It is somewhat troublesome that apparently I cannot trust my own eyes
    but, as I said, who am I to disagree.
    @et99: Yes, I have always had rather strong glasses but so far I had
    never have any reason to mistrust my perception. Ok, it sems that
    there always is a first time.
    Maybe I should attribute it to age - not a very pleasant thought
    either. Well ...
    Many thanks again
    Helmut


    Looking at your images, the rectangle and circle are closer to the
    top-left corner of the canvas than the bottom-right - is this what you
    mean ?

    Shaun
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Helmut Giese@hgiese@ratiosoft.com to comp.lang.tcl on Fri Oct 18 20:53:14 2024
    From Newsgroup: comp.lang.tcl

    Looking at your images, the rectangle and circle are closer to the
    top-left corner of the canvas than the bottom-right - is this what you
    mean ?
    Yes, exactly. However, meanwhile I got convinced that it was only an
    optical illusion (with the help of Tk itself): I enhanced my OP and it
    showed that there is no difference.
    ---
    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
    } 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
    }
    $c create rectangle $x1 $y1 $x2 $y2 -outline red
    puts "horizontal diff: [expr {$x1}] vs [expr {$width - $x2}]"
    puts "vertical diff: [expr {$y1}] vs [expr {$height - $x2}]"
    ---
    So, no matter what the monitor shows (or what we believe it shows) the
    output says that all margins are 8 px.
    Helmut
    --- Synchronet 3.20a-Linux NewsLink 1.114