Re: returning coordinates of canvas objects in integer values



On 30 Apr., 06:30, sd <sham...@xxxxxxxxxxx> wrote:
Greetings,

I noticed when I run my script on a screen resolution 1440x900 and get
coordinates of objects (ex.: .w coords tag1) I get floating numbers
ending with large number of decimals 549.99999999999999 whereas a
screen with resolution 1024x768 gives me numbers such as 550.0

This causes problems for me because I have to look for objects in
certain locations and 549.9999999999 throws things off. Is there a way
to neatly get object coordinates in integers or do I have to keep
converting them myself each time they are returned by the [coords]
command?

Not every time, only when you really need integer coordinates... and
then this little helper may help:

proc listint lst {
set res {}
foreach i $lst {lappend res [expr {round($i)}]}
return $res
}

Note however that floating point coordinates keep some precision in
the case you zoom your canvas in or out.

Alternatively, your item location code could use more elastic
checking, allowing for a certain "halo":

if {[closenuff $x $x0] && [closenuff $y $y0]} ...

where

proc closenuff {a b} {expr {abs($a-$b) <= 1}}
.