Anirban166 / GSoC21-Tests

Solutions for tests pertaining to directlabels improvements.
0 stars 0 forks source link

Grid constructs #2

Closed Anirban166 closed 3 years ago

Anirban166 commented 3 years ago

Thinking to post my learnings/notes + ingeniously1 written code here, in the process of trying out grid functions during my free time.

1rarely applies

Anirban166 commented 3 years ago
library(grid)
vp <- viewport(x = 0.5, y = 0.5, width = 0.9, height = 0.9)
pushViewport(vp)
# Left or right (-- or ++) x-axis shift value to increase/decrease spacing in b/w letters:
x.shift = 0.05
# Set common-to-all values for grid parameters:
line.width = 5 
# Coordinates: (x1, x2) <-> (y1, y2)
# G:
grid.lines(c(0.1 + x.shift, 0.2 + x.shift), c(0.3, 0.3), gp = gpar(lwd = line.width))
grid.lines(c(0.1 + x.shift, 0.1 + x.shift), c(0.3, 0.1), gp = gpar(lwd = line.width))
grid.lines(c(0.1 + x.shift, 0.2 + x.shift), c(0.1, 0.1), gp = gpar(lwd = line.width))
grid.lines(c(0.2 + x.shift, 0.2 + x.shift), c(0.2, 0.1), gp = gpar(lwd = line.width))
grid.lines(c(0.15 + x.shift, 0.2 + x.shift), c(0.2, 0.2), gp = gpar(lwd = line.width))
# S:
grid.lines(c(0.3, 0.4), c(0.3, 0.3), gp = gpar(lwd = line.width))
grid.lines(c(0.3, 0.3), c(0.3, 0.2), gp = gpar(lwd = line.width))
grid.lines(c(0.3, 0.4), c(0.2, 0.2), gp = gpar(lwd = line.width))
grid.lines(c(0.4, 0.4), c(0.2, 0.1), gp = gpar(lwd = line.width))
grid.lines(c(0.3, 0.4), c(0.1, 0.1), gp = gpar(lwd = line.width))
# O:
# line: (5,6 - 3, 3), (5, 5 - 5, 1), (6, 6 - 3, 1), (5, 6 - 1, 1)
# rectangle: 
grid.rect(x = 0.5, y = 0.2, width = 0.1, height = 0.2, gp = gpar(lwd = line.width))
# circle: (needs 0.5 x.shift for letters before and after it to look better)
# grid.circle(x = 0.55, y = 0.2, r = 0.1, gp = gpar(lwd = line.width))
# C:
grid.lines(c(0.6, 0.7), c(0.3, 0.3), gp = gpar(lwd = line.width))
grid.lines(c(0.6, 0.7), c(0.1, 0.1), gp = gpar(lwd = line.width))
grid.lines(c(0.6, 0.6), c(0.3, 0.1), gp = gpar(lwd = line.width))
Screenshot 2021-02-27 at 1 44 09 PM
Anirban166 commented 3 years ago

Creating a new viewport (after initial vp) and switching back and forth:

vp2 <- viewport(x = 0.5, y = 0.75, width = 0.5, height = 0.4)
pushViewport(vp2)
# Specifications of new viewports (vp2 here) are relative to the viewport that it has been
# pushed into (i.e. vp) . For example, the width is 0.5 units relative to vp, but relative 
# to the original plotting region (the 1×1 box), the width is 0.5 × 0.9 = 0.45. vp2 will be 
# the child of vp, and vp the parent of vp2. (vp -> vp2)
pushViewport(vp2) # -> move focus to child vp2 (from vp)
upViewport()      # -> move up to parent (vp)
downViewport()    # -> move down to child (vp2)
popViewport()     # -> pop current viewport (vp2) and move back to parent (vp)
# move up down 'n' times: upViewport(n), downViewport(n)

Can also name viewports for ease of access:

pushViewport(viewport(name = "A")) # cur -> A : A
upViewport()                       # cur <- A : cur   
pushViewport(viewport(name = "B")) # cur -> B : B
upViewport()                       # cur <- B : cur
downViewport("A")                  # cur -> A : A
seekViewport("B")                  # cur -> B : B

