Skip to content

Instantly share code, notes, and snippets.

@a1k0n
Created October 22, 2015 01:02

Revisions

  1. a1k0n created this gist Oct 22, 2015.
    75 changes: 75 additions & 0 deletions ylogo.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    #include <math.h>
    #include <stdio.h>
    #include <unistd.h>

    // Each character encodes an angle of a plane we are checking
    const char plane_angles[] = "O:85!fI,wfO8!yZfO8!f*hXK3&fO;:O;#hP;\"i[";
    // and these encode an offset from the origin s.t. (x, y) dot (cos(a), sin(a)) < offset
    const char plane_offsets[] = "<[\\]O=IKNAL;KNRbF8EbGEROQ@BSXXtG!#t3!^";

    // this table encodes the offsets within the above tables of each polygon
    const char polygon_offsets[] = "8<AFJPTX";

    int main() {
    float scale = 0, velocity = 0;

    for(int frame = 0; frame < 40; frame++) {

    if (frame != 0) {
    // after the first frame, bring the cursor up 25 lines
    puts("\x1b[25A");
    }

    // scale is controlled by a simple underdamped spring system -- velocity is
    // proportional to displacement, with a damping factor (velocity/4)
    scale += velocity;
    velocity += (1 - scale) / 10 - velocity / 4;

    // j loops over rows of the image
    for(int j = 0; j < 72; j += 3) {
    // and i loops over columns
    for(int i = 0; i < 73; i++) {
    float x = scale * (i - 27); // x coordinate in object, controlled by scale and i

    // now we make three vertical samples of our object and put them in the
    // low bits of our accumulator c
    int c = 0;
    for(int n = 2; n >= 0; n--) {
    float y = scale * (j+n-36); // y coordinate in object
    int plane_idx = 0;
    int inside_ellipse = (136*x*x+84*y*y<92033);
    c ^= inside_ellipse << n;

    // this single loop runs over all polygons and all planes within each
    // polygon. it really should be two loops, but this enables us to
    // break out early without a goto, and this hews closer to the
    // original obfuscated code.
    for (int polygon = 0; polygon < 8;) {
    // plane_end_idx is the last plane in our polygon
    int plane_end_idx = polygon_offsets[polygon] - 50;
    float angle = (plane_angles[plane_idx]-79)/14.64;
    float plane_offset = plane_offsets[plane_idx++] / 1.16 - 68;

    if (plane_offset > x*cos(angle) + y*sin(angle)) {
    // we are outside the polygon
    // advance to end of polygon and compute end of the next one
    plane_idx = plane_end_idx;
    polygon++;
    } else if (plane_idx == plane_end_idx) {
    // we are inside all planes of our polygon, so toggle our pixel
    // and early-out
    c ^= 1<<n;
    break;
    }
    }
    }

    // finally, c indexes into a predefined list of characters based on the
    // presence of the object in the three positions
    putchar(" ''\".$u$"[c]);
    }
    putchar('\n');
    }
    usleep(50000);
    }
    }