Recursive C curve drawing

Bill Gosper's "C" curve
discussed as Item #135 in HAKMEM,
MIT Artificial Intelligence Laboratory
Memo #239, 1972

Java source

Postscript source version

Java Applet Source Code
/* java implementation of Recursive C-Curve Drawing */
/* Implemented by Keith Beidelman -- keith@kgbsw.com */
/* Version 1.1 16-Jan-2001 */

import java.applet.*;
import java.math.*;
import java.awt.*;

public class cc extends Applet
{
double length;
double angle;
double x;
double y;
double width;
double height;

final double pi = 3.1415927;
final double sqrt2 = Math.sqrt (2.0);

  public void vector (Graphics g, double angle, double length)
  {
  double nx, ny;

    nx = x + length * Math.sin (angle);
    ny = y + length * Math.cos (angle);
    g.drawLine ((int) x, (int) y, (int) nx, (int) ny);
    x = nx;
    y = ny;
  }

  public void cc (Graphics g, double angle, double length)
  {
      if (length <= 1)
        vector (g, angle, length);
      else
        {
        cc (g, angle - pi / 4.0, length / sqrt2);
        cc (g, angle + pi / 4.0, length / sqrt2);
        }
  }

  public void init ()
  {
    width = size().width;
    height = size().height;
  }

  public void paint (Graphics g)
  {
    x = 3 * width / 4;
    y = 10 * width / 24;
    cc (g, 0, 2 * width / 3);
  }
};
          

PostScript Source Code
%!PS-Adobe-2.0

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Bill Gosper's "C" curve                                                    %
% discussed as Item #135 in HAKMEM, MIT Artificial                           %
% Intelligence Laboratory Memo #239, 1972                                    %
%                                                                            %
% See http://www.inwap.com/pdp10/hbaker/hakmem/flows.html#item135            %
% PostScript implementation by Keith Beidelman                 %
% Beidelman Software http://www.kgbsw.com/                                   %
%                                                                            %
% Used to test postsctipt machine for stack depth and as                     %
% a recursive experiment in PostScript.                                      %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/sq2 2 sqrt def               % square root of 2
/l   5 72 mul def             % length of vector
/a   0 def                    % angle of vector
/x   0 def                    % current cordinate x
/y   0 def                    % current cordinate y

/rv {                         % draw a vector of a length at a angle
  /l exch def /a exch def     % save length and angle of vector
  /x a sin l mul x add def    % compute vector's x cordinate
  /y a cos l mul y add def    % compute vector's y cordinate
  x y lineto                  % draw a line to the new destination (x,y)
  } bind def

/cc {                         % c curve generator
  dup 2 ge                    % until line length < 2 points
    { /l exch sq2 div def     % length <= length / sqrt(2)
      /a exch def             % save angle
      a 45 add l              % recursively replace line with 2 lines
      a 45 sub l              % at +/- 45 degrees
      cc cc }                 % recurse for each of 2 new line segments
    { rv }                    % once line <= 2 points, draw vector
  ifelse                      % end of recurse condition
  } bind def                  % end of c curve definition

72 8.5 1.5 div mul            % compute c curve x org - 72 points / inch
72 11 4 div mul               % compute c curve y org - 72 points / inch
translate 0 0 moveto          % make cordinate system at org (1.5,4) inches
a l cc stroke showpage        % draw a c curve
          


Keith Beidelman - To Family Home Page - to www.kgbsw.com