Last active
November 10, 2015 23:24
-
-
Save bricef/62eb5b998637a91a4c62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#load "Spirograph.fs" | |
open System.Drawing | |
//open FSharp.TV.Spirograph | |
let round (x:float) = int (System.Math.Round (x)) | |
module Seq = | |
let repeat items = | |
seq { while true do for item in items do yield item } | |
type Turtle = { | |
position: (int*int); | |
direction: float; | |
bitmap: Bitmap; | |
color: Color; | |
drawing: bool | |
} | |
let save filename turtle = | |
turtle.bitmap.Save(filename) | |
turtle | |
let dot x y turtle = | |
turtle.bitmap.SetPixel(x, y, turtle.color) | |
turtle | |
let line x y turtle = | |
let x0, y0 = turtle.position | |
let dx = x-x0 | |
let dy = y-y0 | |
let xs = if dx > 0 then [ for ix in [x0..x] -> (ix, y0+(ix-x0)*dy/dx) ] else [] | |
let ys = if dy > 0 then [ for iy in [y0..y] -> (x0+(iy-y0)*dx/dy,iy) ] else [] | |
let dots = Seq.map (fun (x,y) -> dot x y) (Seq.append xs ys) //[for (x,y) in (Seq.append xs ys) -> dot x y ] | |
{Seq.fold (fun turtle fn -> fn turtle) turtle dots with position = (x,y)} | |
let move (distance:int) turtle = | |
let x, y = turtle.position | |
let xd, yd = round (float distance * cos turtle.direction), round (float distance * sin turtle.direction) | |
let nx, ny = x+xd, y+yd | |
if turtle.drawing then | |
line nx ny turtle | |
else | |
{turtle with position = (nx, ny)} | |
let teleport (x:int) (y:int) (turtle:Turtle) = | |
{turtle with position = (x,y)} | |
let penUp (ttl:Turtle) = {ttl with drawing = false} | |
let penDown (ttl:Turtle) = {ttl with drawing = true} | |
let dashMove (distance:int) turtle = | |
let dashlength = 5 | |
let cmds = [penDown; move dashlength; penUp; move dashlength] | |
let numcmds = distance*(Seq.length cmds)/(dashlength*2) | |
let todo = Seq.take numcmds (Seq.repeat cmds) | |
Seq.fold (fun ttl cmd -> cmd ttl) turtle todo | |
let setColor color ttl = {ttl with color = color} | |
let newTurtle (x:int) (y:int) = { | |
position = (0,0); | |
direction = 0.0; | |
bitmap = new Bitmap(x,y); | |
color=Color.Black; | |
drawing=true | |
} | |
let clockwise rads (turtle:Turtle) = | |
{turtle with direction = turtle.direction + rads} | |
newTurtle 100 100 | |
|> teleport 50 50 | |
|> setColor Color.Red | |
|> dashMove 50 | |
|> save (__SOURCE_DIRECTORY__ + "/foo.png") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment