Python画圣诞树看多了,挑战用C语言画一个?圣诞快乐

Posted Neutionwei

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python画圣诞树看多了,挑战用C语言画一个?圣诞快乐相关的知识,希望对你有一定的参考价值。

🍎一年一度的圣诞节再过几天就要来了,在这此向各位程序猿与程序媛提前说一句圣诞快乐!🍎

🌲圣诞节(Christmas)又称耶诞节,译名为“基督弥撒”,西方传统节日,在每年12月25日。弥撒是教会的一种礼拜仪式。圣诞节是一个宗教节,因为把它当作耶稣的诞辰来庆祝,故名“耶诞节”。🌲


⭐️第一个Demo⭐️

❤️源代码❤️

#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;

	puts("\\e[1;32m");	// 亮绿色输出
    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(' ');
        
    puts("\\e[0m");
    return 0;


🌲效果🌲

编译与执行程序:

$ gcc -o main main.c -lm
$ ./main

⭐️第二个Demo⭐️

❤️源代码❤️

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define PI 3.14159265359f

float sx, sy;

typedef float Mat[4][4];
typedef float Vec[4];

void scale(Mat* m, float s)

    Mat temp =  s,0,0,0, 0,s,0,0 ,  0,0,s,0 ,  0,0,0,1  ;
    memcpy(m, &temp, sizeof(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)

    // Culling
    
        Vec v =  0.0f, 0.5f, 0.0f, 1.0f ;
        transformPosition(&v, m, v);        
        if (sphere(v, transformLength(m, 0.55f)) == -1.0f)
            return -1.0f;
    

    float z = -1.0f;

    if (n == 0)  // Leaf
        Vec v =  0.0f, 0.5f, 0.0f, 1.0f ;
        transformPosition(&v, m, v);        
        z = sphere(v, transformLength(m, 0.3f));
      
    else  // Branch
        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;

    puts("\\e[1;32m");	// 亮绿色输出
    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(' ');
        
    puts("\\e[0m");
    return 0;

🌲效果🌲

编译与执行程序:

$ gcc -o main main.c -lm
$ ./main

❤️ 🌲

⭐️


--------------《猫和老鼠》与《米老鼠和唐老鸭》是多少人的回忆啊,哈哈,欢迎留言来聊聊!--------------

以上是关于Python画圣诞树看多了,挑战用C语言画一个?圣诞快乐的主要内容,如果未能解决你的问题,请参考以下文章

python如何画3d圣诞树

C语言画一个 blingbling 的圣诞树!详细思路+源码分享

用Python,C++,Java画圣诞树

用Python,C++,Java画圣诞树

圣诞节文案,C语言画一个 blingbling 的圣诞树

圣诞节文案,C语言画一个 blingbling 的圣诞树