Yes, seekViewport() always starts searching from the top-level viewport.
The current viewport tree can be viewed using current.vpTree(). o/p:

viewport[ROOT]->(viewport[A], viewport[B])

Will come back to viewports again later.

For now, let me emphasize on the fact that Issue #1 is missing. That in itself seems like an issue, maybe I need another issue (#3) reflecting thoughts on that.

Anyways, here's a tunnel perspective of nested viewports for the general use of hypnotism:

pushViewport(viewport())
grid.lines(c(.05, .95), c(.95, .05))
grid.lines(c(.05, .95), c(.05, .95))
for (i in 1:100) {
  # Create a viewport which is 90% of the current viewport:
  vp <- viewport(h = 0.9, w = 0.9)
  pushViewport(vp)
  # Show the viewport boundary by constructing a rectangle with its dimensions:
  grid.rect()
}
Screenshot 2021-02-27 at 9 59 45 PM

Courtesy: pdf1, pdf2

Anirban166 commented 3 years ago

(ノ^^)ノconsole.log(Comment(1).output + Comment(2).output)

Screenshot 2021-02-27 at 10 32 21 PM
gsoc.gridart <- function(line.width = 5) {
  grid.lines(c(0.15, 0.25), c(0.3, 0.3), gp = gpar(lwd = line.width))
  grid.lines(c(0.15, 0.15), c(0.3, 0.1), gp = gpar(lwd = line.width))
  grid.lines(c(0.15, 0.25), c(0.1, 0.1), gp = gpar(lwd = line.width))
  grid.lines(c(0.25, 0.25), c(0.2, 0.1), gp = gpar(lwd = line.width))
  grid.lines(c(0.2, 0.25), c(0.2, 0.2), gp = gpar(lwd = line.width))
  grid.lines(c(0.3, 0.4), c(0.3, 0.3), gp = gpar(lwd = line.width))
  grid.lines(c(0.3, 0.3), c(0.3, 0.2), gp = gpar(lwd = line.width))
  grid.lines(c(0.3, 0.4), c(0.2, 0.2), gp = gpar(lwd = line.width))
  grid.lines(c(0.4, 0.4), c(0.2, 0.1), gp = gpar(lwd = line.width))
  grid.lines(c(0.3, 0.4), c(0.1, 0.1), gp = gpar(lwd = line.width))
  grid.rect(x = 0.5, y = 0.2, width = 0.1, height = 0.2, gp = gpar(lwd = line.width))
  grid.lines(c(0.6, 0.7), c(0.3, 0.3), gp = gpar(lwd = line.width))
  grid.lines(c(0.6, 0.7), c(0.1, 0.1), gp = gpar(lwd = line.width))
  grid.lines(c(0.6, 0.6), c(0.3, 0.1), gp = gpar(lwd = line.width))
}
pushViewport(viewport())
grid.lines(c(.05, .95), c(.95, .05))
grid.lines(c(.05, .95), c(.05, .95))
for (i in 1:100) {
  vp <- viewport(h = 0.9, w = 0.9)
  pushViewport(vp)
  grid.rect()
}
upViewport(100)
for (i in 1:100) {
  vp <- viewport(h = 0.9, w = 0.9)
  pushViewport(vp)
  if(i == 5) {
    pushViewport(viewport(x = 0.2))
    gsoc.gridart() # GSOC!
    upViewport()
  }
  if(i == 7) {
    pushViewport(viewport(x = 0.8))
    gsoc.gridart() # GSOC..again?
    upViewport()
  }
  if(i == 15) {
    gsoc.gridart() # wait, we can't do it three times as a student right?
  }  
}

Note: As evident, grid.lines would be better to construct O, since grid.rect overshadows the lines behind (kind of like being filled with white), and appears as the topmost layer.

Anirban166 commented 3 years ago

Closing issues I'm using as notes in this repository, since I can keep on commenting after closing anyway.