1
1
import { translatePt , rotatePt } from "./affineTransformations.js" ;
2
- import { assertArgs } from './assert.js'
3
2
4
3
const copy = obj => JSON . parse ( JSON . stringify ( obj ) )
5
4
6
5
export class Turtle {
7
6
8
7
constructor ( ) {
9
- assertArgs ( arguments , [ ] , 'new bt.Turtle' )
10
-
11
8
this . drawing = true
12
9
this . position = [ 0 , 0 ]
13
10
this . angle = 0
@@ -18,24 +15,18 @@ export class Turtle {
18
15
}
19
16
20
17
up ( ) {
21
- assertArgs ( arguments , [ ] , 'turtle.up' )
22
-
23
18
if ( ! this . drawing ) return this
24
19
this . drawing = false
25
20
this . path . push ( [ [ ...this . position ] ] )
26
21
return this
27
22
}
28
23
29
24
down ( ) {
30
- assertArgs ( arguments , [ ] , 'turtle.down' )
31
-
32
25
this . drawing = true
33
26
return this
34
27
}
35
28
36
29
goTo ( [ x , y ] ) {
37
- assertArgs ( arguments , [ 'point' ] , 'turtle.goTo' )
38
-
39
30
const lastPath = this . path . at ( - 1 )
40
31
if ( this . drawing ) {
41
32
const [ lastX , lastY ] = this . position
@@ -51,9 +42,7 @@ export class Turtle {
51
42
return this
52
43
}
53
44
54
- step ( [ dx , dy ] ) {
55
- assertArgs ( arguments , [ 'point' ] , 'turtle.step' )
56
-
45
+ step ( [ dx , dy ] ) { // document this
57
46
const [ x , y ] = this . position ;
58
47
59
48
this . goTo ( [
@@ -65,8 +54,6 @@ export class Turtle {
65
54
}
66
55
67
56
jump ( pt ) {
68
- assertArgs ( arguments , [ 'point' ] , 'turtle.jump' )
69
-
70
57
const [ x , y ] = pt ;
71
58
const lastPath = this . path . at ( - 1 ) ;
72
59
if ( lastPath . length === 1 ) {
@@ -82,8 +69,6 @@ export class Turtle {
82
69
}
83
70
84
71
forward ( distance ) {
85
- assertArgs ( arguments , [ 'number' ] , 'turtle.forward' )
86
-
87
72
const last = this . position
88
73
const a = ( this . angle / 180 ) * Math . PI
89
74
const x = last [ 0 ] + distance * Math . cos ( a )
@@ -95,8 +80,6 @@ export class Turtle {
95
80
}
96
81
97
82
arc ( angle , radius ) {
98
- assertArgs ( arguments , [ 'number' , 'number' ] , 'turtle.arc' )
99
-
100
83
if ( angle === 0 || radius === 0 ) return this ;
101
84
102
85
const n = 64 ;
@@ -135,8 +118,6 @@ export class Turtle {
135
118
136
119
// setHeading?
137
120
setAngle ( theta ) {
138
- assertArgs ( arguments , [ 'number' ] , 'turtle.setAngle' )
139
-
140
121
this . angle = theta
141
122
142
123
return this
@@ -147,33 +128,25 @@ export class Turtle {
147
128
// }
148
129
149
130
right ( theta ) {
150
- assertArgs ( arguments , [ 'number' ] , 'turtle.right' )
151
-
152
131
this . angle -= theta
153
132
154
133
return this
155
134
}
156
135
157
136
left ( theta ) {
158
- assertArgs ( arguments , [ 'number' ] , 'turtle.left' )
159
-
160
137
this . angle += theta
161
138
162
139
return this
163
140
}
164
141
165
142
lines ( ) { // could be called polylines
166
- assertArgs ( arguments , [ ] , 'turtle.lines' )
167
-
168
143
const pls = copy ( this . path ) ;
169
144
170
145
return pls . filter ( pl => pl . length > 1 ) ;
171
146
}
172
147
173
148
174
149
copy ( ) {
175
- assertArgs ( arguments , [ ] , 'turtle.copy' )
176
-
177
150
const t = new Turtle ( )
178
151
179
152
t . path = copy ( this . path )
@@ -185,16 +158,12 @@ export class Turtle {
185
158
}
186
159
187
160
applyToPath ( fn ) {
188
- assertArgs ( arguments , [ 'function' ] , 'turtle.applyToPath' )
189
-
190
161
fn ( this . path ) ;
191
162
return this ;
192
163
}
193
164
194
165
// undoced
195
166
apply ( fn ) {
196
- assertArgs ( arguments , [ 'function' ] , 'turtle.apply' )
197
-
198
167
fn ( this ) ;
199
168
return this ;
200
169
}
0 commit comments