Discussion:
How to center a circle?
(too old to reply)
Helmut Giese
2024-10-16 13:15:15 UTC
Permalink
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
Ralf Fassel
2024-10-16 13:29:17 UTC
Permalink
* Helmut Giese <***@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'
Helmut Giese
2024-10-16 15:00:20 UTC
Permalink
Hello Ralf,
Post by Ralf Fassel
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
Ralf Fassel
2024-10-16 16:36:36 UTC
Permalink
* Helmut Giese <***@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'
Helmut Giese
2024-10-16 17:57:59 UTC
Permalink
Hello Ralf,
Post by Ralf Fassel
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
et99
2024-10-17 01:58:18 UTC
Permalink
Post by Helmut Giese
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 :)
Ralf Fassel
2024-10-17 08:55:00 UTC
Permalink
* Helmut Giese <***@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... ;-)
Rich
2024-10-16 17:08:25 UTC
Permalink
Post by Helmut Giese
If I knew a site where I could upload a screen shot everybody would
see what I am talking about.
Try:

https://0x0.st
Helmut Giese
2024-10-17 17:45:11 UTC
Permalink
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
saito
2024-10-17 18:43:01 UTC
Permalink
Post by Helmut Giese
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.
Shaun Deacon
2024-10-18 16:28:00 UTC
Permalink
Post by Helmut Giese
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
Helmut Giese
2024-10-18 18:53:14 UTC
Permalink
Post by Shaun Deacon
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

Loading...