当我想使用 rgl
绘制多边形时,对于某些坐标对,我收到错误:
# polygon data
xyz <- structure(c(5.90000000002328, 6, 6.20000000001164, 5.90000000002328,
12.2999999998137, 12.2999999998137, 12.2999999998137, 12.2999999998137,
7.45599365234375, 7.6009521484375, 7.7039794921875, 7.45599365234375
), dim = 4:3, dimnames = list(c("p1", "p2", "p3", "p1"), c("x",
"y", "Z")))
# plotting
polygon3d(
x = as.numeric(xyz[,1]),
y = as.numeric(xyz[,2]),
z = as.numeric(xyz[,3]), col = "red")
> Error in processOutside(i) : Cannot triangulate polygon
但是,对于其他多边形,它是有效的,例如用于:
# working polygon
xyz <- structure(c(3.70000000001164, 3, 3.09999999997672, 3.70000000001164,
12.2999999998137, 12, 12, 12.2999999998137, 8.74200439453125,
8.91094970703125, 8.990966796875, 8.74200439453125), dim = 4:3, dimnames = list(
c("p1", "p2", "p3", "p1"), c("x", "y", "Z")))
我认为这可能是因为点的接近,但将点乘以 100 或四舍五入小数点后,我仍然收到错误。为什么我会收到此错误?这与太锐角有关吗?
最佳答案
polygon3d
的文档说它使用 coords
参数(默认 1:2
)来选择用于三角测量的坐标多边形。由于您的 y
坐标是恒定的,这会创建一个退化三角形并且三角测量失败。
要解决此问题,请指定 coords = c(1, 3)
以告诉它使用 x
和 z
坐标,即
polygon3d(
x = as.numeric(xyz[,1]),
y = as.numeric(xyz[,2]),
z = as.numeric(xyz[,3]),
coords = c(1,3), col = "red")
正在进行一项更改以自动做出此选择;如果您安装了 rgl
的开发版本(版本 0.109.15 或更高版本),您应该很快就可以使用原始代码。使用
remotes::install_github("dmurdoch/rgl")
它将需要一整套工具(编译器等)才能运行。
关于r - 创建具有锐角的多边形不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73247916/