使用C语言绘制一个笑脸smile

您所在的位置:网站首页 眨眼的笑脸怎么画 使用C语言绘制一个笑脸smile

使用C语言绘制一个笑脸smile

2024-07-14 02:56| 来源: 网络整理| 查看: 265

首先我们绘制一个简单的例子

#include #include int main() { double a, b; for (b = 1; b >= -1; b -= 0.05, putchar('\n')) for (a = -1; a 0.5 && a * a + pow(b - 0.5, 2) > 0.02]); }

运行后,可以在控制台显示出一个笑脸~~~

# if you do not have gcc, run `brew install gcc` to install gcc test1.c && ./a.out gcc test2.c && ./a.out

然后我们可以循环生成多个笑脸(当然可以再加一个循环生成三重笑脸)

#include #include int func(double x, double y, int d) { return pow(x - 0.5, 2) + pow(y - 0.5, 2) < 0.25 && fabs(atan2(y - 0.5, x - 0.5)) > 0.5 && pow(x - 0.5, 2) + pow(y - 0.75, 2) > 0.005 && (d == 0 || f(fmod(x * 8, 1), fmod(y * 8, 1), d - 1)); } int main() { double x, y; for (y = 1; y >= 0; y -= 1.0 / 160.0, putchar('\n')) for (x = 0; x b ? a : b; } float map(V p) { V m1 = { cosf(1), 0, sinf(1) }, m2 = { cosf(1), 0, -sinf(1)}; return min(max(len(p) - 1.0f, min(dot(p, m1), dot(p, m2))), p.z + 1.0f); } V normal(V p) { float e = 1e-6f; V dx = { e, 0, 0 }, dy = {0, e, 0}, dz = {0, 0, e }; return norm(make( map(add(p, dx)) - map(sub(p, dx)), map(add(p, dy)) - map(sub(p, dy)), map(add(p, dz)) - map(sub(p, dz)))); } V albedo(V p) { if (p.z + 1 < 1e-4f) return make(1, 1, 1); // ground else if (p.x * p.x + powf(p.z - 0.5, 2) < 0.02f) return make(0, 0, 0); // eye else return make(1, 1, 0); // main } #define S 1024 unsigned char img[S * S * 3], *c = img; int main() { V eye = { 2, 3, 1 }, at = { 0, 0, 0 }, up = { 0, 0, 1 }; V vz = norm(sub(at, eye)), vx = cross(up, vz), vy = cross(vz, vx); int x, y, i; for (y = S - 1; y >= 0; y--) for (x = 0; x < S; x++, c += 3) { float sx = ((float)x / S - 0.5f) * 3.0f; float sy = ((float)y / S - 0.5f) * 3.0f; V o = add(add(eye, mul(vx, sx)), mul(vy, sy)); float t = 0; do { V p = add(o, mul(vz, t)); float sd = map(p); if (sd < 1e-4f) { V n = normal(p), alb = albedo(p); float ao = 1, s = 0.5f; for (i = 1; i < 5; i++) { float d = i * 0.1f; ao = max(ao - (d - map(add(p, mul(n, d)))) * s, 0.1f); s *= 0.95f; } V r = mul(alb, (dot(n, norm(make(0, 1, 1))) * 0.5f + 0.5f) * ao * 255); c[0] = (unsigned char)r.x; c[1] = (unsigned char)r.y; c[2] = (unsigned char)r.z; break; } t += sd; } while (t < 100); } svpng(fopen("pacman.png", "wb"), S, S, img, 0); }

是不是很厉害!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3