-
Notifications
You must be signed in to change notification settings - Fork 18
/
OOTurtleLib.fsx
60 lines (45 loc) · 1.67 KB
/
OOTurtleLib.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(* ======================================
OOTurtleLib.fsx
Part of "Thirteen ways of looking at a turtle"
Related blog post: http://fsharpforfunandprofit.com/posts/13-ways-of-looking-at-a-turtle/
======================================
Common code for OO-style mutable turtle class
====================================== *)
// requires Common.fsx to be loaded by parent file
// Uncomment to use this file standalone
// #load "Common.fsx"
open System
open Common
// ======================================
// Turtle class
// ======================================
// inject a logging function
type Turtle(log) =
let mutable currentPosition = initialPosition
let mutable currentAngle = 0.0<Degrees>
let mutable currentColor = initialColor
let mutable currentPenState = initialPenState
member this.Move(distance) =
log (sprintf "Move %0.1f" distance)
// calculate new position
let newPosition = calcNewPosition distance currentAngle currentPosition
// draw line if needed
if currentPenState = Down then
dummyDrawLine log currentPosition newPosition currentColor
// update the state
currentPosition <- newPosition
member this.Turn(angle) =
log (sprintf "Turn %0.1f" angle)
// calculate new angle
let newAngle = (currentAngle + angle) % 360.0<Degrees>
// update the state
currentAngle <- newAngle
member this.PenUp() =
log "Pen up"
currentPenState <- Up
member this.PenDown() =
log "Pen down"
currentPenState <- Down
member this.SetColor(color) =
log (sprintf "SetColor %A" color)
currentColor <- color