r/programminghorror • • Sep 29 '18

Postscript Animation written in PostScript

During college, I had a task to do something unusual with Postscript (page description language).
I decided to make an animation. I created abomination.
Animation (screen capture of Ghostscript https://youtu.be/ILR704g4b20)

Bear in mind, this language uses reverse Polish notation so it may look... unusual.

It was a great lesson about using wrong tool for the job.

%!PS-Adobe-3.0 
/delay{%delay execution by provided number of miliseconds
	{7500 {1 2 3 4 5 6 7 8 9 mul div mul div mul div mul div pop} repeat} repeat } bind def
/point %take coordinates of 2 points, project them on (0,0,300,300) 
{
6 dict begin
/y1 exch def
/x1 exch def
/y exch def
/x exch def
x x1 eq {
	y1 y ge {x 300} {x 0} ifelse
} if
x x1 ne {
	/a y1 y sub x1 x sub div def
	/b y a x mul sub def
	y1 y ge x1 x ge and{
		300 a mul b add 300 ge {300 b sub a div 300} {300 a 300 mul b add } ifelse
	}if
	y1 y lt x1 x ge and{
		300 a mul b add 0 le {0 b sub a div 0} {300 a 300 mul b add} ifelse
	}if
	y1 y ge x1 x lt and{
		b 300 ge {300 b sub a div 300} {0 b} ifelse
	}if
	y1 y lt x1 x lt and{
		b 0 le {0 b sub a div 0} {0 b} ifelse
	}if
} if
end
} bind def
/cart_to_pol { %carthesian to polar coordinates
3 dict begin
/y exch def
/x exch def
/r x x mul y y mul add sqrt def
x 0 eq { y 0 ge {r 90} {r 270} ifelse } 
{x 0 gt {y 0 ge {r y x atan} {r y x atan} ifelse } {r y x atan  } ifelse }
ifelse
end
} bind def 
/pol_to_cart { %polar to carthesian coordinates
2 dict begin
/fi exch def
/r exch def
r fi cos mul r fi sin mul
end
} bind def
/sort { %tough shit here
5 dict begin
/tab exch def
/len tab length 2 div 1 sub cvi def
/tmp 0 def
len {
len {
	 tab tmp 2 mul 1 add get tab tmp 2 mul 3 add get gt {
	 tab tmp 2 mul get
	 tab tmp 2 mul 1 add get
	 tab tmp 2 mul 2 add get
	 tab tmp 2 mul 3 add get
	 tab tmp 2 mul 1 add 3 -1 roll put
	 tab tmp 2 mul 3 -1 roll put
	 tab tmp 2 mul 3 add 3 -1 roll put
	 tab tmp 2 mul 2 add 3 -1 roll put
	 } if
	/tmp tmp 1 add def
} repeat
/tmp 0 def
} repeat
tab
end
}bind def
/shadow {% create shadow, treating moving point as light source
20 dict begin
/y exch def
/x exch def
/tab exch def
/len tab length 2 div cvi def
/tab2 len 2 mul array def
/res len 4 mul array def
/tmp 0 def
/xsr 0 def
/ysr 0 def
/tmp 0 def
len {
	x y
	tab tmp 2 mul get 
	tab tmp 2 mul 1 add get
	point
	tab2 tmp 2 mul 1 add 3 -1 roll put 
	tab2 tmp 2 mul 3 -1 roll put
	/tmp tmp 1 add def
} repeat
res 0 tab putinterval
res len 2 mul tab2 putinterval
/tmp 0 def
len 2 mul{
	/xsr xsr res tmp 2 mul get add def
	/ysr ysr res tmp 2 mul 1 add get add def
	/tmp tmp 1 add def
} repeat
/xsr xsr len 2 mul div def
/ysr ysr len 2 mul div def
/tmp 0 def
len 2 mul{
	res tmp 2 mul get xsr sub
	res tmp 2 mul 1 add get ysr sub
	cart_to_pol
	res tmp 2 mul 1 add 3 -1 roll put
	res tmp 2 mul 3 -1 roll put
	/tmp tmp 1 add def
} repeat
/res res sort def
/tmp 0 def
len 2 mul{
	res tmp 2 mul get 
	res tmp 2 mul 1 add get 
	pol_to_cart
	ysr add res tmp 2 mul 1 add 3 -1 roll put
	xsr add res tmp 2 mul 3 -1 roll put
	/tmp tmp 1 add def
} repeat
0 setgray
res aload pop
2 copy moveto
2 copy
2 copy
/ay exch def
/ax exch def
/by exch def
/bx exch def
len 4 mul 2 roll
len 2 mul{
	/ay exch def
	/ax exch def
	ax 299 ge by 1 le and { 300 0 lineto} if
	ax 299 ge by 299 ge and {300 300 lineto} if
	ax 1 le by 299 ge and { 0 300 lineto} if
	ax 1 le by 1 le and { 0 0 lineto } if
	ay 299 ge bx 1 le and {0 300 lineto } if
	ay 299 ge bx 299 ge and {300 300 lineto} if
	ay 1 le bx 299 ge and { 300 0 lineto } if
	ay 1 le bx 1 le and {0 0 lineto } if
	/bx ax def
	/by ay def
	ax ay lineto
} repeat
closepath
fill
/tmp 0 def
res 0 get res 1 get moveto
len {
	/ay res tmp 2 mul 1 add get def
	/ax res tmp 2 mul get def
	ax 299 ge by 1 le and { 300 0 lineto} if
	ax 299 ge by 299 ge and {300 300 lineto} if
	ax 1 le by 299 ge and { 0 300 lineto} if
	ax 1 le by 1 le and { 0 0 lineto } if
	ay 299 ge bx 1 le and {0 300 lineto } if
	ay 299 ge bx 299 ge and {300 300 lineto } if
	ay 1 le bx 299 ge and { 300 0 lineto } if
	ay 1 le bx 1 le and {0 0 lineto } if
	/bx ax def
	/by ay def
	ax ay lineto
	/tmp tmp 2 add def
} repeat
closepath
fill
/tmp 1 def
res 2 get res 3 get moveto
len {
	/ay res tmp 2 mul 1 add get def
	/ax res tmp 2 mul get def
	ax 299 ge by 1 le and { 300 0 lineto} if
	ax 299 ge by 299 ge and {300 300 lineto} if
	ax 1 le by 299 ge and { 0 300 lineto} if
	ax 1 le by 1 le and { 0 0 lineto } if
	ay 299 ge bx 1 le and {0 300 lineto } if
	ay 299 ge bx 299 ge and {300 300 lineto } if
	ay 1 le bx 299 ge and { 300 0 lineto } if
	ay 1 le bx 1 le and {0 0 lineto } if
	/bx ax def
	/by ay def
	ax ay lineto
	/tmp tmp 2 add def
} repeat
closepath
fill
flushpage
end
} bind def

/fun {
1 setgray 
0 0 moveto
302 0 rlineto
0 302 rlineto
-302 0 rlineto
closepath
fill
2 dict begin
/y exch def
/x exch def
0 setgray 
0 0 moveto
300 0 rlineto
0 300 rlineto
-300 0 rlineto
closepath
stroke
x y 1 0 360 arc closepath stroke 
[100 100 150 100 100 150] x y shadow
[200 200 250 200 250 250 200 250] x y shadow
1 0 0 setrgbcolor
100 100 moveto
150 100 lineto
100 150 lineto
closepath
fill
0 1 0 setrgbcolor
200 200 moveto
250 200 lineto
250 250 lineto
200 250 lineto
closepath
fill
flushpage
50 delay
end } bind def
1000 delay
2 dict begin
/fi 0 def
/r 50 def
{
r fi pol_to_cart exch 130 add exch 130 add fun
/fi fi 1 sub def
fi 360 ge { /fi 0 def} if
fi 0 lt { /fi 360 def} if
} loop
end
64 Upvotes

6 comments sorted by

View all comments

7

u/TotesMessenger Sep 29 '18

I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:

 If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)