# (x1, y1) と (x2, y2) を通る直線と垂直に交わり,(x3, y3) を通る直線の切片と傾き
prog <- function(x1, y1, x2, y2, x3, y3)
{
a <- -(x2-x1)/(y2-y1)
b <- y3-a*x3
return(c(intercept=b, slope=a))
}
# 以下は,利用法と説明図を描くためのプログラム
line2 <- function(x1, y1, x2, y2)
{
abline(y1-(y2-y1)/(x2-x1)*x1, (y2-y1)/(x2-x1), col="green")
}
x1 <- 1; y1 <- 3
x2 <- 5; y2 <- 5
x3 <- 2; y3 <- 6
(ans <- prog(x1, y1, x2, y2, x3, y3))
par(xpd=TRUE, mar=c(5, 5, 2, 2))
plot(c(x1, x2, x3), c(y1, y2, y3), pch=19, xlab="x", ylab="y", asp=1)
line2(x1, y1, x2, y2)
abline(ans["intercept"], ans["slope"], col="green")
text(c(x1, x2, x3), c(y1, y2, y3), paste("(x", 1:3, ", y", 1:3, ")", sep=""), pos=4)
実行結果として,
intercept slope
10 -2
を得る
prog <- function(x1, y1, x2, y2, x3, y3)
{
a <- -(x2-x1)/(y2-y1)
b <- y3-a*x3
return(c(intercept=b, slope=a))
}
# 以下は,利用法と説明図を描くためのプログラム
line2 <- function(x1, y1, x2, y2)
{
abline(y1-(y2-y1)/(x2-x1)*x1, (y2-y1)/(x2-x1), col="green")
}
x1 <- 1; y1 <- 3
x2 <- 5; y2 <- 5
x3 <- 2; y3 <- 6
(ans <- prog(x1, y1, x2, y2, x3, y3))
par(xpd=TRUE, mar=c(5, 5, 2, 2))
plot(c(x1, x2, x3), c(y1, y2, y3), pch=19, xlab="x", ylab="y", asp=1)
line2(x1, y1, x2, y2)
abline(ans["intercept"], ans["slope"], col="green")
text(c(x1, x2, x3), c(y1, y2, y3), paste("(x", 1:3, ", y", 1:3, ")", sep=""), pos=4)
実行結果として,
intercept slope
10 -2
を得る
※コメント投稿者のブログIDはブログ作成者のみに通知されます