目的: 2点が与えられたら、それらを結ぶ弧の座標を見つけてプロットします。 実装:円弧の点(circleFun
)とそれをプロットする関数(plottest
)の1つの関数。色は赤から緑へのパスの方向を示します。なぜlines関数がR内のパスを閉じますか?
circleFun <- function(x,y)
{
center <- c((x[1]+y[1])/2,(x[2]+y[2])/2)
diameter <- as.numeric(dist(rbind(x,y)))
r <- diameter/2
tt <- seq(0,2*pi,length.out=1000)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
res <- data.frame(x = xx, y = yy)
if((x[1]<y[1] & x[2]>y[2]) | (x[1]>y[1] & x[2]<y[2])){
res <- res[which(res$x>min(c(x[1],y[1])) & res$y>min(c(x[2],y[2]))),]
} else {
res <- res[which(res$x<max(c(x[1],y[1])) & res$y>min(c(x[2],y[2]))),]
}
return(res)
}
plottest <- function(x1,y1)
{
plot(c(x1[1],y1[1]),c(x1[2],y1[2]),
xlim=c(-2,2),ylim=c(-2,2),col=2:3,pch=20,cex=2,asp=1)
lines(circleFun(x1,y1))
}
par(mfrow=c(2,2))
plottest(c(1,-1),c(-1, 1))
plottest(c(-1, 1),c(1,-1))
plottest(c(-1,-1),c(1, 1))
plottest(c(1, 1),c(-1,-1))
結果:
質問:lines
機能は図中のパス[1,1]と[1,2]を閉じ、なぜ私が把握することはできませんそれはしばらく図[2,1]と[2,2]ではそうではありません。期待される結果は、すべての図が第2列のものとすべきである。
ありがとうございました!
1:この機能は、それが他の1つの入力点からプロット利益を有します。サークル内の点の数を(例えば)10に減らすと、OPの関数にプロット内のいずれかの端点が含まれていないことがわかります。 – Simon
awsome、非常にエレガントなコード – Emer