Old Glory, in Postscript

August 19, 2010 | My Projects, Toys and Gadgets | By: Mark VandeWettering

I needed a graphic of a flag that I could scale to whatever size I needed. About 10 minutes of Postscript hacking with the specifications in Wikipedia yielded the following results:

Sadly, the syntax highlighter that I have here doesn’t know about PostScript, so I’ll just have to add it here.

%!PS
%
%
% PostScript program for generating an image of the US Flag
% Generated by implementing the specifications which are listed on
% the wikipedia page:
%
% http://en.wikipedia.org/wiki/Flag_of_the_United_States#Specifications
%
% by Mark VandeWettering

/inch {72 mul} def

/Hoist 1.0 def
/Fly 1.9 def
/UnionHoist Hoist 7 mul 13 div def
/UnionFly Fly 2 mul 5 div def
/WidthStripe Hoist 13 div def
/F 0.054 def
/G 0.063 def

/StarDiameter 0.0616 def
/StarRadius StarDiameter 2 div def

% When a regular pentagon is inscribed in a circle with radius R, its
% edge length t is given by the expression:
% t = 2 R sin(pi / 5)
% PostScript does sin in degrees, so we need 180 instead...

/StarSide StarDiameter 36 sin mul def
/StarDiagonal StarSide 5 sqrt 1 add 2 div mul def

/white {1 1 1 setrgbcolor} def 
/red {0.698 .132 .203 setrgbcolor} def
/blue {0.234 0.233 0.430 setrgbcolor} def

% draw a star at the cursor position...

/stardiagonal {
	StarDiagonal 0 lineto
	currentpoint translate 
	-144 rotate
} def

/star {
	gsave
	exch StarDiagonal 2 div sub exch 
	StarRadius 18 sin mul add
	moveto currentpoint translate
	4 { stardiagonal } repeat closepath
	fill
	grestore
} def

% draw the border...
/Flag {
    gsave
    0.001 setlinewidth
    Fly 2 div neg Hoist 2 div neg translate
    0 0 Fly Hoist rectstroke
    % paint in the bg, just in case
    white
    0 0 Fly Hoist rectfill
    % paint in the red stripes...
    red
    1 2 13 { 
	1 sub
	WidthStripe mul 0 exch Fly WidthStripe rectfill
    } for
    % draw in the union...
    blue
    0 Hoist UnionHoist sub UnionFly UnionHoist rectfill

    % now, the stars, easiest to do in two passes.
    gsave
    0 Hoist UnionHoist sub translate
    white
    1 2 9 {
	/y exch def
	1 2 11 {
	    /x exch def
	    x G mul y F mul star
	} for
    } for 
    2 2 8 {
	/y exch def
	2 2 10 {
	    /x exch def
	    x G mul y F mul star
	} for
    } for 
    grestore

    grestore

} def

8.5 inch 2 div 11.0 inch 2 div translate 4 inch dup scale

Flag

showpage
quit

Enjoy.

Addendum:

Oh, incidently, you can use The Gimp to convert this from PostScript to a normal graphics format like the PNG file above. It will even do some antialiasing to make the jaggies look better. Very nice.