如何分割多边形并沿偏斜值填充区域? [英] How to split a polygon and fill areas along skew values?

查看:101
本文介绍了如何分割多边形并沿偏斜值填充区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果值很简单,我知道如何沿水平线分割和填充多边形的区域.

I know how to split and fill areas of a polygon along a horizontal line, if the values are quite simple.

x <- 9:15
y1 <- c(5, 6, 5, 4, 5, 6, 5)

plot(x, y1, type="l")
abline(h=5, col="red", lty=2)

polygon(x[c(1:3, 5:7)], y1[c(1:3, 5:7)], col="green")
polygon(x[3:5], y1[3:5], col="red")

y2 <- c(5, 6, 4, 7, 5, 6, 5)

plot(x, y2, type="l")
abline(h=5, col="red", lty=2)

但是,如果值偏斜些,如何获得结果呢?

But how to get the result if the values are a bit more skew?

预期产量(已购物):

推荐答案

正如@Henrik在评论中指出的,我们可以

As pointed out by @Henrik in comments we can interpolate the missing points.

如果数据以另一个非零值为中心–就我而言我们需要稍微修改一下方法.

If the data is centered around another value than zero – as in my case – we need to adapt the method a little.

x <- 9:15
y2 <- c(5, 6, 4, 7, 5, 6, 5)

zp <- 5  # zero point
d <- data.frame(x, y=y2 - zp)              # scale at zero point

# kohske's method
new_d <- do.call(rbind, 
                 sapply(1:(nrow(d) - 1), function(i) {
                   f <- lm(x ~ y, d[i:(i + 1), ])
                   if (f$qr$rank < 2) return(NULL)
                   r <- predict(f, newdata=data.frame(y=0))
                   if(d[i, ]$x < r & r < d[i + 1, ]$x)
                     return(data.frame(x=r, y=0))
                   else return(NULL)
                 })
)

d2 <- rbind(d, new_d)
d2 <- transform(d2, y=y + zp)              # descale
d2 <- unique(round(d2[order(d2$x), ], 4))  # get rid of duplicates

# plot
plot(d2, type="l")
abline(h=5, col="red", lty=2)

polygon(d2$x[c(1:3, 5:9)], d2$y[c(1:3, 5:9)], col="green")
polygon(d2$x[3:5], d2$y[3:5], col="red")

结果

这篇关于如何分割多边形并沿偏斜值填充区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