Skip to content

Commit 6ab7cc9

Browse files
committed
Add convenience definitions to (lispkit draw).
1 parent ec39e09 commit 6ab7cc9

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

Sources/LispKit/Primitives/DrawingLibrary.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public final class DrawingLibrary: NativeLibrary {
101101

102102
/// Dependencies of the library.
103103
public override func dependencies() {
104-
self.`import`(from: ["lispkit", "core"], "define-syntax", "syntax-rules")
104+
self.`import`(from: ["lispkit", "core"], "define", "define-syntax", "syntax-rules")
105105
self.`import`(from: ["lispkit", "control"], "let")
106106
self.`import`(from: ["lispkit", "dynamic"], "parameterize")
107107
self.`import`(from: ["lispkit", "system"], "current-directory")
@@ -200,6 +200,16 @@ public final class DrawingLibrary: NativeLibrary {
200200
self.define(Procedure("rect-width", rectWidth))
201201
self.define(Procedure("rect-height", rectHeight))
202202

203+
// Define constants
204+
self.define("zero-point", via: "(define zero-point (point 0 0))")
205+
self.define("black", via: "(define black (make-color 0 0 0))")
206+
self.define("gray", via: "(define gray (make-color 0.5 0.5 0.5))")
207+
self.define("white", via: "(define white (make-color 1 1 1))")
208+
self.define("red", via: "(define red (make-color 1 0 0))")
209+
self.define("green", via: "(define green (make-color 0 1 0))")
210+
self.define("blue", via: "(define blue (make-color 0 0 1))")
211+
self.define("yellow", via: "(define yellow (make-color 1 1 0))")
212+
203213
// Syntax definitions
204214
self.define("drawing", via: """
205215
(define-syntax drawing

Sources/LispKit/Resources/Examples/Plot.scm

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,21 @@
4242
; Move rest of drawing into the bounding box
4343
(transform (translate (rect-x rect) (rect-y rect))
4444
; Draw the coordinate axis
45-
(make-color 0.3 0.3 0.3)
45+
(set-color (make-color 0.3 0.3 0.3))
4646
(if (and (<= xmin 0.0) (>= xmax 0.0))
4747
(draw (line (point (* xfac (- xmin)) 0)
4848
(point (* xfac (- xmin)) (rect-height rect))) 0.3))
4949
(if (and (<= ymin 0.0) (>= ymax 0.0))
5050
(draw (line (point 0 (+ (rect-height rect) (* yfac ymin)))
5151
(point (rect-width rect) (+ (rect-height rect) (* yfac ymin)))) 0.3))
5252
; Draw flipped interpolation shape
53-
(set-color (make-color 0 0 1))
53+
(set-color blue)
5454
(draw graph)
5555
; Draw interpolation points
56-
(set-color (make-color 0 0 0))
57-
(set-fill-color (make-color 0 0 0))
58-
(for-each (lambda (p) (fill (flip-shape (circle p 1) (shape-bounds graph)))) ps)
56+
(set-fill-color white)
57+
(for-each (lambda (p)
58+
(let ((s (flip-shape (circle p 1) (shape-bounds graph))))
59+
(fill s) (draw s 0.3))) ps)
5960
; Draw the label
6061
(draw-text label
6162
(point 30 (- (rect-height rect) 12))
@@ -68,10 +69,7 @@
6869
(define page
6970
(drawing
7071
; Draw a header in font "Helvetica-Bold" of size 8
71-
(draw-text "Demo of library (lispkit draw)"
72-
(point 160 8)
73-
(font "Helvetica-Bold" 8)
74-
(make-color 0 0 0))
72+
(draw-text "Demo of library (lispkit draw)" (point 160 8) (font "Helvetica-Bold" 8) black)
7573
; Plot four graphs
7674
(draw-drawing (plot sin -1 6.3 50 (rect 10 30 200 100) "sin(x)"))
7775
(draw-drawing (plot cos -1 6.3 50 (rect 220 30 200 100) "cos(x)"))

0 commit comments

Comments
 (0)