shocked! C language can draw pig Paige? [Social] I Pei Qige with source code!

My brother Paige society then fire, then the next we are going to share how to use  C language painting pig Page

Use signed distance fields (signed distance field, SDF) represents a circular:

Adopt this method represents the shape, but this time we want to use ASCII characters | / = \ draw the shape of the frame, and fills the inside, like this:

SDF gradient (gradient) on behalf of SDF biggest change in direction, this direction can be used to decide which of the characters.

We ask SDF gradient approximation by the differential, and the gradient angle determined by the atan2 ():

Language simple implementation written in C, a draw radius canvas 0.8 and 0.1 with a round frame in the width by:

       Then, we can draw a plurality of circular, they properly rotate and zoom, combined with constructive solid geometry ratio thereof, then with 19 lines of code to draw the pig Page:

Code!

// 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('\n'))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 times:

4 times:

8 times:

how about it? This time will be yet? You can also try to let this Pecs move up yo!

For those who love programming, there are a group of small partners together to learn is very important, I have a zero-based introductory programming learning exchange clubs , as well as learning video files, are welcome beginners and advanced in little friends!

Published 520 original articles · won praise 132 · views 80000 +

Guess you like

Origin blog.csdn.net/HUYA69/article/details/105197812