Attaching package: 'dbscan'
The following object is masked from 'package:stats':
as.dendrogram
Attaching package: 'dbscan'
The following object is masked from 'package:stats':
as.dendrogram
dt <- fread("input.txt")
colnames(dt) <- c("x", "y")
dt$x <- dt$x + 1
dt$y <- dt$y + 1
dt$id <- 1:nrow(dt)
head(dt)
x y id
1: 109 325 1
2: 47 92 2
3: 357 217 3
4: 210 170 4
5: 171 332 5
6: 333 216 6
size <- max(max(dt$x), max(dt$y))
mat <- matrix(NA, size, size)
mat[as.matrix(dt[, 1:2])] <- dt$id
for(x in 1:ncol(mat)) {
for(y in 1:nrow(mat)) {
distX <- abs(x - dt$x)
distY <- abs(y - dt$y)
dist <- (distX + distY) / 2
closest <- dist[dist == min(dist)]
mat[x,y] <- ifelse(length(closest) == 1, which.min(dist), NA)
}
}
edges <- unique(c(mat[1, ], mat[,1], mat[nrow(mat), ], mat[, ncol(mat)]))
mat[mat %in% edges] <- NA
max(table(mat))
[1] 4166