如何用 C 語言畫小豬佩奇

如何用 C 語言畫小豬佩奇

來自專欄 Milo的編程

我們曾在《用 C 語言畫光(一):基礎》中,使用到帶符號距離場(signed distance field, SDF)表示圓形:

phi_	ext{circle}(mathbf{x})=| mathbf{x} - mathbf{c}| - r	ag{1}

沿用這個方法表示形狀,但這次我們想利用 ASCII 字元|/=畫出形狀的外框,並填充內部,類似這樣:

===== //.....\||.......|| \.....// =====

SDF 的梯度(gradient)代表 SDF 變化最大的方向,可用這個方向去決定用哪一個字元。

我們通過差分求 SDF 的梯度近似值,然後用 atan2() 求出梯度的角度:

egin{align} 	heta &= mathrm{atan2} left(frac{partial phi(mathbf{x})}{partial y}, frac{partial phi(mathbf{x})}{partial x} 
ight) 	ag{2}\ &approx mathrm{atan2} left(phi(x,y+Delta) - phi(x,y-Delta), phi(x+Delta,y) - phi(x-Delta,y)
ight) 	ag{3}\ end{align}

用 C 語言簡單實現,在 [-1, 1]	imes[-1,1] 畫布中畫一個半徑 0.8 並帶有 0.1 寛度外框的圓形:

#include <math.h>#include <stdio.h>#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));}

然後,我們就可以畫多個圓形,把它們適當地旋轉和縮放,用構造實體幾何比它們組合起來,那麼用19行代碼就可以畫出小豬佩奇了:

// ASCII Peppa Pig by Milo Yip#include <math.h>#include <stdio.h>#include <stdlib.h>#define T doubleT 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))]);}

2倍:

4 倍:

8 倍:

今天也是精緻的豬豬女孩~

推薦閱讀:

我最喜歡小豬佩奇了。
突然之間小豬佩奇變成了「社會人」的標準配備?
《小豬佩奇》究竟是怎麼樣的社會人?
小豬佩奇×YSL、Chanel!社會人專用
小豬佩奇 x 國貨 | 掌聲送給社會人!

TAG:小豬佩奇 | C編程語言 | 計算機圖形學 |