Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "System.Drawing.dll"
- #r "System.Windows.Forms.dll"
- open System
- open System.Drawing
- open System.Windows.Forms
- let width, height = 512, 512
- let black = 0,0,0
- let white = 255,255,255
- let red = 183,51,32
- let green = 45, 140, 108
- let blue = 68, 116, 178
- let toBrush (r,g,b) =
- let color = Color.FromArgb(255,r,g,b)
- new SolidBrush(color)
- let snowflake (graphics:Graphics) length =
- let pen = ref (new Pen(toBrush white))
- let angle = ref 0
- let x = ref ((float width/2.0) - length/2.0)
- let y = ref ((float height/2.0) - length/3.0)
- let n = length
- let rec draw n depth =
- if depth = 0 then
- line n
- else
- let d = n/3.0
- pen := new Pen(toBrush red)
- draw (n/3.0) (depth-1)
- rotate -60
- pen := new Pen(toBrush green)
- draw (n/3.0) (depth-1)
- rotate 120
- pen := new Pen(toBrush blue)
- draw (n/3.0) (depth-1)
- rotate -60
- pen := new Pen(toBrush red)
- draw (n/3.0) (depth-1)
- and line n =
- let r = (float !angle * Math.PI) / 180.0
- let x2 = float !x + cos(r) * n
- let y2 = float !y + sin(r) * n
- graphics.DrawLine(!pen, float32 !x,float32 !y, float32 x2, float32 y2)
- x := x2
- y := y2
- and rotate a =
- angle := !angle + a
- let depth = 3
- pen := new Pen(toBrush red)
- draw n depth
- rotate 120
- pen := new Pen(toBrush green)
- draw n depth
- pen := new Pen(toBrush blue)
- rotate 120
- draw n depth
- let draw () =
- let image = new Bitmap(width, height)
- use graphics = Graphics.FromImage(image)
- let brush = toBrush black
- graphics.FillRectangle(brush, 0, 0, width, height)
- for i in 10.0..20.0..360.0 do
- snowflake (graphics) i
- image
- let show () =
- let image = draw ()
- let form = new Form (Text="Koch Snowflake", Width=width+16, Height=height+36)
- let picture = new PictureBox(Dock=DockStyle.Fill, Image=image)
- image.Save(@"C:\app\Koch.png", Imaging.ImageFormat.Png)
- do form.Controls.Add(picture)
- form.ShowDialog() |> ignore
- show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement