Today, we're going to draw a little piggy in C. Page---society, society ....
Before we draw Piglet Paige, we draw a circle using a long, signed distance (signed distance field,sdf).
Use this method to represent the shape, but this time we use the Ascⅱ character \/=\ to draw the shape's outer frame and fill the interior, similar to this:
=====
//.....\\
||.....||
\\....//
=====
The SDF gradient (gradient) represents the most varied direction of the SDF change, which is used in this direction to determine which character to use.
We use the differential to find the gradient approximation of the SDF, and then using atan2 () to find the angle of the gradient:
#include <math.h>
#include <stdio.h>
#define T Double
T 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 i =-1; i < 1; i + = 0.05, Putchar (' \ n '))
{
for (T j =-1; j < 1; j + = 0.025)
{
Putchar (Outline (j, i));
}
}
GetChar ();
return 0;
}
We can then draw the Piglet page by drawing multiple circles, rotating and zooming them appropriately, and combining them with structural solid geometry:
#include <math.h> #include <stdio.h> #include <stdlib.h> #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 (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, a)): Fmax (O (x, y, FA, 1), Fmax (O (x, y, EY, one), Fmax (O (x, Y, EA, 1), Fmax (O (x, y, MO, 1 ), Fmax (s (x, Y, FR, a), 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)]);
}
}
GetChar ();
return 0;
}
Twice times:
Four times times:
Reference:
Know Milo Yip
How to use C language gracefully to draw a little piggy page