Dry goods! Draw a piggy Peppa in C language (source code attached), no need to poke!

Share how to use  C language to draw a piggy page using signed distance field (SDF) to represent a circle: file:///C:\Users\Administrator.WIN-

STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps19.png continues to use this method to represent shapes, but this time we want to use ASCII characters |/=\ to draw the outer frame of the shape and fill the inside, similar to this: =====

//.....\\

||.......||

\\.....//

   ===== The gradient of SDF represents the direction in which SDF changes the most, and this direction can be used to determine which character to use. file:///C:\Users\Administrator.WIN-

STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps20.png We obtain the gradient approximation of SDF by difference, and then use atan2() to find the angle of the gradient: file:///C:\Users\Administrator.WIN-

STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps21.jpg is simply implemented in C language. In the canvas, draw a circle with a radius of 0.8 and an outer frame of 0.1: file:///C:\Users\Administrator. WIN-


\AppData\Local\Temp\ksohtml9656\wps22.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('\n'))

        for (T x = -1; x < 1; x += 0.025)

            putchar(outline(x, y));} The code can be moved left and right!  file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps23.png Then, we can draw multiple circles,

Rotate and scale them appropriately, combine them with structural solid geometry, then you can draw Peppa Pig with 19 lines of code: the code can move left and right!

▼// 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,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))]);}file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps24.png 2倍:

file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps25.png 4倍:

file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps26.png 8倍:

file:///C:\Users\Administrator.WIN-STED6B9V5UI\AppData\Local\Temp\ksohtml9656\wps27.png 

how about it? Is it meeting now? You can also try to make this Peppa move

In addition, if you want to better improve your programming ability, learn C language and C++ programming! Overtaking in a curve, one step faster! I may be able to help you here~

UP has uploaded some video tutorials on learning C/C++ programming on the homepage. Those who are interested or are learning must go and take a look! It will be helpful to you~

Sharing (source code, actual project video, project notes, basic introductory tutorial)

Welcome partners who change careers and learn programming, use more information to learn and grow faster than thinking about it yourself!

Programming learning:

Programming learning:

 

Guess you like

Origin blog.csdn.net/weixin_45713725/article/details/115006684