【问题标题】:How to split line segments by a line equation?如何通过线方程分割线段?
【发布时间】:2019-09-14 16:54:03
【问题描述】:

我在平面上有四个由坐标定义的段:

A <- matrix(c(0, 4, 4, 0, 0,            # x    
              0, 0, 3, 3, 0), ncol=2)   # y

x <- A[,1]
y <- A[,2]
n <- dim(x)

xx <- c()
yy <- c()

片段的长度很棒1。我注意分割所有段,步长等于1

我的尝试如下。我已经计算了第 i 段的长度,dist,现在只使用水平段。我应该添加x-coordinats 的新值,然后重复dist-1y-coordinat。

for (i in 1:n-1){
dist <- sqrt((x[i] - x[i+1])^2 + (y[i] - y[i+1])^2)
if (!is.null(dist) & length(dist) > 0 & dist[1] > 1)
    {   
        # horizontal segment, 'y' is const
        if (y[i] - y[i+1] == 0)
        {    
            # split a horizontal segment on (dist-1) parts with step 1

            tmp <- c(seq(from = min(x[i], x[i+1]),
                            to   = max(x[i], x[i+1])))

            # remove 1st and last elements
            xx <- c(xx, tmp[2 : (length(tmp)-1)]) 
            yy <- c(yy, rep(y[i], dist-1));
         } # if
    } #if
#} # i
xx;yy;

输出是:

> x
[1] 1 2 3 1 2 3
> y
[1] 0 0 0 3 3 3

C <- matrix(c(x,y), ncol=2)

plot(A, col='red', type= 'l', xlim=c(min(A[,1]),max(A[,1])),
      ylim=c(min(A[,2]),max(A[,2])), xlab='x', ylab='y'); 
points(A, col='black', pch = 22); 

points(C, col='red', pch = 21); 
grid()

问题。如何通过2点的直线方程分割线段?

https://algs4.cs.princeton.edu/91primitives/

【问题讨论】:

  • 你能解释一下你想做什么吗?
  • @RémiCoulaud,我知道每个垂直或水平线段的开始 (x1, y1) 点和结束 (x2,y2) 点。我们可以找到直线方程 y=kx+b,然后可以计算步长等于 1 的坐标。

标签: r line sequence polygon segment


【解决方案1】:

很抱歉,我对您显然想要什么的理解并不完美。但也许是从上面的例子中找到所有方程或跟踪 2 点之间的所有段? 如果我在 R 中使用 segments 函数来跟踪 10 * 10 段,是你想要的吗?

all_points <- unique(rbind(A, C))

indices <- expand.grid(1:10, 1:10)
plot(all_points, xlim = c(0, 4), ylim = c(0, 3), xlab = "X", ylab = "Y")
for(i in 1:10){
  segments(x0 = all_points[indices[i, 1], 1],
           y0 = all_points[indices[i, 1], 2],
           x1 = all_points[indices[i, 2], 1],
           y1 = all_points[indices[i, 2], 2]
           )
}

【讨论】:

    猜你喜欢
    • 2015-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 2015-01-27
    • 1970-01-01
    • 2016-12-03
    • 2012-09-15
    相关资源
    最近更新 更多