如何从随机自截取线列表(R中的psp)生成段列表?

如何从随机自截取线列表(R中的psp)生成段列表?,r,segment,spatstat,R,Segment,Spatstat,我正在使用X=rpoisline(4)生成线条,并使用plot(X)进行绘制。 对于X$端点我有它们的坐标和它们与selfcrossing.psp(X)(在带有spatstat的R中:库(spatstat)) 我需要得到一个线段及其坐标的列表,并能够操纵它们(更改它们的方向、位置、交点…)。这些线段必须通过一条线与另一条线以及与窗的交点来定义 那么,我是否错过了一个简单的方法来转换一个psp的几个相交的线在一个psp的非相交部分(我希望它是清楚的) 如果你有一个不简单的方法,我很感兴趣 谢谢你的

我正在使用
X=rpoisline(4)
生成线条,并使用
plot(X)
进行绘制。 对于
X$端点
我有它们的坐标和它们与
selfcrossing.psp(X)
(在带有spatstat的R中:
库(spatstat)

我需要得到一个线段及其坐标的列表,并能够操纵它们(更改它们的方向、位置、交点…)。这些线段必须通过一条线与另一条线以及与窗的交点来定义

那么,我是否错过了一个简单的方法来转换一个psp的几个相交的线在一个psp的非相交部分(我希望它是清楚的)

如果你有一个不简单的方法,我很感兴趣

谢谢你的时间

编辑:

以下是我的台词:

这里是我认为如果我能处理好每一个片段(一个接一个),我可以产生的一些随机的东西。所以我需要从我的随机线列表中得到一个线段列表


好的,几次咖啡休息之后,这里有一些错误代码可以满足您的需要。清理工作我将留给你

ranpoly <- function(numsegs=10,plotit=TRUE) {

require(spatstat)
# temp fix: put the first seg into segset. Later make it a constrained random.
segset<-psp(c(0,1,1,0,.25),c(0,0,1,1,0),c(1,1,0,0,1),c(0,1,1,0,.75),owin(c(0,1),c(0,1)) ) #frame the frame
for (jj in 1: numsegs) {
    # randomly select a segment to start from, a point on the seg, the slope,and direction
    # later... watch for slopes that immediately exit the frame
    endx <-sample(c(-0.2,1.2),1)  #force 'x1' outside the frame
# watch that sample() gotcha
    if(segset$n<=5) sampset <- c(5,5) else sampset<-5:segset$n
    startseg<-sample(sampset,1) #don't select a frame segment
    # this is slope of segment to be constructed
    slope <- tan(runif(1)*2*pi-pi) # range +/- Inf 
    # get length of selected segment
    seglen<-lengths.psp(segset)[startseg]
    startcut <- runif(1) 
    # grab the coords of starting point (similar triangles)
    startx<- segset$ends$x0[startseg] + (segset$ends$x1[startseg]-segset$ends$x0[startseg])*startcut #seglen
    starty<- segset$ends$y0[startseg] + (segset$ends$y1[startseg]-segset$ends$y0[startseg])*startcut #seglen
    # make a psp object with that startpoint and slope; will adjust it after finding intersections
    endy <- starty + slope*(endx-startx)
    newpsp<-psp(startx,starty,endx,endy,segset$window,check=FALSE)
    # don't calc crossing for current element of segset
    hits <- crossing.psp(segset[-startseg],newpsp)
    segdist <- dist(cbind(c(startx,hits$x),c(starty,hits$y)))
    # dig back to get the crosspoint desired -- have to get matrixlike object out of class "dist" object
    # And, as.matrix puts a zero in location 1,1 kill that row.
    cutx <- hits$x[ which.min( as.matrix(segdist)[-1,1] )]
    cuty <- hits$y[which.min(as.matrix(segdist)[-1,1] )]
    segset <- superimpose(segset,psp(startx,starty,cutx,cuty,segset$window))

} #end jj loop
if(plotit) plot(segset,col=rainbow(numsegs))
return(invisible(segset))
}

ranpoly好的,几次咖啡休息后,这里有一些bug代码,可以满足您的需要。清理工作我将留给你

ranpoly <- function(numsegs=10,plotit=TRUE) {

require(spatstat)
# temp fix: put the first seg into segset. Later make it a constrained random.
segset<-psp(c(0,1,1,0,.25),c(0,0,1,1,0),c(1,1,0,0,1),c(0,1,1,0,.75),owin(c(0,1),c(0,1)) ) #frame the frame
for (jj in 1: numsegs) {
    # randomly select a segment to start from, a point on the seg, the slope,and direction
    # later... watch for slopes that immediately exit the frame
    endx <-sample(c(-0.2,1.2),1)  #force 'x1' outside the frame
# watch that sample() gotcha
    if(segset$n<=5) sampset <- c(5,5) else sampset<-5:segset$n
    startseg<-sample(sampset,1) #don't select a frame segment
    # this is slope of segment to be constructed
    slope <- tan(runif(1)*2*pi-pi) # range +/- Inf 
    # get length of selected segment
    seglen<-lengths.psp(segset)[startseg]
    startcut <- runif(1) 
    # grab the coords of starting point (similar triangles)
    startx<- segset$ends$x0[startseg] + (segset$ends$x1[startseg]-segset$ends$x0[startseg])*startcut #seglen
    starty<- segset$ends$y0[startseg] + (segset$ends$y1[startseg]-segset$ends$y0[startseg])*startcut #seglen
    # make a psp object with that startpoint and slope; will adjust it after finding intersections
    endy <- starty + slope*(endx-startx)
    newpsp<-psp(startx,starty,endx,endy,segset$window,check=FALSE)
    # don't calc crossing for current element of segset
    hits <- crossing.psp(segset[-startseg],newpsp)
    segdist <- dist(cbind(c(startx,hits$x),c(starty,hits$y)))
    # dig back to get the crosspoint desired -- have to get matrixlike object out of class "dist" object
    # And, as.matrix puts a zero in location 1,1 kill that row.
    cutx <- hits$x[ which.min( as.matrix(segdist)[-1,1] )]
    cuty <- hits$y[which.min(as.matrix(segdist)[-1,1] )]
    segset <- superimpose(segset,psp(startx,starty,cutx,cuty,segset$window))

} #end jj loop
if(plotit) plot(segset,col=rainbow(numsegs))
return(invisible(segset))
}

ranpolyspatstat
功能
selfcut.psp
正是为此而设计的

Y <- selfcut.psp(X)

Yspatstat
功能
selfcut.psp
正是为此而设计的

Y <- selfcut.psp(X)

Y您能否提供一个示例,即生成您的输出的一些命令?这将给人们提供一些可供实验的东西,从而增加获得有用答案的机会。同样的问题是,您有一组表示交点的有序对,但这些数据没有映射(例如,通过它们在数据数组中的位置)对于定义上述交叉点所涉及的线的数据,这里是我输入到R库(spatstat)lignes=rpoisline(4)plot(lignes)lignes$ends interP=selfcrossing.psp(lignes)plot(interP,add=T,col=“red”)@CarlWitthoft:这是我的问题!很抱歉,我没有设置评论的格式…请发布您正在制作的情节以及您想要制作的情节草图。我担心语言障碍会混淆这个问题。你能提供一个例子吗,即一些生成你的输出的命令?这将给人们提供一些可供实验的东西,从而增加获得有用答案的机会。同样的问题是,您有一组表示交点的有序对,但这些数据没有映射(例如,通过它们在数据数组中的位置)对于定义上述交叉点所涉及的线的数据,这里是我输入到R库(spatstat)lignes=rpoisline(4)plot(lignes)lignes$ends interP=selfcrossing.psp(lignes)plot(interP,add=T,col=“red”)@CarlWitthoft:这是我的问题!很抱歉,我没有设置评论的格式…请发布您正在制作的情节以及您想要制作的情节草图。我担心语言障碍会混淆问题。这太棒了!我会试着为我的主题处理它,我可能会带着问题回来找你非常感谢您抽出时间!这太棒了!我会试着为我的主题处理它,我可能会带着问题回来找你非常感谢您抽出时间!哦,太好了——3年后提供一个干净简单的解决方案,然后窃取我的分数。:-。不,说真的,我很高兴你发布了这段代码和我的乱七八糟的代码。哦,太好了——3年后提供了一个很好的简单解决方案,偷走了我的分数。:-。不,说真的,我很高兴你发布了这个和我的乱七八糟的代码。