1;;; line-nova.scm for gimp-1.1 -*-scheme-*-
2;;; Time-stamp: <1998/11/25 13:26:44 narazaki@gimp.org>
3;;; Author Shuji Narazaki <narazaki@gimp.org>
4;;; Version 0.7
5
6(define (script-fu-line-nova img drw num-of-lines corn-deg offset variation)
7  (let* (
8        (*points* (cons-array (* 3 2) 'double))
9        (modulo fmod)                        ; in R4RS way
10        (pi/2 (/ *pi* 2))
11        (pi/4 (/ *pi* 4))
12        (pi3/4 (* 3 pi/4))
13        (pi5/4 (* 5 pi/4))
14        (pi3/2 (* 3 pi/2))
15        (pi7/4 (* 7 pi/4))
16        (2pi (* 2 *pi*))
17        (rad/deg (/ 2pi 360))
18        (variation/2 (/ variation 2))
19        (drw-width (car (gimp-drawable-width drw)))
20        (drw-height (car (gimp-drawable-height drw)))
21        (drw-offsets (gimp-drawable-offsets drw))
22        (old-selection FALSE)
23        (radius (max drw-height drw-width))
24        (index 0)
25        (dir-deg/line (/ 360 num-of-lines))
26        (fg-color (car (gimp-context-get-foreground)))
27        )
28    (gimp-context-push)
29    (gimp-context-set-defaults)
30    (gimp-context-set-foreground fg-color)
31
32    (define (draw-vector beg-x beg-y direction)
33
34      (define (set-point! index x y)
35            (aset *points* (* 2 index) x)
36            (aset *points* (+ (* 2 index) 1) y)
37      )
38      (define (deg->rad rad)
39            (* (modulo rad 360) rad/deg)
40      )
41      (define (set-marginal-point beg-x beg-y direction)
42        (let (
43             (dir1 (deg->rad (+ direction corn-deg)))
44             (dir2 (deg->rad (- direction corn-deg)))
45             )
46
47          (define (aux dir index)
48                   (set-point! index
49                               (+ beg-x (* (cos dir) radius))
50                               (+ beg-y (* (sin dir) radius)))
51          )
52
53          (aux dir1 1)
54          (aux dir2 2)
55        )
56      )
57
58      (let (
59           (dir0 (deg->rad direction))
60           (off (+ offset (- (modulo (rand) variation) variation/2)))
61           )
62
63        (set-point! 0
64                    (+ beg-x (* off (cos dir0)))
65                    (+ beg-y (* off (sin dir0)))
66        )
67        (set-marginal-point beg-x beg-y direction)
68        (gimp-image-select-polygon img CHANNEL-OP-ADD 6 *points*)
69      )
70    )
71
72    (gimp-image-undo-group-start img)
73
74    (set! old-selection
75      (if (eq? (car (gimp-selection-is-empty img)) TRUE)
76         #f
77         (car (gimp-selection-save img))
78      )
79    )
80
81    (gimp-selection-none img)
82    (srand (realtime))
83    (while (< index num-of-lines)
84      (draw-vector (+ (nth 0 drw-offsets) (/ drw-width 2))
85                   (+ (nth 1 drw-offsets) (/ drw-height 2))
86                   (* index dir-deg/line)
87      )
88      (set! index (+ index 1))
89    )
90    (gimp-drawable-edit-fill drw FILL-FOREGROUND)
91
92    (if old-selection
93      (begin
94        (gimp-image-select-item img CHANNEL-OP-REPLACE old-selection)
95        ;; (gimp-image-set-active-layer img drw)
96        ;; delete extra channel by Sven Neumann <neumanns@uni-duesseldorf.de>
97        (gimp-image-remove-channel img old-selection)
98      )
99    )
100
101    (gimp-image-undo-group-end img)
102    (gimp-displays-flush)
103    (gimp-context-pop)
104  )
105)
106
107(script-fu-register "script-fu-line-nova"
108  _"Line _Nova..."
109  _"Fill a layer with rays emanating outward from its center using the foreground color"
110  "Shuji Narazaki <narazaki@gimp.org>"
111  "Shuji Narazaki"
112  "1997,1998"
113  "*"
114  SF-IMAGE       "Image"               0
115  SF-DRAWABLE    "Drawable"            0
116  SF-ADJUSTMENT _"Number of lines"     '(200 40 1000 1 1 0 1)
117  SF-ADJUSTMENT _"Sharpness (degrees)" '(1.0 0.0 10.0 0.1 1 1 1)
118  SF-ADJUSTMENT _"Offset radius"       '(100 0 2000 1 1 0 1)
119  SF-ADJUSTMENT _"Randomness"          '(30 1 2000 1 1 0 1)
120)
121
122(script-fu-menu-register "script-fu-line-nova"
123                         "<Image>/Filters/Render")
124