setCoef: Functions to Convert Tk and User Coordinates

Description Usage Arguments Examples

View source: R/functions.R

Description

Convert Tk coordinates from/to user coordinates.

Usage

1
2
3
4
setCoef(W, width, height)
getCoef(W)
tk2usr(W, x = NULL, y = NULL)
usr2tk(W, x = NULL, y = NULL)

Arguments

W

the window (toplevel). If W is missing the getCoef function returns the coefficients for the last toplevel visited.

width

width of the canvas (image)

height

height of the canvas (image)

x

x position.

y

y position.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
## Not run: 

bb <- 1
tt <- tktoplevel()
tt <- tkRplot(tt, function() {
 x <- 1:20 / 20
   plot(
   x,
   x ^ bb,
   col = "#0000ff50",
       xlab = "x",
           ylab = paste0("x^", bb),
               type = "l",
                   axes = FALSE,
                       lwd = 4)
   title(main = bb)
     points(x,
      x ^ bb,
      col = "#ff000050",
      pch = 19,
      cex = 2)
        axis(1)
        axis(2)
          box()
          })

 getCoef()

 tkbind(tt$env$canvas, "<Button-1>", function(x, y)
 print(tk2usr(x, y)))

 # A more complex example
 local({
 canPos <-.Tcl(paste(tt$env$canvas, "create text 0 0 "))
 canPosX <-.Tcl(paste(tt$env$canvas, "create text 0 0 "))
 canPosY <-.Tcl(paste(tt$env$canvas, "create text 0 0 "))
 lineVertical <- .Tcl(paste(tt$env$canvas, "create line 0 0 0 0"))
 lineHorizontal<-.Tcl(paste(tt$env$canvas, "create line 0 0 0 0"))
 tkbind(tt, "<Motion>", function (x, y) {
   x <- as.numeric(x)
     y <- as.numeric(y)
       for (i in c(canPos, lineVertical, lineHorizontal,canPosX,canPosY))
       tkdelete(tt$env$canvas, tclvalue(i))

            xy <- formatC(tk2usr(x, y),
                            digits = 2,
                             format = "f",
                              width = 5)

xRange <- tt$env$plt[1:2] * tt$env$width
  yRange <- (1 - tt$env$plt[4:3]) * tt$env$height
    canPos <<- .Tcl(
 paste(tt$env$canvas, "create text 40 10 -fill blue -justify left -text { ",
     xy[1], " ", xy[2],
     "} -font {Helvetica -10}"))
      if (x < xRange[1] | x > xRange[2])
         return()
         if (y < yRange[1] | y > yRange[2])
           return()
canPosX <<- .Tcl(paste(tt$env$canvas, "create text ", x, yRange[1]-10,
           " -fill blue -justify center -text { ",xy[1],
           "} -font {Helvetica -10}"))
canPosY <<- .Tcl(paste(tt$env$canvas, "create text ",xRange[2]+10, y,
   " -fill blue -justify center -text { ",xy[2], "} -font {Helvetica -10}"))
           lineVertical <<- .Tcl(paste(tt$env$canvas, "create line ",
                 x,      yRange[1],      x,      yRange[2],
                    "-fill blue -dash 4"))
           lineHorizontal <<- .Tcl(paste(tt$env$canvas,
                 "create line ",
                  xRange[1],  y, xRange[2], y,
                        "-fill blue -dash 4"))})
     tkbind(tt$env$canvas, "<Leave>", function (x, y)
     {tkdelete(tt$env$canvas, tclvalue(canPos))})
     } )

 
## End(Not run)

tkRplotR documentation built on June 27, 2021, 5:08 p.m.