Skip to content

Instantly share code, notes, and snippets.

@gbluma
Created September 10, 2012 20:18
Show Gist options
  • Save gbluma/3693563 to your computer and use it in GitHub Desktop.
Save gbluma/3693563 to your computer and use it in GitHub Desktop.
Simple recursive function to build a list of paired points (line segments) that also loops back to first element.
#lang racket
(define shapes '(((0 0) (0 110) (220 0 ) (220 110))
((1 1) (1 111) (221 1 ) (221 111)))
)
(define (get-lines shape output)
;; do we have enough elements to build a pair?
(if (> (length shape) 1)
;; ... more than one element (build pair and recurse)
(let* ([p (list (take shape 2))]
[rest (list-tail shape 1)]
[new-output (append output p)])
(get-lines rest new-output))
;; ... just one element (take first from output and return)
(let* ([last-el (first (take shape 1))]
[first-el (first (first output))]
[p (list (list last-el first-el))]
[new-output (append output p)])
new-output)))
(map (λ (shape)
(printf "~s~n" (get-lines shape '())))
shapes)
;; Example output:
;; (((0 0) (0 110)) ((0 110) (220 0)) ((220 0) (220 110)) ((220 110) (0 0)))
;; (((1 1) (1 111)) ((1 111) (221 1)) ((221 1) (221 111)) ((221 111) (1 1)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment