% The CMUMC (CMU Math Club) logo, as proposed 7 Dec 2004.
% METAFONT source code by Arthur O'Dwyer.
% Released to the public domain, 10 Dec 2004.

font_size 10pt#;
df := 0.7807764064;
cf := 1.3721867191;  % 1 + df**4 + df**8 + df**12 + ...
u# := 4pt#;

% When 'simple_shapes' is 'true', use a circular pen and simple strokes.
% When it's false, try to make the text as "normal-looking" as possible.
boolean simple_shapes;

mode_setup;
define_pixels(u);
pair ofs;

%
% This subroutine is used by the main loop to keep drawing strokes
% until we get to the point where the stroke's boundary is no longer
% wide enough to count as a proper path.
%
boolean halt;
def throwing_penstroke text t =
  forsuffixes e = l,r: path_.e:=t; endfor
  if cycle path_.l: cyclestroke_
  else:
    path path__; path__ := path_.l -- reverse path_.r -- cycle;
    if turningnumber path__ = 0: halt:=true else: fill path__ fi;
  fi
enddef;


%
% The following two subroutines draw the letters "C" and "M", given
% the suffixes of four points marking the UL, UR, LL, and LR corners
% of the bounding rectangle, respectively.
%
def draw_letter_C(suffix $, $$, @, @@)(expr pc, t) =
  for i=0 upto 5: x[i].c := whatever; y[i].c := whatever;
                  x[i].c.l := whatever; y[i].c.l := whatever;
                  x[i].c.r := whatever; y[i].c.r := whatever;
  endfor;
  z0c = 0.5[z$,z@@];
  z1c = 0.8[z0c,z$$];
  z2c = 0.8[z0c,0.5[z$,z@]];
  z3c = 0.8[z0c,z@@];
  z4c = 0.5[z1c,0.8[z0c,z$]];
  z5c = 0.5[z3c,0.8[z0c,z@]];
  if (simple_shapes):
    y5c := df[y@, y5c];
    y3c := df[y@, y3c];
    pickup pencircle scaled pc;
    draw ((z1c..z4c{left}..z2c{down}..z5c{right}..z3c) transformed t)
      shifted ofs;
  else:
    y1c := y1c - 0.5pc;
    y3c := y3c + 0.2pc;
    penpos1c(2.2pc,90);
    penpos4c(1.7pc,90);
    penpos2c(1.7pc,180);
    penpos5c(1.7pc,270);
    penpos3c(1.7pc,270);
    pickup penrazor;
    throwing_penstroke ((z1c.e..z4c.e{left}..z2c.e{down}..z5c.e{right}..z3c.e)
      transformed t) shifted ofs;
  fi
enddef;

def draw_letter_M(suffix $, $$, @, @@)(expr pc, t) =
  for i=0 upto 5: x[i].m := whatever; y[i].m := whatever;
                  x[i].m.l := whatever; y[i].m.l := whatever;
                  x[i].m.r := whatever; y[i].m.r := whatever;
  endfor;
  z0m = 0.5[z$,z@@];
  z1m = 0.8[z0m,z@];
  z2m = 0.8[z0m,z$];
  z4m = 0.8[z0m,z$$];
  z5m = 0.8[z0m,z@@];
  if (simple_shapes):
    z3m = z0m;
    pickup pencircle scaled pc;
    draw ((z1m--z2m--z3m--z4m--z5m) transformed t) shifted ofs;
  else:
    z3m = 0.7[z0m, 0.5[z@,z@@]];
    y1m := y1m - 0.6pc; y5m := y1m;
    penpos1m(2pc,0); penpos2m(1.6pc,0);
    penpos3m(if (pc > u/20): 1.5pc else: 1pc fi,0);
    penpos4m(1.6pc,0); penpos5m(2pc,0);
    throwing_penstroke ((z1m.e--z2m.e) transformed t) shifted ofs;
    throwing_penstroke ((z2m.e--z3m.e) transformed t) shifted ofs;
    throwing_penstroke ((z3m.e--z4m.e) transformed t) shifted ofs;
    throwing_penstroke ((z4m.e--z5m.e) transformed t) shifted ofs;
  fi
enddef;


def create_logo(expr code, name) =
  beginchar(code,(2df+1)*u#,(2df+1)*df*u#,0); name;
    transform t;
    t := identity; pc := 0.2pt;
    ofs := u*(cf,cf*df);
    halt:=false;
    for i=1 upto 42: exitif halt;
      % Mark the extreme corners of the line of text. Note that y0tl > y0tr.
      z0bl = u*(-cf,-cf*df);
      z0br = u*(1+2df-cf,-cf*df);
      z0tl = z0bl+u*up;
      z0tr = z0br+u*df*up;
      % Mark the five interletter corners, top to bottom, left to right.
      z1 = z0tl+u*right; z2 = z0tr+2u*df*left; z3 = z0bl+u*right;
      z4 = z0tr+u*df*left; z5 = z0br+u*df*left;

      % Draw the letter "C", which is a sideways "U".
      draw_letter_C(0tl,1,0bl,3, pc, t);
      % Draw the letter "M" next to it.
      draw_letter_M(2,4,3,5, pc, t);

      % Update the transform.
      transform nt;
      z0tl transformed nt = z5 transformed t;
      z0bl transformed nt = z0br transformed t;
      z1 transformed nt = z4 transformed t;
      t := nt;
      if (simple_shapes):
        pc := df*pc;
      else:
        pc := (pc/0.2pt)[
                  1/4length((z0tl transformed nt) - (z0bl transformed nt)),
                  .7[df,1]*pc
                 ];
      fi
    endfor;

    % Erase the initial "C" (left-hand bottom of the logo).
    z10a = origin;
    z10b = origin + u*right + (2df+1)*df*u*up;
    erase fill z10a--(x10a,y10b)--z10b--(x10b,y10a)--cycle;

    % Now replace it with a stacked "M" "C".

    z101 = origin + (2df+1)*df*u*up;
    z102 = origin + u*right + (2df+1)*df*u*up;
    z103 = origin + 1/2(2df+1)*df*u*up;
    z104 = origin + u*right + 1/2(2df+1)*df*u*up;
    z105 = origin;
    z106 = origin + u*right;

    ofs:=0.2pt*up;
    draw_letter_M(101,102,103,104, 0.4pt, identity);
    ofs:=0.1pt*up;
    draw_letter_C(103,104,105,106, 0.4pt, identity);

  endchar;
enddef;

simple_shapes:=true;
  create_logo("s", "simple CMUMC logo");
simple_shapes:=false;
  create_logo("n", "normal CMUMC logo");