我在坐标定义的平面上有四个部分:
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
-coordinat重复y
次。
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个点分割线段?
答案 0 :(得分:1)
对不起,但是我对您明确想要的东西的理解并不完美。但是,也许是查找所有等式或跟踪上述示例中两点之间的所有线段?
如果我在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]
)
}