R:一条线穿过多少个矩形

时间:2016-05-06 18:41:13

标签: r plot

我有一个用于绘制矩形的数据框。我想知道垂直线在任何x坐标处穿过多少个矩形。

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 <- 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))

这是情节:

enter image description here

所以,在这个示例中,所有x的答案都应该是2.注意:边框并不总是相同y - 矩形之间可能存在间隙。

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:3)

如果x值位于topleftx&lt; = x&lt; = right right x

的范围内,

线将穿过一个矩形

x.test <- 720
sum(topleftx<=x.test & x.test<=bottomrightx)

请注意有关如何处理与矩形边缘重合的线条的含糊不清。您必须决定是否要使用&lt; =(包括这些值)或&lt;排除它们。