title: R: how many rectangles a line crosses
body: <p>I have a data frame that is used to draw rectangles. I would like to know how many rectangles a vertical line crosses at any x-coordinate.</p>
  
  <pre><code>DataFrame = structure(list(topleftx = c(567L, 762L, 579L), toplefty = c(1000L, 
  1001L, 1304L), bottomrightx = c(761L, 956L, 949L), bottomrighty = c(1292L, 
  1309L, 1774L), PageDetailID = c("0014214H565", "0014215H565", 
  "0014216H565"), Page = c(3L, 3L, 3L)), .Names = c("topleftx", 
  "toplefty", "bottomrightx", "bottomrighty", "PageDetailID", "Page"
  ), row.names = 5:7, class = "data.frame")
  
  xmin = round_any(min(DataFrame$topleftx),100)
  xmax = round_any(max(DataFrame$bottomrightx),100)
  ymin = round_any(min(DataFrame$toplefty),100)
  ymax = round_any(max(DataFrame$bottomrighty),100)
  
  print(paste(xmin,xmax,ymin,ymax))
  
  prec_x = round_any(0.1*(xmax-xmin),10)
  prec_y = round_any(0.1*(ymax-ymin),10)
  
  topleftx = round_any(DataFrame$topleftx,prec_x)
  toplefty = round_any(DataFrame$toplefty,prec_y)
  bottomrightx = round_any(DataFrame$bottomrightx,prec_x)
  bottomrighty = round_any(DataFrame$bottomrighty,prec_y)
  
  
  # View(DataFrame)
  require(grDevices)
  ## set up the plot region:
  op &lt;- par(bg = "white")
  plot(c(xmin, xmax), c(ymin, ymax), type = "n", xlab = "", ylab = "", main = "Y-axis should be inverted")
  
  #569  935   723 1076
  rect(topleftx, toplefty, bottomrightx, bottomrighty , col = c(NA,0))#, col = rainbow(11, start = 0.7, end = 0.1))
  </code></pre>
  
  <p>Here's the plot:</p>
  
  <p><a href="https://i.stack.imgur.com/VEmiD.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VEmiD.jpg" alt="enter image description here"></a></p>
  
  <p>So, at all <code>x</code> in this example the answer should be 2. Note: borders are not always at same <code>y</code> - there could be a gap between rectangles. </p>
  
  <p>Any ideas? Thanks!</p>