Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit fd20a20

Browse files
committedSep 10, 2018
Include library (lispkit draw turtle).
1 parent 848c0cc commit fd20a20

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
 

‎LispKit.xcodeproj/project.pbxproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
CC26264020F8014900AC08E8 /* 113.sld in Copy pre-installed SRFI libraries */ = {isa = PBXBuildFile; fileRef = CC26263F20F7FB8100AC08E8 /* 113.sld */; };
3838
CC26264220F9491E00AC08E8 /* SRFI113.scm in Resources */ = {isa = PBXBuildFile; fileRef = CC26264120F9469100AC08E8 /* SRFI113.scm */; };
3939
CC26264420FA007800AC08E8 /* Plot.scm in Copy examples */ = {isa = PBXBuildFile; fileRef = CC26264320FA003700AC08E8 /* Plot.scm */; };
40+
CC26AD41214722960090D9EE /* turtle.sld in Copy pre-installed LispKit library: LispKit Draw Turtle */ = {isa = PBXBuildFile; fileRef = CC26AD3F214722120090D9EE /* turtle.sld */; };
4041
CC2D124F1DB40E7F009BDC72 /* LibraryManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */; };
4142
CC31A6981D3AC223008D7728 /* Hash.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC31A6971D3AC223008D7728 /* Hash.swift */; };
4243
CC35FD0D1C6FA4B700C8B992 /* VirtualMachine.swift in Sources */ = {isa = PBXBuildFile; fileRef = CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */; };
@@ -251,6 +252,17 @@
251252
name = "Embed Frameworks";
252253
runOnlyForDeploymentPostprocessing = 0;
253254
};
255+
CC26AD40214722710090D9EE /* Copy pre-installed LispKit library: LispKit Draw Turtle */ = {
256+
isa = PBXCopyFilesBuildPhase;
257+
buildActionMask = 2147483647;
258+
dstPath = LispKit/Resources/Libraries/lispkit/draw;
259+
dstSubfolderSpec = 7;
260+
files = (
261+
CC26AD41214722960090D9EE /* turtle.sld in Copy pre-installed LispKit library: LispKit Draw Turtle */,
262+
);
263+
name = "Copy pre-installed LispKit library: LispKit Draw Turtle";
264+
runOnlyForDeploymentPostprocessing = 0;
265+
};
254266
CC2E6A901C486AA70046E680 /* Copy default prelude */ = {
255267
isa = PBXCopyFilesBuildPhase;
256268
buildActionMask = 2147483647;
@@ -452,6 +464,7 @@
452464
CC26263F20F7FB8100AC08E8 /* 113.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = 113.sld; sourceTree = "<group>"; };
453465
CC26264120F9469100AC08E8 /* SRFI113.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = SRFI113.scm; sourceTree = "<group>"; };
454466
CC26264320FA003700AC08E8 /* Plot.scm */ = {isa = PBXFileReference; lastKnownFileType = text; path = Plot.scm; sourceTree = "<group>"; };
467+
CC26AD3F214722120090D9EE /* turtle.sld */ = {isa = PBXFileReference; lastKnownFileType = text; path = turtle.sld; sourceTree = "<group>"; };
455468
CC2D124E1DB40E7F009BDC72 /* LibraryManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LibraryManager.swift; sourceTree = "<group>"; };
456469
CC31A6971D3AC223008D7728 /* Hash.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Hash.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
457470
CC35FD0C1C6FA4B700C8B992 /* VirtualMachine.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = VirtualMachine.swift; sourceTree = "<group>"; tabWidth = 2; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
@@ -729,6 +742,7 @@
729742
CC26AD3E21470AC90090D9EE /* draw */ = {
730743
isa = PBXGroup;
731744
children = (
745+
CC26AD3F214722120090D9EE /* turtle.sld */,
732746
);
733747
path = draw;
734748
sourceTree = "<group>";
@@ -1131,6 +1145,7 @@
11311145
CC8E2BA01E3361C50080299A /* Copy pre-installed LispKit library: SRFI 41 */,
11321146
CC48EBD21F3E5B9900233FA9 /* Copy pre-installed LispKit library: SRFI 135 */,
11331147
CC5848001E50ABE100BE6497 /* Copy pre-installed LispKit libraries */,
1148+
CC26AD40214722710090D9EE /* Copy pre-installed LispKit library: LispKit Draw Turtle */,
11341149
CC7F7C191FC04B8A00639166 /* Copy pre-installed Scheme libraries */,
11351150
CC474EAE1ECFAC1300B535E3 /* Copy examples */,
11361151
);
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
;;; LISPKIT DRAW TURTLE
2+
;;;
3+
;;; This is a simple library implementing turtle graphics. A new turtle plane gets created
4+
;;; by invoking `make-turtle`. `make-turtle` sets the origin of the plane as well as a scaling
5+
;;; factor. A range of functions for modifying the state of a turtle plane is provided:
6+
;;;
7+
;;; - `(pen-up turtle)`: Lifts the turtle from the plane
8+
;;; - `(pen-down turtle)`: Drops the turtle onto the plane
9+
;;; - `(pen-color color turtle)`: Sets the current color of the turtle
10+
;;; - `(pen-size size turtle)`: Sets the size of the turtle pen
11+
;;; - `(home)`: Moves the turtle back to the origin
12+
;;; - `(move x y turtle)`: Moves the turtle to position `(x, y)`
13+
;;; - `(heading angle turtle)`: Sets the angle of the turtle (in radians)
14+
;;; - `(turn angle turtle)`: Turns the turtle by the given angle (in radians)
15+
;;; - `(left angle turtle)`: Turn left by the given angle (in radians)
16+
;;; - `(right angle turtle)`: Turn right by the given angle (in radians)
17+
;;; - `(forward length turtle)`: Moves forward by `length` units drawing a line if the
18+
;;; pen is down
19+
;;; - `(backward length turtle)`: Moves backward by `length` units drawing a line if the
20+
;;; pen is down
21+
;;;
22+
;;; Author: Matthias Zenger
23+
;;; Copyright © 2018 Matthias Zenger. All rights reserved.
24+
;;;
25+
;;; Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
26+
;;; except in compliance with the License. You may obtain a copy of the License at
27+
;;;
28+
;;; http://www.apache.org/licenses/LICENSE-2.0
29+
;;;
30+
;;; Unless required by applicable law or agreed to in writing, software distributed under the
31+
;;; License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
32+
;;; either express or implied. See the License for the specific language governing permissions
33+
;;; and limitations under the License.
34+
35+
(define-library (lispkit draw turtle)
36+
37+
(export turtle?
38+
make-turtle
39+
turtle-drawing
40+
pen-up
41+
pen-down
42+
pen-color
43+
pen-size
44+
home
45+
move
46+
heading
47+
turn
48+
left
49+
right
50+
forward
51+
backward)
52+
53+
(import (lispkit base)
54+
(lispkit draw))
55+
56+
(begin
57+
58+
(define-record-type turtle
59+
(new-turtle drawing x y angle down)
60+
turtle?
61+
(drawing turtle-drawing)
62+
(x turtle-x set-turtle-x!)
63+
(y turtle-y set-turtle-y!)
64+
(angle turtle-angle set-turtle-angle!)
65+
(down turtle-pen-down? set-turtle-pen-down!))
66+
67+
(define (make-turtle x y sc)
68+
(let ((drawing (make-drawing)))
69+
(enable-transformation (scale sc sc (translate x y)) drawing)
70+
(new-turtle drawing 0.0 0.0 0.0 #t)))
71+
72+
(define (pen-up plane)
73+
(set-turtle-pen-down! plane #f))
74+
75+
(define (pen-down plane)
76+
(set-turtle-pen-down! plane #t))
77+
78+
(define (pen-color color plane)
79+
(set-color color (turtle-drawing plane)))
80+
81+
(define (pen-size size plane)
82+
(set-line-width size (turtle-drawing plane)))
83+
84+
(define (home plane)
85+
(move 0.0 0.0 plane))
86+
87+
(define (move x y plane)
88+
(set-turtle-x! plane x)
89+
(set-turtle-y! plane y))
90+
91+
(define (heading angle plane)
92+
(set-turtle-angle! plane (radian angle)))
93+
94+
(define (turn angle plane)
95+
(set-turtle-angle! plane (+ (radian angle) (turtle-angle plane))))
96+
97+
(define (left angle plane)
98+
(turn (- angle) plane))
99+
100+
(define (right angle plane)
101+
(turn angle plane))
102+
103+
(define (forward len plane)
104+
(let* ((angle (turtle-angle plane))
105+
(ox (turtle-x plane))
106+
(oy (turtle-y plane))
107+
(x (+ ox (* (cos angle) len)))
108+
(y (+ oy (* (sin angle) len))))
109+
(if (turtle-pen-down? plane)
110+
(draw-line (point ox oy) (point x y) (turtle-drawing plane)))
111+
(move x y plane)))
112+
113+
(define (backward len plane)
114+
(forward (- len) plane))
115+
116+
(define (radian angle)
117+
(inexact (/ (* angle pi) 180.0)))
118+
)
119+
)
120+

0 commit comments

Comments
 (0)
Please sign in to comment.