gpt4 book ai didi

r - 使用网格填充在 R 中绘制的贝塞尔曲线

转载 作者:行者123 更新时间:2023-12-02 09:49:12 26 4
gpt4 key购买 nike

我有一张图如下:

library(grid)

grid.newpage()

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"),
height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"),
height = unit(0.6, "npc"))

for (i in c(0.1, 0.17)) {
x <- c(0.08, 0.05, i, 0.08)
y <- c(0.15, 0.25, 0.25, 0.5)
grid.bezier(x, y, gp = gpar(col = "green3"))
}

for (i in c(0.38, 0.8)) {
x <- c(0.4, i, i, 0.9)
y <- c(0.4, i, 0.15, 0.15)
grid.bezier(x, y, gp = gpar(col = "blue3"))
}

这给了我下图, Figure-1

在这里,我想用绿色和蓝色填充形状。有没有更简单的方法来实现这一目标。如果我必须使用 grid.polygon,我必须指定很多点来绘制该形状。

最佳答案

您是否考虑过使用grid.xspline?它可以采用闭合形状 (open = FALSE),因此可以采用填充 颜色。然而,形状会有所不同,如下所示。

library(grid)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 1)))

## Beziers
pushViewport(viewport(layout.pos.row = 1))
grid.text("Bezier", x = 0.05, y = 0.8, just = "left", gp=gpar(cex = 2))

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"),
height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"),
height = unit(0.6, "npc"))

x1 <- c(0.08, 0.05, 0.1, 0.08, 0.08, 0.17, 0.05, 0.08)
y1 <- c(0.15, 0.25, 0.25, 0.5, 0.5, 0.25, 0.25, 0.15)
id <- rep(1:2, each = 4)
grid.bezier(x1, y1, id = id, gp = gpar(col = "green3"))

x2 <- c(0.4, .38, .38, 0.9, .9, .8, .8, .4)
y2 <- c(0.4, .38, 0.15, 0.15, .15, .15, .8, .4)
grid.bezier(x2, y2, id = id, gp = gpar(col = "blue4"))

upViewport(1)

## X-splines
pushViewport(viewport(layout.pos.row = 2))
grid.text("X-spline", x = .05, y = 0.8, just = "left", gp=gpar(cex = 2))

grid.rect(x = 0.1, y = 0.4, width = unit(0.18, "npc"),
height = unit(0.6, "npc"))
grid.rect(x = 0.65, y = 0.4, width = unit(0.6, "npc"),
height = unit(0.6, "npc"))

s <- c(0, 1, 1, 0, 0, 1, 1, 0)
grid.xspline(x1, y1, shape = s, open = FALSE, gp = gpar(col = "green3", fill = "green3"))
grid.xspline(x2, y2, shape = s, open = FALSE, gp = gpar(col = "blue4", fill = "blue4"))

upViewport(0)

enter image description here

关于r - 使用网格填充在 R 中绘制的贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49211452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com