【发布时间】: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-1 次y-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点的直线方程分割线段?
【问题讨论】:
-
你能解释一下你想做什么吗?
-
@RémiCoulaud,我知道每个垂直或水平线段的开始 (x1, y1) 点和结束 (x2,y2) 点。我们可以找到直线方程 y=kx+b,然后可以计算步长等于 1 的坐标。
标签: r line sequence polygon segment