Skip to content

Instantly share code, notes, and snippets.

@hyperlogic
Created March 24, 2009 04:20
Show Gist options
  • Save hyperlogic/83936 to your computer and use it in GitHub Desktop.
Save hyperlogic/83936 to your computer and use it in GitHub Desktop.
;; Gimp script-fu plugin
;;
;; Draws a polygon with the specified number of sides into a new layer.
;; The edges of the polygon are drawn using the current brush & foreground color.
;;
;; Author: Anthony J. Thibault
;; email: ajt@hyperlogic.org
;;
;; Copyright (c) 2009 Anthony J. Thibault
;;
;; Permission is hereby granted, free of charge, to any person obtaining a copy
;; of this software and associated documentation files (the "Software"), to deal
;; in the Software without restriction, including without limitation the rights
;; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;; copies of the Software, and to permit persons to whom the Software is
;; furnished to do so, subject to the following conditions:
;;
;; The above copyright notice and this permission notice shall be included in
;; all copies or substantial portions of the Software.
;;
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;; THE SOFTWARE.
(define (walk v stroke-id center-x center-y radius theta theta-end theta-step)
(if (<= theta theta-end)
(begin
(gimp-vectors-bezier-stroke-lineto v
stroke-id
(+ center-x (* (cos theta) radius))
(+ center-y (* (sin theta) radius)))
(walk v stroke-id center-x center-y radius (+ theta theta-step) theta-end theta-step))))
(define (script-fu-render-polygon image sides)
(let* ((width (car (gimp-image-width image)))
(height (car (gimp-image-height image)))
(layer (car (gimp-layer-new image
width
height
1 ; rbga
"polygon" ; name
100 ; opacity
0)))) ; combination mode
(gimp-image-undo-disable image)
(gimp-image-add-layer image layer -1)
(gimp-drawable-fill layer 3) ; transparent fill
(let ((v (car (gimp-vectors-new image "polygon")))
(two-pi (* 2 (acos -1)))
(center-x (/ width 2.0))
(center-y (/ height 2.0))
(radius (/ width 2.0)))
(gimp-image-add-vectors image v -1)
(let ((stroke-id (car (gimp-vectors-bezier-stroke-new-moveto v width (/ height 2.0)))))
(walk v stroke-id center-x center-y radius (/ two-pi sides) two-pi (/ two-pi sides))
(gimp-vectors-stroke-close v stroke-id)
(gimp-edit-stroke-vectors layer v)))
(gimp-image-undo-enable image)))
;; register
(script-fu-register
"script-fu-render-polygon" ;func name
"Polygon" ;menu label
"Draws a polygon with the specified number of sides into a new layer. The edges of the polygon are drawn using the current brush & foreground color"
"Anthony Thibault" ;author
"Copyright 2008, Anthony Thibault" ;copyright notice
"January 29, 2008" ;date created
"RGB*" ;image type that the script works on
SF-IMAGE "Image" 0
SF-VALUE "Sides:" "5" ;sides
)
(script-fu-menu-register "script-fu-render-polygon" "<Image>/Script-Fu")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment