Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Tk Canvas coord system question

1,245 views
Skip to first unread message

Arman Ohanian

unread,
Mar 30, 2009, 10:38:32 PM3/30/09
to
Hi all,

The Tk Canvas uses a coordinate system where the origin is actually in
the top left corner by default.
I find it counterintuitive since most coordinate systems have the 0,0 at
lower-left.

Is there a way to change that in the canvas?

If not, what's the best way to work around it?

All my data I am trying to plot comes in with the assumption that origin
is at the lower-left.

Thanks,
-Arman.

Jeff Godfrey

unread,
Mar 31, 2009, 12:13:24 AM3/31/09
to
Arman Ohanian wrote:
> Hi all,
>
> The Tk Canvas uses a coordinate system where the origin is actually in
> the top left corner by default.
> I find it counterintuitive since most coordinate systems have the 0,0 at
> lower-left.
>
> Is there a way to change that in the canvas?

Hi Arman,

You should be able to use the canvas [scale] subcommand to get what you
need. Specifically...

$c scale all 0 0 1 -1

The -1 "yScale" factor will effectively flip the coordinate system so
that positive Y values move "up" the screen. You may have to use
something like the above with either a different origin point or a
[move] subcommand to get exactly what you want, but it shouldn't be too
difficult.

Jeff

Gerald W. Lester

unread,
Mar 31, 2009, 9:19:54 AM3/31/09
to

Another way is to multiple all your Y coordinates by -1 as you go to place
them on the canvas (or use them from the canvas).

--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

Jeff Godfrey

unread,
Mar 31, 2009, 10:09:06 AM3/31/09
to
Gerald W. Lester wrote:
> Jeff Godfrey wrote:
>> Arman Ohanian wrote:
>>> Hi all,
>>>
>>> The Tk Canvas uses a coordinate system where the origin is actually
>>> in the top left corner by default.
>>> I find it counterintuitive since most coordinate systems have the 0,0
>>> at lower-left.
>>>
>>> Is there a way to change that in the canvas?
>>
>> Hi Arman,
>>
>> You should be able to use the canvas [scale] subcommand to get what
>> you need. Specifically...
>>
>> $c scale all 0 0 1 -1
>>
>> The -1 "yScale" factor will effectively flip the coordinate system so
>> that positive Y values move "up" the screen. You may have to use
>> something like the above with either a different origin point or a
>> [move] subcommand to get exactly what you want, but it shouldn't be
>> too difficult.
>>
>> Jeff
>
> Another way is to multiple all your Y coordinates by -1 as you go to
> place them on the canvas (or use them from the canvas).
>

One other thought. If you're data is already scaled to fit within your
viewport, just modifying your Y-coords as follows before drawing them
should do what you want.

set yCoord = [expr {$viewPortHeight - $yCoord}]

So, to recap, you can just draw the whole thing "upside down" to begin
with and then flip it via the canvas [scale] subcommand (as in my first
post), or modify the Y coords as you draw (as in Gerald's post and this
post).

Jeff

Arman Ohanian

unread,
Mar 31, 2009, 4:48:30 PM3/31/09
to
Thanks guys.

The last two options I had figured out on my own.
The [scale] option was new. So, I will see what works.

Maybe I should explain the what I am doing just to hear your thoughts.

1. I am reading data from a file that is basically just rectangles and
polygons.
2. The user needs edit the data. Stretch the edges of the rectangles
and polygons, change tags, etc.
3. I need to write the edited data back to a text file.

So, the problems:
1. User need to be able to zoom-in and out, pan, fit. User should be
able to click and draw a box to zoom in.
a. How to keep track of the scale? I was thinking of adding a
"reference" box to the canvas that is 1x1. Then, I can check this box
size after zooming and figure out the zoom-level. I can divide all the
object sizes from the screen by this value before writing out data.
b. are there any good zooming solutions built-in?
2. I need to have x/y axis for the user's reference and have labels on
them. When I use the zoom solutions on the web, the axis shrink with
the other canvas objects.
3. Need to implement a rules so the user can figure measure the 'world
dimensions' of the object on the canvas.


... I guess that's it. Seems pretty simple, huh? ;o)

Thanks for all the inputs.

-Arman.

Jeff Godfrey

unread,
Mar 31, 2009, 5:34:25 PM3/31/09
to
Arman Ohanian wrote:
> Thanks guys.
>
> The last two options I had figured out on my own.
> The [scale] option was new. So, I will see what works.
>
> Maybe I should explain the what I am doing just to hear your thoughts.

Hi Arman,

I don't have time for a proper response right now, but here are my
thoughts quickly... ;^)

First, while you should be able to do all you need with the standard Tk
canvas widget, have you looked at all into TkZinc (http://www.tkzinc.org)?

IMO, it's much better suited for the type of application you're working
on. There, you can create true coordinate systems, assign different
coordinate systems to different objects, and easily map between systems.
That makes something like showing a live coordinate position on the
screen very easy.

And, as a bonus, TkZinc supports advanced rendering via OpenGL,
gradients, antialiasing, transparency, etc.

I think its API is a bit strange as compared to the canvas, but it's
probably a better fit for your app.

Jeff

Arman Ohanian

unread,
Apr 2, 2009, 6:14:19 PM4/2/09
to
Hi Jeff,

Is TKZinc pure-tcl extension?
Do you know if it's ok to use it in a proprietary commercial application?

Thanks,
-Arman

Jeff Godfrey

unread,
Apr 2, 2009, 6:33:14 PM4/2/09
to
Arman Ohanian wrote:
> Hi Jeff,
>
> Is TKZinc pure-tcl extension?
> Do you know if it's ok to use it in a proprietary commercial application?
>
> Thanks,
> -Arman

Arman,

TkZinc is a binary extension, but its website has builds available for
lots of platforms. Regarding licensing, you should verify the details
yourself (on the site), but here's a cut/paste from there:

"Tkzinc is available as open source under the GNU Lesser General Public
License (LGPL)"

The site is at --> www.tkzinc.org

Jeff

0 new messages