これも以前書いたもの
drawTree <- function(a.x, a.y, b.x, b.y, n, col=1)
{
STEM.RATIO <- 0.25
BRANCH.RATIO <- 0.6
xx <- b.x-a.x
yy <- a.y-b.y
angle1 <- atan(yy/xx)+pi/6
angle2 <- atan(yy/xx)-pi/6*1.5
center.length <- sqrt(xx^2+yy^2)*(1-STEM.RATIO)
branch.length <- BRANCH.RATIO*center.length
sig <- ifelse(xx >= 0, 1, -1)
c.x <- a.x+STEM.RATIO*xx
c.y <- a.y-STEM.RATIO*yy
d.x <- c.x+sig*(branch.length*cos(angle1))
d.y <- c.y-sig*(branch.length*sin(angle1))
e.x <- c.x+sig*(branch.length*cos(angle2))
e.y <- c.y-sig*(branch.length*sin(angle2))
segments(a.x, a.y, c.x, c.y, col=col)
if (n <= 0) {
segments(c.x, c.y, c(b.x, d.x, e.x), c(b.y, d.y, e.y), col=col)
}
else {
drawTree(c.x, c.y, b.x, b.y, n-1, col=col)
drawTree(c.x, c.y, d.x, d.y, n-1, col=col)
drawTree(c.x, c.y, e.x, e.y, n-1, col=col)
}
}
plot(c(00,500), c(0, 500), type="n", axes=FALSE, xlab="", ylab="", asp=1)
for (i in 1:10) {
x <- sample(500, 1)
y1 <- sample(500, 1)
y2 <- y1-runif(1, min=100, max=400)
drawTree(x, y2, x, y1, sample(4, 1)+2, col=i)
}
描画結果
drawTree <- function(a.x, a.y, b.x, b.y, n, col=1)
{
STEM.RATIO <- 0.25
BRANCH.RATIO <- 0.6
xx <- b.x-a.x
yy <- a.y-b.y
angle1 <- atan(yy/xx)+pi/6
angle2 <- atan(yy/xx)-pi/6*1.5
center.length <- sqrt(xx^2+yy^2)*(1-STEM.RATIO)
branch.length <- BRANCH.RATIO*center.length
sig <- ifelse(xx >= 0, 1, -1)
c.x <- a.x+STEM.RATIO*xx
c.y <- a.y-STEM.RATIO*yy
d.x <- c.x+sig*(branch.length*cos(angle1))
d.y <- c.y-sig*(branch.length*sin(angle1))
e.x <- c.x+sig*(branch.length*cos(angle2))
e.y <- c.y-sig*(branch.length*sin(angle2))
segments(a.x, a.y, c.x, c.y, col=col)
if (n <= 0) {
segments(c.x, c.y, c(b.x, d.x, e.x), c(b.y, d.y, e.y), col=col)
}
else {
drawTree(c.x, c.y, b.x, b.y, n-1, col=col)
drawTree(c.x, c.y, d.x, d.y, n-1, col=col)
drawTree(c.x, c.y, e.x, e.y, n-1, col=col)
}
}
plot(c(00,500), c(0, 500), type="n", axes=FALSE, xlab="", ylab="", asp=1)
for (i in 1:10) {
x <- sample(500, 1)
y1 <- sample(500, 1)
y2 <- y1-runif(1, min=100, max=400)
drawTree(x, y2, x, y1, sample(4, 1)+2, col=i)
}
描画結果
![](https://blogimg.goo.ne.jp/user_image/5d/3f/9d5d6a8551d752a3c050e4f24cad965e.png)
※コメント投稿者のブログIDはブログ作成者のみに通知されます