C Language Print Christmas tree

Source: Internet
Author: User
Tags mul

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

int main (int argc, char* argv[]) {
int n = argc > 1? Atoi (Argv[1]): 4;
for (int j = 1; J <= N; j + +) {
int s = 1 << j, k = (1 << N)-S, x;
for (int y = s-j; y >= 0; y--, putchar (' \ n ')) {
for (x = 0; x < y + K, x + +) printf ("");
for (x = 0; x + y < S; + +) printf ("%c", '! ' ^ y & x);
for (x = 1; x + y < S; + +) printf ("%c", '! ' ^ Y & (S-y-x-1));
}
}
}


*********************************************************************************************

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

#define PI 3.14159265359

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

#define T px + scale * R * COSF (theta), py + scale * r * sin (theta)

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 main (int argc, char* argv[]) {
int n = argc > 1? Atoi (Argv[1]): 3;
for (sy = 0.8f; sy > 0.0f; SY-= 0.02f, Putchar (' \ n '))
for (SX = -0.35f; sx < 0.35f; SX + 0.01f)
Putchar (f (0, 0, PI * 0.5f, 1.0f, N) < 0? ‘*‘ : ‘ ‘);
}


*********************************************************************************************

#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) {
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 (&AMP;M3, T, M3);
Mul (&AMP;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 (");
}
}

*********************************************************************************************

Print Christmas tree in C language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.