Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #725

Snowflakes and my library

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.postscript
Subject Snowflakes and my library
Date 2012-06-10 01:05 +0200
Organization Decebal Computing
Message-ID <87wr3gqe6x.fsf@Compaq.site> (permalink)

Show all headers | View raw


I made a nice snowflake postscript program. No rocket science, but I
thought it nice to share it. Also a copy of the library I made until
now.

snowflakes:
    %!PS-Adobe-3.0 EPSF-3.0
    %%BoundingBox: 0 0 600 600

    /pageBottom  0 def
    /pageLeft    0 def
    /pageRight 600 def
    /pageTop   600 def

    #standard

    %% Functions
    /changeColor {
      currentrgbcolor 3 1 roll .2 add setrgbcolor
    } def

    % Do a snowflake edge to recursion 'depth' moving distance 'length'
    /snowedge {
      depth 0 eq
      {
        length 0 rlineto
      }
      {
        /depth  depth  1 sub def
        /length length 3 div def
        snowedge
        60 rotate
        snowedge
        -120 rotate
        snowedge
        60 rotate
        snowedge
        /length length 3 mul def
        /depth  depth  1 add def
      } ifelse
    } def


    % Draw a snowflake around (x0, y0) of size 'length', and recursion 'depth' and fill it.
    /snowflake {
      newpath
      length 2 div neg length 3 sqrt 2 mul div
      2 copy
      moveto
      snowedge
      length 2 div     length 3 sqrt 2 mul div
      2 copy
      moveto
      -120 rotate
      snowedge
      120 rotate
      0                length 3 sqrt div neg
      2 copy
      moveto
      120 rotate
      snowedge
      -120 rotate
      moveto
      lineto
      lineto
      closepath
      fill
    } def

    /authorFont     10 def
    /backgroundGray .6 def
    /decDept        false def
    /depth          5 def
    /length         500 def
    /recur          8 def
    /usedFont       /Bookman findfont def

    backgroundGray grayBackground
    pageRight 2 div pageTop 2 div translate
    0.2 setlinewidth
    0 0 .6 setrgbcolor

    snowflake
    recur {
      changeColor
      30 rotate
      /length length 3 sqrt div def
      decDept {
        /depth depth 1 sub def
      } if
      /decDept decDept not def
      snowflake
    } repeat

    showpage

library:
    /colors <<
      /colorBlack  { 0     0     0 }
      /colorBlue   { 0     0     1 }
      /colorGreen  { 0     .5    0 }
      /colorIndigo { .3125 0     .5 }
      /colorOrange { 1     .5625 0 }
      /colorRed    { 1     0     0 }
      /colorViolet { .9375 .5    .9375 }
      /colorWhite  { 1     1     1 }
      /colorYellow {  1    1     0 }
    >> def
    colors begin

    /grayBackground { % gray
      newpath
      gsave
        setgray
        newpath
        pageLeft pageBottom pageRight pageTop rectangle
        fill
        .4 setgray
        usedFont
        authorFont scalefont
        setfont
        ((c) Cecil Westerhof - Decebal Computing) 550 5 rightString
      grestore
    } def

    /rectangle { % left bottom right top
      4 dict begin
        /top    exch def
        /right  exch def
        /bottom exch def
        /left   exch def

        left  bottom moveto
        right bottom lineto
        right top    lineto
        left  top    lineto
        left  bottom lineto
        closepath
      end
    } def

    /rightString { % msg x y
      3 dict begin
        /y   exch def
        /x   exch def
        /msg exch def

        x y moveto
        msg stringwidth pop neg
        0
        rmoveto
        msg show
      end
    } def

    /textheight {
      gsave
        {
          newpath
          100 100 moveto
          (HÍpg) true charpath pathbbox      % gets text path bounding box (LLx LLy URx URy)
          exch pop 3 -1 roll pop             % keeps LLy and URy
          exch sub                           % URy - LLy
        }
        stopped                                % did the last block fail?
        {
          pop pop                            % get rid of "stopped" junk
          currentfont /FontMatrix get 3 get  % gets alternative text height
        }
        if
      grestore
    } def

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.postscript | Previous | Next | Find similar


Thread

Snowflakes and my library Cecil Westerhof <Cecil@decebal.nl> - 2012-06-10 01:05 +0200

csiph-web