调用R中的多边形时禁止重绘 [英] Suppress redraw when calling polygon in R

查看:131
本文介绍了调用R中的多边形时禁止重绘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在R中的一幅图上绘制60,000多个不重叠的三角形(非结构化三角形网格的一部分)。目前,每个图需要15-20分钟,这使得无法使用它来制作动画。例如,

I would like to draw 60,000+ non-overlapping triangles (part of an unstructured triangular mesh) on a single plot in R. Currently, it takes 15-20 minutes per plot, which makes it impossible to use this for making animations. For example,

n <- 100 #Except replace this with 60,000
x <- matrix(runif(3*n), n)
y <- matrix(runif(3*n), n)
cols <- heat.colors(n)[order(x[,1])]
poly <- function(i) {polygon(x[i,], y[i,], col=cols[i])}
plot(0, xlim=c(min(x),max(x)), ylim=c(min(y),max(y)))
sapply(1:n, poly)

是否可以在每个多边形之后抑制多边形()重绘?我猜这是最耗时的步骤,并且在手册页中没有提及。对于如何实现这一点的替代建议将不胜感激。谢谢。

Is it possible to suppress polygon() from redrawing after every polygon? I'm guessing that is the most time consuming step, and it's not mentioned in the man page. Alternative suggestions for how to achieve this would be appreciated. Thank you.

推荐答案

您可以将多个多边形传递给多边形。你所要做的就是用 NA 分开。这里是一个代码:

You can pass several polygons to polygon. All you have to do is separate then with NA. here's a code:

cuts <- function(x)
{
    n <- length(x) %/% 3

    map <- rep(c(TRUE,TRUE,TRUE,FALSE), n)

    result <- rep(NA, n*4)

    result[map] <- x

    result
}


set.seed(1234)

n <- 10000
x <- matrix(runif(3*n), n)
y <- matrix(runif(3*n), n)
cols <- heat.colors(n)[order(x[,1])]
plot(0, xlim=c(min(x),max(x)), ylim=c(min(y),max(y)))
polygon(x=cuts(t(x)), y=cuts(t(y)), col=cols)

工作迅速。我测试过控制种子,并与代码生成的图进行比较。

Works fast. I've tested controlling the seed and comparing with the plot generated by your code.

这篇关于调用R中的多边形时禁止重绘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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