[plt-scheme] drawing open lines

From: David Van Horn (dvanhorn at ccs.neu.edu)
Date: Thu May 14 21:19:28 EDT 2009

I'm trying to draw a list of lines on a canvas, but the lines seem to be 
implicitly closed so that further drawing within the area closed by the 
lines does not appear.

Attached is a sample program.  Draw a circle (click and drag the mouse 
on the canvas), then draw a line through it to see the effect.

How can I draw lines without this closing behavior?


#lang scheme/gui
(define frame (new frame%
                    [label "Squiggles"]
                    [width 300]
                    [height 300]))

;; A canvas you can draw squiggles on.
(define squiggle-canvas%
   (class canvas%
     (define squiggling? false)
     (define points empty)  ;; Listof Point
     (define strokes empty) ;; Listof [Listof Point]

     (define/public (draw-strokes dc)
       (for-each (lambda (s) (send dc draw-path (stroke->path s)))

     ;; [Listof Point] -> Path
     (define (stroke->path s)
       (let ((path (make-object dc-path%)))
         (send path move-to
               (send (first s) get-x)
               (send (first s) get-y))
         (send path lines s)

     (define/override (on-event ev)
       (cond [(and (not squiggling?)
                   (send ev dragging?))
              ; start sguiggling
              (set! squiggling? true)
              (set! points empty)]
             [(and squiggling?
                   (send ev dragging?))
              ; continue squiggling
              (set! points
                    (cons (make-object point%
                            (send ev get-x)
                            (send ev get-y))
              ; stop squiggling (other event)
              (unless (empty? points)
                (set! strokes (cons points strokes)))
              (set! squiggling? false)
              (send this on-paint)]
             [else (void)]))


; Make the drawing area
(define canvas
   (new squiggle-canvas%
        [parent frame]
        [style '(no-autoclear)]
         (lambda (canvas dc)
           (send dc set-smoothing 'smoothed)
           (send dc set-pen "red" 3 'dot)
           (send canvas draw-strokes dc))]))

(send frame show #t)

Posted on the users mailing list.