不知道现在大家画小猪佩奇用什么工具?不管用的是什么,如果你是计算机专业或者正在学编程,那么你可以试试用C语言画出小猪佩奇。
使用带符号距离场(signed distance field, SDF)表示圆形:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps1.png
沿用这个方法表示形状,但这次我们想利用 ASCII 字符|/=画出形状的外框,并填充内部,类似这样:
=====
//.....\
||.......||
\.....//
=====
SDF 的梯度(gradient)代表 SDF 变化最大的方向,可用这个方向去决定用哪一个字符。
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps2.png
我们通过差分求 SDF 的梯度近似值,然后用atan2()求出梯度的角度:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps3.jpg
用 C 语言简单实现,在画布中画一个半径 0.8 并带有 0.1寛度外框的圆形:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps4.jpg
#include #include #define T doubleT f(T x, T y) {
return sqrt(x * x + y * y) - 0.8f;}char outline(T x, T y) {
T delta = 0.001;
if (fabs(f(x, y)) < 0.05) {
T dx = f(x + delta, y) - f(x - delta, y);
T dy = f(x, y + delta) - f(x, y - delta);
return "|/=\|/=\|"[(int)((atan2(dy, dx) / 6.2831853072 + 0.5) * 8 + 0.5)];
}
else if (f(x, y) < 0)
return '.';
else
return ' ';}int main() {
for (T y = -1; y < 1; y += 0.05, putchar(' '))
for (T x = -1; x < 1; x += 0.025)
putchar(outline(x, y));}
代码可以左右移动!▲
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps5.png
然后,我们就可以画多个圆形,把它们适当地旋转和缩放,用构造实体几何比它们组合起来,那么用 19 行代码就可以画出小猪佩奇了:
代码可以左右移动!▼
// ASCII Peppa Pig by Milo Yip#include #include #include #define T double
T c(T x,T y,T r){return sqrt(x*x+y*y)-r;}
T u(T x,T y,T t){return x*cos(t)+y*sin(t);}
T v(T x,T y,T t){return y*cos(t)-x*sin(t);}
T fa(T x,T y){return fmin(c(x,y,0.5),c(x*0.47+0.15,y+0.25,0.3));}
T no(T x,T y){return c(x*1.2+0.97,y+0.25,0.2);}
T nh(T x,T y){return fmin(c(x+0.9,y+0.25,0.03),c(x+0.75,y+0.25,0.03));}
T ea(T x,T y){return fmin(c(x*1.7+0.3,y+0.7,0.15),c(u(x,y,0.25)*1.7,v(x,y,0.25)+0.65,0.15));}
T ey(T x,T y){return fmin(c(x+0.4,y+0.35,0.1),c(x+0.15,y+0.35,0.1));}
T pu(T x,T y){return fmin(c(x+0.38,y+0.33,0.03),c(x+0.13,y+0.33,0.03));}
T fr(T x,T y){return c(x*1.1-0.3,y+0.1,0.15);}
T mo(T x,T y){return fmax(c(x+0.15,y-0.05,0.2),-c(x+0.15,y,0.25));}
T o(T x,T y,T(*f)(T,T),T i){T r=f(x,y);return fabs(r)<0.02?(atan2(f(x,y+1e-3)-r,f(x+1e-3,y)-r)+0.3)*1.273+6.5:r<0?i:0;}
T s(T x,T y,T(*f)(T,T),T i){return f(x,y)<0?i:0;}
T f(T x,T y){return o(x,y,no,1)?fmax(o(x,y,no,1),s(x,y,nh,12)):fmax(o(x,y,fa,1),fmax(o(x,y,ey,11),fmax(o(x,y,ea,1),fmax(o(x,y,mo,1),fmax(s(x,y,fr,13),s(x,y,pu,12))))));}
int main(int a,char**b){for(T y=-1,s=a>1?strtod(b[1],0):1;y<0.6;y+=0.05/s,putchar(' '))for(T x=-1;x<0.6;x+=0.025/s)putchar(" .|/=\|/=\| @!"[(int)f(u(x,y,0.3),v(x,y,0.3))]);}
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps6.png
2倍:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps7.png
4倍:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps8.png
8倍:
file:///C:UsersAdministrator.WIN-STED6B9V5UIAppDataLocalTempksohtml9656wps9.png
怎么样?这下会了吗?你还可以尝试着让这只佩奇动起来哟!
最后,如果你也想成为程序员,想要快速掌握编程,赶紧加入学习企鹅圈子!
里面有资深专业软件开发工程师,在线解答你的所有疑惑~编程语言入门“so easy”
编程学习书籍:
编程学习视频: