Recursion in IDL [message #881] |
Fri, 02 April 1993 06:57  |
sterner
Messages: 106 Registered: February 1991
|
Senior Member |
|
|
One of the least used features of IDL may be recursion. But it's
there and works very well. I suspect there are some rather impressive
graphics routines that could be written using recursion. Below is
a very basic routine to show how recursion may be used to easily make
elaborately detailed plots. It is intended to show the needed parts
of a recursive graphics routine, not to be an example of a spectacular
plot. Try it in a screen window.
Ray Sterner sterner@tesla.jhuapl.edu
Johns Hopkins University North latitude 39.16 degrees.
Applied Physics Laboratory West longitude 76.90 degrees.
Laurel, MD 20723-6099
;----------------------------------------------------------- ---------
; rec1.pro = recursion example 1.
; R. Sterner, 2 Apr, 1993
; Draw branches from center of a square out to corners, then
; recursively do the same for smaller squares centered at
; each corner.
; Syntax: rec1, x, y, s
; x,y = device coordinates of ceneter of a square,
; s = half size of square.
; Example call for a default screen window: rec1, 320, 256, 100
; Works in device coordinates so needs modified for PostScript.
;----------------------------------------------------------- ----------
pro rec1, x, y, s
;------- Recursion exit condition ---------
if s lt 1 then return ; Too small to continue.
;------- Find corners of square ------------
x1 = x+s & y1 = y+s ; Corner 1.
x2 = x-s & y2 = y+s ; Corner 2.
x3 = x-s & y3 = y-s ; Corner 3.
x4 = x+s & y4 = y-s ; Corner 4.
;------- Plot branches ---------------------
plots,/dev,[x,x1],[y,y1] ; Draw branch 1.
plots,/dev,[x,x2],[y,y2] ; Draw branch 2.
plots,/dev,[x,x3],[y,y3] ; Draw branch 3.
plots,/dev,[x,x4],[y,y4] ; Draw branch 4.
;--- Recursively work on each corner -------
rec1, x1, y1, .45*s
rec1, x2, y2, .45*s
rec1, x3, y3, .45*s
rec1, x4, y4, .45*s
return
end
|
|
|