已经是深冬了。今晚是平安夜。你会给你的男/女朋友一个圣诞水果吗?
作为一个科技之家,很抱歉不能给每一个小伙伴都送苹果,所以给大家一个技巧,用各种编程语言写一棵圣诞树,祈祷所有关心我的小伙伴都平安无事!
00-1010如何用C语言画一棵“圣诞树”,我们就不介绍太简单的方法了(用于循环打印几个' * ')。直接看网络上大神写的圣诞树。是一种好的感觉吗?
下面是C语言的源代码,可以自己编译运行。
# include math . h # include stdio . h # include stdlib . h #定义PI 3.14159265359float sx,sy;float sdCircle(float px,float py,float r) { float dx=px - sx,dy=py-sy;返回sqrtf(dx * dx dy * dy)-r;}float opUnion(float d1,float d2) { return d1 d2?d1 : d2} #定义T px标度* r * cosf(),py标度* r * sin()float f(float px,float py,float theta,float scale,int n){ float d=0.0f;for(float r=0.0f;r 0.8fr=0.02f) d=opUnion(d,sdCircle(T,0.05f *刻度*(0.95 f-r)));if (n 0)为(int t=-1;t=1;t=2) {浮TT=t * 1.8f;float ss=scale * 0.9ffor(float r=0.2f;r 0.8fr=0.1f) { d=opUnion(d,f(T,tt,ss * 0.5f,n-1));ss *=0.8f} }返回d;}int main(int argc,char* argv[]) { int n=argc 1?atoi(argv[1]): 3;对于(sy=0.8fsy 0.0fsy -=0.02f,putchar('\n ')为(sx=-0.35 f;sx 0.35fsx=0.01f) putchar(f(0,0,PI * 0.5f,1.0f,n) 0?'*' : ' ');}因为原来圣诞树的树叶和树枝都换成了“*”,风格太单一了。经过作者的优化,成功升级到3D效果还是挺酷的。由于浮点运算太多,运行以下程序需要很长时间,耗时近1060s。动手能力强的小伙伴可以尝试优化一下。
下面是源代码:
# include math . h # include stdio . h # include stdlib . h # include string . h # define PI 3.1415926359 float sx,sy;typedef浮动垫[4][4];typedef float Vec[4];空隙比例(Mat* m,float s) { Mat temp={ {s,0,0,0},{0,s,0,0 },{ 0,0,s,0 },{ 0,0,0,1 } };记忆(m,温度,大小(Mat));}void rotateY(Mat* m,float t) { float c=cosf(t),s=sinf(t);Mat temp={ {c,0,s,0},{0,1,
0,0}, {-s,0,c,0}, {0,0,0,1} }; memcpy(m, &temp, sizeof(Mat));}void rotateZ(Mat* m, float t) { float c = cosf(t), s = sinf(t); Mat temp = { {c,-s,0,0}, {s,c,0,0}, {0,0,1,0}, {0,0,0,1} }; memcpy(m, &temp, sizeof(Mat));}void translate(Mat* m, float x, float y, float z) { Mat temp = { {1,0,0,x}, {0,1,0,y}, {0,0,1,z}, {0,0,0,1} }; memcpy(m, &temp, sizeof(Mat));}void mul(Mat* m, Mat a, Mat b) { Mat temp; for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) { temp[j][i] = 0.0f; for (int k = 0; k < 4; k++) temp[j][i] += a[j][k] * b[k][i]; } memcpy(m, &temp, sizeof(Mat)); }void transformPosition(Vec* r, Mat m, Vec v) { Vec temp = { 0, 0, 0, 0 }; for (int j = 0; j < 4; j++) for (int i = 0; i < 4; i++) temp[j] += m[j][i] * v[i]; memcpy(r, &temp, sizeof(Vec)); }float transformLength(Mat m, float r) { return sqrtf(m[0][0] * m[0][0] + m[0][1] * m[0][1] + m[0][2] * m[0][2]) * r;}float sphere(Vec c, float r) { float dx = c[0] - sx, dy = c[1] - sy; float a = dx * dx + dy * dy; return a < r * r ? sqrtf(r * r - a) + c[2] : -1.0f;}float opUnion(float z1, float z2) { return z1 > z2 ? z1 : z2;}float f(Mat m, int n) { float z = -1.0f; for (float r = 0.0f; r < 0.8f; r += 0.02f) { Vec v = { 0.0f, r, 0.0f, 1.0f }; transformPosition(&v, m, v); z = opUnion(z, sphere(v, transformLength(m, 0.05f * (0.95f - r)))); } if (n > 0) { Mat ry, rz, s, t, m2, m3; rotateZ(&rz, 1.8f); for (int p = 0; p < 6; p++) { rotateY(&ry, p * (2 * PI / 6)); mul(&m2, ry, rz); float ss = 0.45f; for (float r = 0.2f; r < 0.8f; r += 0.1f) { scale(&s, ss); translate(&t, 0.0f, r, 0.0f); mul(&m3, s, m2); mul(&m3, t, m3); mul(&m3, m, m3); z = opUnion(z, f(m3, n - 1)); ss *= 0.8f; } } } return z;}float f0(float x, float y, int n) { sx = x; sy = y; Mat m; scale(&m, 1.0f); return f(m, n);}int main(int argc, char* argv[]) { int n = argc > 1 ? atoi(argv[1]) : 3; float zoom = argc > 2 ? atof(argv[2]) : 1.0f; for (float y = 0.8f; y > -0.0f; y -= 0.02f / zoom, putchar('\n')) for (float x = -0.35f; x < 0.35f; x += 0.01f / zoom) { float z = f0(x, y, n); if (z > -1.0f) { float nz = 0.001f; float nx = f0(x + nz, y, n) - z; float ny = f0(x, y + nz, n) - z; float nd = sqrtf(nx * nx + ny * ny + nz * nz); float d = (nx - ny + nz) / sqrtf(3) / nd; d = d > 0.0f ? d : 0.0f; // d = d < 1.0f ? d : 1.0f; putchar(".-:=+*#%@@"[(int)(d * 9.0f)]); } else putchar(' '); }}
python
提供一种最简单的python实现方法,哈哈!不喜勿喷!
源代码如下:
height = 5stars = 1for i in range(height): print((' ' * (height - i)) + ('*' * stars)) stars += 2print((' ' * height) + '|')
也可以使用简单的图形库Turtle来实现,就像下面这样子。原理也很简单,参照前面最简单的实现方法,加载图形库后把原有的*使用绿色的方块代替,在各个角和顶部采用红色圆形代替,树干用棕色方块代替即可,最后在适当润色一下。
当然还有另外一种实现方式,是一种动态的效果。
下面是源代码:
from turtle import *import randomimport timen = 80.0speed("fastest")screensize(bg='seashell')left(90)forward(3*n)color("orange", "yellow")begin_fill()left(126)for i in range(5): forward(n/5) right(144) forward(n/5) left(72)end_fill()right(126)color("dark green")backward(n*4.8)def tree(d, s): if d <= 0: return forward(s) tree(d-1, s*.8) right(120) tree(d-3, s*.5) right(120) tree(d-3, s*.5) right(120) backward(s)tree(15, n)backward(n/2)for i in range(200): a = 200 - 400 * random.random() b = 10 - 20 * random.random() up() forward(b) left(90) forward(a) down() if random.randint(0, 1) == 0: color('tomato') else: color('wheat') circle(2) up() backward(a) right(90) backward(b)time.sleep(60)
JAVA
这款JAVA版本的圣诞树和前面Python版本类似,不同的地方在于此版本引用的较多的素材和动态效果,在点击了左上角的按钮为ON之后开始播放音乐,圣诞树也开始闪烁变化。
这里放出作者的github链接,作者为[codeXiaoMing]:
大家学会了吗?快去编译打包一下发给朋友吧!
往期推荐:
上手世界上最小的操作系统MenuetOS,仅有1.4M,安装运行全教程
Windows系统被养蛊,这些下“崽”器简直神了
内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/157920.html