• Can I shift the coordinates of a canvas?

    From Luc@21:1/5 to All on Fri Nov 11 13:43:48 2022
    I'm studying canvas and I decided to make a classic X/Y chart for plotting functions and equations.

    It looks good!


    # ------------- MAKE WINDOW --------------
    package require Tk
    wm withdraw .
    eval destroy [winfo children .]
    set ::w [toplevel .mycanvas]
    bind $::w <Escape> {exit 0}
    wm protocol $::w WM_DELETE_WINDOW {exit 0}
    # -------------- OUTER FRAME -------------
    frame $::w.outer
    pack $::w.outer -fill both -expand 1
    # ----------- MEASURE WINDOW -------------
    set wh [winfo screenheight $::w.outer]
    set ww [winfo screenwidth $::w.outer]
    set midwh [expr $wh / 2]
    set midww [expr $ww / 2]
    # ----------- DRAW CANVAS ----------------
    canvas $::w.outer.canv
    $::w.outer.canv configure -height $wh -width $ww
    pack $::w.outer.canv

    for {set x 0} {$x < $wh} {set x [expr $x + 40]} {
    $::w.outer.canv create line 0 $x $ww $x -fill #E5E5E5
    }
    for {set x 0} {$x < $ww} {set x [expr $x + 40]} {
    $::w.outer.canv create line $x 0 $x $wh -fill #E5E5E5
    }
    $::w.outer.canv create line 0 $midwh $ww $midwh -fill black
    $::w.outer.canv create line $midww 0 $midww $wh -fill black

    wm attributes $::w -zoomed 1
    # ------------------------------------

    Now, the 0 coordinate refers to either the leftmost edge (x) or the top
    of the screen (y).

    Of course I can work with that.

    But I would love to be able to "shift" everything so that zero always refers
    to the middle vertical line (x) and the middle horizontal line (y) when
    writing code, I mean:

    Instead of writing

    $::w.outer.canv create line 0 540 1920 540 -fill black

    I would write

    $::w.outer.canv create line -540 0 540 0 -fill black

    So the leftmost edge is now negative and the center is zero.

    I read the manual page and found a section that explains about COORDINATES
    and COORDINATES, but I don't really understand what it says. For example,

    "Canvases do not support scaling or rotation of the canvas coordinate system relative to the window coordinate system."

    So is it possible to change the entire reference point of a canvas so that
    zero is always the center?

    --
    Luc


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Robert Heller@21:1/5 to [email protected] on Fri Nov 11 19:18:52 2022
    At Fri, 11 Nov 2022 13:43:48 -0300 Luc <[email protected]> wrote:


    I'm studying canvas and I decided to make a classic X/Y chart for plotting functions and equations.

    It looks good!


    # ------------- MAKE WINDOW --------------
    package require Tk
    wm withdraw .
    eval destroy [winfo children .]
    set ::w [toplevel .mycanvas]
    bind $::w <Escape> {exit 0}
    wm protocol $::w WM_DELETE_WINDOW {exit 0}
    # -------------- OUTER FRAME -------------
    frame $::w.outer
    pack $::w.outer -fill both -expand 1
    # ----------- MEASURE WINDOW -------------
    set wh [winfo screenheight $::w.outer]
    set ww [winfo screenwidth $::w.outer]
    set midwh [expr $wh / 2]
    set midww [expr $ww / 2]
    # ----------- DRAW CANVAS ----------------
    canvas $::w.outer.canv
    $::w.outer.canv configure -height $wh -width $ww
    pack $::w.outer.canv

    for {set x 0} {$x < $wh} {set x [expr $x + 40]} {
    $::w.outer.canv create line 0 $x $ww $x -fill #E5E5E5
    }
    for {set x 0} {$x < $ww} {set x [expr $x + 40]} {
    $::w.outer.canv create line $x 0 $x $wh -fill #E5E5E5
    }
    $::w.outer.canv create line 0 $midwh $ww $midwh -fill black
    $::w.outer.canv create line $midww 0 $midww $wh -fill black

    wm attributes $::w -zoomed 1
    # ------------------------------------

    Now, the 0 coordinate refers to either the leftmost edge (x) or the top
    of the screen (y).

    Of course I can work with that.

    But I would love to be able to "shift" everything so that zero always refers to the middle vertical line (x) and the middle horizontal line (y) when writing code, I mean:

    Instead of writing

    $::w.outer.canv create line 0 540 1920 540 -fill black

    I would write

    $::w.outer.canv create line -540 0 540 0 -fill black

    So the leftmost edge is now negative and the center is zero.

    I read the manual page and found a section that explains about COORDINATES and COORDINATES, but I don't really understand what it says. For example,

    "Canvases do not support scaling or rotation of the canvas coordinate system relative to the window coordinate system."

    So is it possible to change the entire reference point of a canvas so that zero is always the center?

    Yes. What you do is set the canvas's *Scroll Region*:

    $::w.outer.canv configure -scrollregion [list -540 -540 540 540]
    # left top right bottom

    If the window size of the canvas is 1080 x 1080 (1080 = 540 - (-540)), then
    the canvas would not need scrollbars and the canvas coords will behave as you want (0,0 would be the center of the canvas, -540,-540 would be the top left corner, etc.).

    If the canvas is smaller, you can add a set of scrollbars (see "man 3tk options" re -xscrollcommand and -yscrollcommand, "man 3tk scrollbar", and "man 3tk canvas re: xview and yview).

    --
    Robert Heller -- Cell: 413-658-7953 GV: 978-633-5364
    Deepwoods Software -- Custom Software Services
    http://www.deepsoft.com/ -- Linux Administration Services
    [email protected] -- Webhosting Services

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From [email protected]@21:1/5 to All on Fri Nov 11 19:25:06 2022
    But I would love to be able to "shift" everything so that zero always refers >to the middle vertical line (x) and the middle horizontal line (y) when >writing code, I mean:

    You can draw everything around the origin, them move all items by adding an x and y displacement
    using the canvas move command.


    Instead of writing

    $::w.outer.canv create line 0 540 1920 540 -fill black

    I would write

    $::w.outer.canv create line -540 0 540 0 -fill black

    So the leftmost edge is now negative and the center is zero.

    I read the manual page and found a section that explains about COORDINATES >and COORDINATES, but I don't really understand what it says. For example,

    "Canvases do not support scaling or rotation of the canvas coordinate system >relative to the window coordinate system."

    However they do support scrolling. See the -scrollregion configuration option and the canvas xview and yview commands. If you set -scrollregion size to match the window size, the whole scroll region will be displayed (xview and yview are not needed).

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)