1

我有一个线段图案,它与另一条线成 90 度角:

require(spatstat)    
range <- c(0,10)

owin <- owin(xrange = range, yrange = range)

l1 <- psp(x0 = 8, x1 = 8, y0 = 2, y1 = 8, window = owin, marks = "l1")
l2 <- psp(x0 = 6, x1 = 6, y0 = 2, y1 = 8, window = owin, marks = "l2")
l3 <- psp(x0 = 4, x1 = 4, y0 = 2, y1 = 8, window = owin, marks = "l3")
l4 <- psp(x0 = 2, x1 = 2, y0 = 2, y1 = 8, window = owin, marks = "l4")

lines <- superimpose(l1, l2, l3, l4)

main <- psp(x0 = 8, x1 = 0, y0 = 5, y1 = 5, window = owin, marks = "main")

angles.psp(lines)*(180/pi)
[1] 90 90 90 90

这是一个视觉表示:

plot(x = range, y = range, type = "n", main = "", asp = 1, axes = F, xlab = "x", ylab = "y")
plot(lines, col = "darkgrey", add = T)
plot(main, col = "black", add = T)
axis(1)
axis(2, las = 2)

在此处输入图像描述

现在我想旋转lines,使它们main在同一点交叉,但角度为 45 度。

lines.rotated <- rotate(lines, -0.7853982)
angles.psp(lines.rotated)*(180/pi)
[1] 45 45 45 45

这可行,但angles.psp函数似乎owin根据我的需要单独旋转窗口()而不是行。

plot(x = range, y = range, type = "n", main = "", asp = 1, axes = F, xlab = "x", ylab = "y")
plot(lines, col = "darkgrey", add = T)
plot(lines.rotated, col = "blue", add = T)
plot(main, col = "black", add = T)
axis(1)
axis(2, las = 2)

在此处输入图像描述

有没有办法lines相对于main线旋转所有角度,使角度为 45 度,但交叉点保持不变?

4

2 回答 2

1

只是为了使答案自成一体。下面的代码应该会产生您正在寻找的东西(如果这是一个有用的函数,我们可以要求 Adrian(spatstat 的维护者)在 spatstat 中包含它的改进版本)。首先是先决条件:

require(spatstat)

linerotate.psp <- function(X, L, angle){
    ## Window:
    W <- as.owin(X)
    ## Empty psp object:
    Y <- psp(numeric(0),numeric(0),numeric(0),numeric(0),W)
    for(i in 1:X$n){
        ## Line i:
        Xi <- X[i]
        ## Crossing of line i and test line L in the window:
        cross <- crossing.psp(L, Xi)
        ## Rotate line if the crossing is non-empty:
        if(npoints(cross)>0){
            m <- as.numeric(coords(cross))
            ## Move to crossing:
            Xi <- affine(Xi, vec = -m)
            ## Rotate:
            Xi <- rotate(Xi, angle = angle)
            ## Move back:
            Xi <- affine(Xi, vec = m)
            ## Restrict to non-rotated window:
            Xi <- Xi[W]
        }
        ## Collect results:
        Y <- superimpose(Y, Xi)
    }
    return(Y)
}

数据:

W <- square(10)

x0 <- c(8,6,4,2)
x1 <- x0
y0 <- rep(2,4)
y1 <- rep(8,4)

lines <- psp(x0, y0, x1, y1, window = W)

main <- psp(x0 = 8, x1 = 0, y0 = 5, y1 = 5, window = W)

旋转后的数据和角度对比:

rotlines <- linerotate.psp(lines, main, angle = -pi/4)
angles.psp(lines)*(180/pi)
[1] 90 90 90 90
angles.psp(rotlines)*(180/pi)
[1] 45 45 45 45

图形化:

plot(lines, col = "darkgrey")
plot(main, col = "black", add = T)
plot(rotlines, col = "blue", add = T)

旋转线

用非水平测试线图形化:

main <- psp(x0 = 0, x1 = 10, y0 = 3, y1 = 7, window = W)
rotlines <- linerotate.psp(lines, main, angle = -pi/4)

plot(lines, col = "darkgrey")
plot(main, col = "black", add = T)
plot(rotlines, col = "blue", add = T)

旋转线 2

于 2014-02-26T10:44:52.547 回答
1

我不确定我是否明白你的想法。

对于简单的方法,可以elide从 maptools 中选择吗?

像这样的东西:

library(maptools)
# convert lines to SpatialLines
slines <- as(lines, 'SpatialLines')

plot(x = range, y = range, type = "n", main = "", asp = 1, axes = F,
     xlab = "x", ylab = "y")
plot(lines, col = "darkgrey", add = T)
plot(main, col = "black", add = T)
axis(1)
axis(2, las = 2)
# Plot slines object on top of your data
plot(elide(slines, rotate = 45, center = c(5, 5)), add = T)

# plot it

旋转线

但是您可能希望旋转线中的每条线都穿过线和主线之间的交点。真的吗?

如果是这样,则在每个 psp 行上循环。对于 psp l4:

plot(elide(slines[4], rotate = 45, center = c(2, 5)), add = T)

在十字路口绘制

你可以用

as.psp.Lines(from, ..., window=NULL, marks=NULL, fatal)
于 2014-02-26T10:26:34.140 回答