yuanfan 早年间有段时期论坛里有一股用 R “不务正业”的风气,我看这股歪风时隔十几年又要刮起来了。
yuanfan 用我残存的一点初中三角函数知识勉强算了一下坐标。我只是路过手痒,上桌搓一把就走,麻将你们继续。我估计楼主的多数花都可以这样蛮力画出来,只是太杀时间了。
# 将坐标矩阵逆时针旋转 theta 度
rotate = function(m, theta) {
ct = cos(theta); st = sin(theta)
as.matrix(m) %*% matrix(c(ct, -st, st, ct), 2)
}
# 一层花瓣的坐标矩阵
petal_one = function(n = 8, init = 0, r = 1, N = 100) {
a = 2 * pi / n / 2 # 每片花瓣夹角的一半
x1 = r * cos(a)
y1 = r * sin(a)
r2 = r * tan(a)
a2 = pi/2 - a
x2 = r2 * cos(a2)
t = seq(-pi/2 - a, pi/2 + a, length.out = N)
x3 = x1 + x2 + r2 * cos(t)
y3 = r2 * sin(t)
xa = c(0, x1, x3, x1, 0)
ya = c(0, -y1, y3, y1, 0)
xy = cbind(xa, ya)
res = lapply(init + 2 * pi * (seq(n) - 1)/n, function(theta) {
rbind(rotate(xy, theta), NA)
})
do.call(rbind, res)
}
petal_all = function(L = 3, n = 8, ...) {
res = NULL
for (i in L:1) {
init = if (i %% 2 == 0) pi/n else 0
res = rbind(res, petal_one(n, init, 2 + i/L, ...))
}
res
}
# 参数:L 为层数,n 为花瓣数量
draw_flower = function(...) {
xy = petal_all(...)
par(mar = rep(1, 4), bg = '#FFFFE0')
plot(xy, type = 'n', asp = 1, ann = FALSE, axes = FALSE)
polygon(xy, col = '#FF6A6A', border = 'white', lwd = 5)
points(0, 0, cex = 10, col = 'white', bg = '#FFA500', pch = 21, lwd = 3)
}
# 默认 3 层 8 瓣
draw_flower()
# 4 层 16 瓣
draw_flower(4, 16)
# 锐化花瓣(五角花瓣)
draw_flower(N = 5)
楼主的花蕊左上角有一段白弧,估计是无意渲染出来的,但造成了画花点晶的光线效果;我画的花蕊缺了那一丁点用来表示亮晶晶的白色反光,相比之下就土了一截。