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.
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).
--
+------------------------------------------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
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
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.
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
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