圣诞树
马上圣诞节了,听说都在画圣诞树?
手画的才有心意!!作为脱发程序猿
作为会写代码的,哪能只满足于手绘,当然要来代码实现的啦
下面就是三个绘制的圣诞树,需要的自取:
先看这三个的效果:
0:
1:
2:
环境
下面的各个代码均是在 win10 下使用 VS2019 编译测试的,没有任何问题,可以正常运行。语言均为C/C++
部分代码转载自网络,由个人整理运行测试。
第0个
这个就比较简单了,没有什么技术含量,也没有什么心意,就不多说什么了:
#include <iostream>
using namespace std;
int main()
{
cout << "\t * " << "\n";
cout << "\t *** " << "\n";
cout << "\t ***** " << "\n";
cout << "\t ******* "<< "\n";
cout << "\t*********"<< "\n";
cout << "\t * " << "\n";
cout << "\n\n\tMerry Christmas !\n";
cout << "\t2021/12/19,create by 夏海藻\n";
return 0;
}
第一个
第一个个人感觉还是比较普通的,除了会一闪一闪的,但是能运行出来,而且效果也还行吧,至少比第一个要高级多了不是吗?
源码:
#include <cstdio>
#include <string>
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int len;
struct Dot {
int x;
int y;
char c;
}a[10000000];
int vis[10000000];
char c[5] = { 35, 36, 37, 38, 79 };
string s = "/\\";
int n;
string me = "Merry Christmas !";
string fm = "2021/12/19, create by lllong";
void getcur(short int x, short int y)//移动光标函数
{
//定义一个屏幕上的座标
COORD pos = { x,y };
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(handle, pos);;
}
void goprint(int x, int y, char c) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), rand() % 16);
getcur(x, y); printf("%c", c); //getcur(0,row);//消除光标影响
}
void del(int x, int y)
{
getcur(x, y); printf(" ");
}
int main() {
cout << "请输入你喜欢的一个数字(10 - 17),并将窗口最大化:";
cin >> n;
while (n < 10 || n>17)
{
cout << "输入有误,请重新输入:";
cin >> n;
}
//printf("2021/12/19, create by lllong\n");
cout << endl;
int Layer = 2 * n - 1;
int Space = 2 * n - 2;
for (int i = 1; i <= Layer; ++i)
{
if (i & 1) {
for (int j = 0; j < 2 * i; j += 2) {
a[len].x = j + Space + 30, a[len].y = i + 5, a[len++].c = c[rand() % 5];
}
Space -= 2;
}
else
{
int r = 0;
for (int j = 0; j < 2 * i; j += 2) {
a[len].x = j + Space + 31, a[len].y = i + 5, a[len++].c = s[r];
r = (r + 1) % 2;
}
}
}
for (int i = 0; i < Layer / 3 + 1; ++i) {
a[len].x = Layer + 29, a[len].y = i + 6 + Layer, a[len++].c = '|';
a[len].x = Layer + 30, a[len].y = i + 6 + Layer, a[len++].c = '|';
}
for (int i = 0; i < me.length(); ++i) {
a[len].x = Layer - 1 + i + 30;
a[len].y = Layer / 3 + 10 + Layer;
a[len++].c = me[i];
}
for (int i = 0; i < fm.length(); ++i) {
a[len].x = Layer - 1 + i + 30;
a[len].y = Layer / 3 + 11 + Layer;
a[len++].c = fm[i];
}
while (1) {
int i = rand() % len;
if ((a[i].c == '\\' || a[i].c == '/') && vis[i]) {
continue;
}
goprint(a[i].x, a[i].y, a[i].c);
vis[i] = 1;
}
return 0;
}
第二个
我个人对这个圣诞树还是比较喜欢的,很喜欢一个一个绘制出来的感觉。
源码:
#include <windows.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define PI 3.14159265359
#define T px + scale * r * cosf(theta), py + scale * r * sin(theta)
float sx, sy;
float sdCircle(float px, float py, float r) {
float dx = px - sx, dy = py - sy;
return sqrtf(dx * dx + dy * dy) - r;
}
float opUnion(float d1, float d2) {
return d1 < d2 ? d1 : d2;
}
float f(float px, float py, float theta, float scale, int n) {
float d = 0.0f;
for (float r = 0.0f; r < 0.8f; r += 0.02f)
d = opUnion(d, sdCircle(T, 0.05f * scale * (0.95f - r)));
if (n > 0)
for (int t = -1; t <= 1; t += 2) {
float tt = theta + t * 1.8f;
float ss = scale * 0.9f;
for (float r = 0.2f; r < 0.8f; r += 0.1f) {
d = opUnion(d, f(T, tt, ss * 0.5f, n - 1));
ss *= 0.8f;
}
}
return d;
}
int ribbon() {
float x = (fmodf(sy, 0.1f) / 0.1f - 0.5f) * 0.5f;
return sx >= x - 0.05f && sx <= x + 0.05f;
}
int main(int argc, char* argv[]) {
int n = argc > 1 ? atoi(argv[1]) : 3;
float zoom = argc > 2 ? atof(argv[2]) : 1.0f;
for (sy = 0.8f; sy > 0.0f; sy -= 0.02f / zoom, putchar('\n'))
for (sx = -0.35f; sx < 0.35f; sx += 0.01f / zoom) {
if (f(0, 0, PI * 0.5f, 1.0f, n) < 0.0f) {
if (sy < 0.1f) {
putchar('.');
}
else {
if (ribbon()) {
putchar('=');
}
else {
putchar("............................#j&o"[rand() % 32]);
}
}
}
else {
putchar(' ');
}
}
Sleep(1000);
printf("\n\n\n");
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
//_sleep(5000);
printf("Merry Christmas !\n");
Sleep(500);
for (size_t i = 0; i < 40; i++) {
putchar(' ');
}
printf("2021/12/19,create by 夏海藻\n");
getchar();
}
前端版,这个是 html + css+ Js 都已经写好了,直接就可以使用浏览器打开了。
先看下效果吧:
`
这个也是有动态效果的,时间问题,没有录制 gif,先放了张截图,后续我再补上运行效果图。
<!DOCTYPE html>
<html>
<head>
<title>圣诞节圣诞树</title>
<meta charset="utf-8">
<style>
*{
text-align:center;
}
html, body { width: 100%; height: 100%; margin: 0; padding: 0; border: 0; }
div { margin: 0; padding: 0; border: 0; }
.nav {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 27px;
background-color: white;
color: black;
text-align: center;
line-height: 25px;
}
a { color: black; text-decoration: none; border-bottom: 1px dashed black; }
a:hover { border-bottom: 1px solid red; }
.previous { float: left; margin-left: 10px; }
.next { float: right; margin-right: 10px; }
.green { color: green; }
.red { color: red; }
textarea { width: 100%; height: 100%; border: 0; padding: 0; margin: 0; padding-bottom: 20px; }
.block-outer { float: left; width: 22%; height: 100%; padding: 5px; border-left: 1px solid black; margin: 30px 3px 3px 3px; }
.block-inner { height: 68%; }
.one { border: 0; }
</style>
</head>
<body marginwidth="0" marginheight="0">
<h1>专属圣诞树</h1>
<h4>2021/12/19,create by lllong</h4>
<canvas id="c" height="356" width="446">
<script>
var collapsed = true;
function toggle()
{
var fs = top.document.getElementsByTagName('frameset')[0];
var f = fs.getElementsByTagName('frame');
if (collapsed)
{
fs.rows = '250px,*';
// enable resizing of frames in firefox/opera
fs.noResize = false;
f[0].noResize = false;
f[1].noResize = false;
}
else
{
fs.rows = '30px,*';
// disable resizing of frames in firefox/opera
fs.noResize = true;
f[0].noResize = true;
f[1].noResize = true;
}
collapsed = !collapsed;
}
</script>
<script>
var b = document.body;
var c = document.getElementsByTagName('canvas')[0];
var a = c.getContext('2d');
document.body.clientWidth; // fix bug in chrome.
</script>
<script>
// start of submission //
M=Math;Q=M.random;J=[];U=16;
T=M.sin;E=M.sqrt;
for(O=k=0;x=z=j=i=k<200;)
with(M[k]=k?c.cloneNode(0):c)
{
width=height=k?32:W=446;
with(getContext('2d'))
if(k>10|!k)
for(font='60px Impact',V='rgba(';I=i*U,fillStyle=k?k==13?V+'205,205,215,.15)':V+(147+I)+','+(k%2?128+I:0)+','+I+',.5)':'#cca',i<7;)
beginPath(fill(arc(U-i/3,24-i/2,k==13?4-(i++)/2:8-i++,0,M.PI*2,1)));
else
for(;x=T(i),y=Q()*2-1,D=x*x+y*y,B=E(D-x/.9-1.5*y+1),R=67*(B+1)*(L=k/9+.8)>>1,i++<W;)
if(D<1)beginPath(strokeStyle=V+R+','+(R+B*L>>0)+',40,.1)'),moveTo(U+x*8,U+y*8),lineTo(U+x*U,U+y*U),stroke();
for(y=H=k+E(k++)*25,R=Q()*W;P=3,j<H;)J[O++]=[x+=T(R)*P+Q()*6-3,y+=Q()*U-8,z+=T(R-11)*P+Q()*6-3,j/H*20+((j+=U)>H&Q()>.8?Q(P=9)*4:0)>>1]}
setInterval(function G(m,l){A=T(D-11);if(l)return(m[2]-l[2])*A+(l[0]-m[0])*T(D);
a.clearRect(0,0,W,W);J.sort(G);
for(i=0;L=J[i++];a.drawImage(M[L[3]+1],207+L[0]*A+L[2]*T(D)>>0,L[1]>>1))
{
if(i==2e3)a.fillText('Merry Christmas!',U,345);
if(!(i%7))a.drawImage(M[13],((157*(i*i)+T(D*5+i*i)*5)%W)>>0,((113*i+(D*i)/60)%(290+i/99))>>0);
}
D+=.02},1)
// end of submission //
</script>
</canvas>
</body>
</html>
``
<h2>总结</h2>
<p>主要是跟个风,有兴趣的可以研究下源码,技术含量不高,对于不太明白的系统函数,通过搜索引擎的简单检索就可以找到你需要的信息了,这对个人的代码研读能力也是一种锻炼不是嘛。
## 最后
**感谢观赏,一起提高,慢慢变强**
</p>
<p> </p>
</body>
</html>