4 people like it.

Koch snowflake

Script to render a Koch snowflake using a Windows Forms bitmap.

 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: 
#r "System.Drawing.dll"
#r "System.Windows.Forms.dll"
 
open System
open System.Drawing
open System.Windows.Forms
 
let width, height = 512, 512

let snowflake (graphics:Graphics) length =
   use pen = new Pen(Color.White)
   let angle = ref 0.0
   let x = ref ((float width/2.0) - length/2.0)
   let y = ref ((float height/2.0) - length/3.0)   
   let rec segment n depth =
      if depth = 0 then
         line n
      else        
         segment (n/3.0) (depth-1)
         rotate -60.0                  
         segment (n/3.0) (depth-1)
         rotate 120.0         
         segment (n/3.0) (depth-1)
         rotate -60.0         
         segment (n/3.0) (depth-1)
   and line n =
      let r = !angle * Math.PI / 180.0
      let x2 = !x + cos(r) * n
      let y2 = !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 = 5
   segment length depth
   rotate 120.0  
   segment length depth  
   rotate 120.0
   segment length depth

let draw () =   
   let image = new Bitmap(width, height)
   use graphics = Graphics.FromImage(image)  
   use brush = new SolidBrush(Color.Black)
   graphics.FillRectangle(brush, 0, 0, width, height)  
   snowflake graphics 360.0
   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:\temp\Koch.png", Imaging.ImageFormat.Png)
   do  form.Controls.Add(picture)
   form.ShowDialog() |> ignore
 
show()
namespace System
namespace System.Drawing
namespace System.Windows
namespace System.Windows.Forms
val width : int

Full name: Script.width
val height : int

Full name: Script.height
val snowflake : graphics:Graphics -> length:float -> unit

Full name: Script.snowflake
val graphics : Graphics
type Graphics =
  inherit MarshalByRefObject
  member AddMetafileComment : data:byte[] -> unit
  member BeginContainer : unit -> GraphicsContainer + 2 overloads
  member Clear : color:Color -> unit
  member Clip : Region with get, set
  member ClipBounds : RectangleF
  member CompositingMode : CompositingMode with get, set
  member CompositingQuality : CompositingQuality with get, set
  member CopyFromScreen : upperLeftSource:Point * upperLeftDestination:Point * blockRegionSize:Size -> unit + 3 overloads
  member Dispose : unit -> unit
  member DpiX : float32
  ...
  nested type DrawImageAbort
  nested type EnumerateMetafileProc

Full name: System.Drawing.Graphics
val length : float
val pen : Pen
Multiple items
type Pen =
  inherit MarshalByRefObject
  new : color:Color -> Pen + 3 overloads
  member Alignment : PenAlignment with get, set
  member Brush : Brush with get, set
  member Clone : unit -> obj
  member Color : Color with get, set
  member CompoundArray : float32[] with get, set
  member CustomEndCap : CustomLineCap with get, set
  member CustomStartCap : CustomLineCap with get, set
  member DashCap : DashCap with get, set
  member DashOffset : float32 with get, set
  ...

Full name: System.Drawing.Pen

--------------------
Pen(color: Color) : unit
Pen(brush: Brush) : unit
Pen(color: Color, width: float32) : unit
Pen(brush: Brush, width: float32) : unit
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
property Color.White: Color
val angle : float ref
Multiple items
val ref : value:'T -> 'T ref

Full name: Microsoft.FSharp.Core.Operators.ref

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val x : float ref
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val y : float ref
val segment : (float -> int -> unit)
val n : float
val depth : int
val line : (float -> unit)
val rotate : (float -> unit)
val r : float
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
field Math.PI = 3.14159265359
val x2 : float
val cos : value:'T -> 'T (requires member Cos)

Full name: Microsoft.FSharp.Core.Operators.cos
val y2 : float
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
Graphics.DrawLine(pen: Pen, pt1: Point, pt2: Point) : unit
Graphics.DrawLine(pen: Pen, pt1: PointF, pt2: PointF) : unit
Graphics.DrawLine(pen: Pen, x1: int, y1: int, x2: int, y2: int) : unit
Graphics.DrawLine(pen: Pen, x1: float32, y1: float32, x2: float32, y2: float32) : unit
Multiple items
val float32 : value:'T -> float32 (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float32

--------------------
type float32 = Single

Full name: Microsoft.FSharp.Core.float32

--------------------
type float32<'Measure> = float32

Full name: Microsoft.FSharp.Core.float32<_>
val a : float
val draw : unit -> Bitmap

Full name: Script.draw
val image : Bitmap
Multiple items
type Bitmap =
  inherit Image
  new : filename:string -> Bitmap + 11 overloads
  member Clone : rect:Rectangle * format:PixelFormat -> Bitmap + 1 overload
  member GetHbitmap : unit -> nativeint + 1 overload
  member GetHicon : unit -> nativeint
  member GetPixel : x:int * y:int -> Color
  member LockBits : rect:Rectangle * flags:ImageLockMode * format:PixelFormat -> BitmapData + 1 overload
  member MakeTransparent : unit -> unit + 1 overload
  member SetPixel : x:int * y:int * color:Color -> unit
  member SetResolution : xDpi:float32 * yDpi:float32 -> unit
  member UnlockBits : bitmapdata:BitmapData -> unit
  ...

Full name: System.Drawing.Bitmap

--------------------
Bitmap(filename: string) : unit
   (+0 other overloads)
Bitmap(stream: IO.Stream) : unit
   (+0 other overloads)
Bitmap(original: Image) : unit
   (+0 other overloads)
Bitmap(filename: string, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(type: Type, resource: string) : unit
   (+0 other overloads)
Bitmap(stream: IO.Stream, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(width: int, height: int) : unit
   (+0 other overloads)
Bitmap(original: Image, newSize: Size) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, format: Imaging.PixelFormat) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, g: Graphics) : unit
   (+0 other overloads)
Graphics.FromImage(image: Image) : Graphics
val brush : SolidBrush
Multiple items
type SolidBrush =
  inherit Brush
  new : color:Color -> SolidBrush
  member Clone : unit -> obj
  member Color : Color with get, set

Full name: System.Drawing.SolidBrush

--------------------
SolidBrush(color: Color) : unit
property Color.Black: Color
Graphics.FillRectangle(brush: Brush, rect: Rectangle) : unit
Graphics.FillRectangle(brush: Brush, rect: RectangleF) : unit
Graphics.FillRectangle(brush: Brush, x: int, y: int, width: int, height: int) : unit
Graphics.FillRectangle(brush: Brush, x: float32, y: float32, width: float32, height: float32) : unit
val show : unit -> unit

Full name: Script.show
val form : Form
Multiple items
type Form =
  inherit ContainerControl
  new : unit -> Form
  member AcceptButton : IButtonControl with get, set
  member Activate : unit -> unit
  member ActiveMdiChild : Form
  member AddOwnedForm : ownedForm:Form -> unit
  member AllowTransparency : bool with get, set
  member AutoScale : bool with get, set
  member AutoScaleBaseSize : Size with get, set
  member AutoScroll : bool with get, set
  member AutoSize : bool with get, set
  ...
  nested type ControlCollection

Full name: System.Windows.Forms.Form

--------------------
Form() : unit
Multiple items
namespace System.Drawing.Text

--------------------
namespace System.Text
val picture : PictureBox
Multiple items
type PictureBox =
  inherit Control
  new : unit -> PictureBox
  member AllowDrop : bool with get, set
  member BorderStyle : BorderStyle with get, set
  member CancelAsync : unit -> unit
  member CausesValidation : bool with get, set
  member ErrorImage : Image with get, set
  member Font : Font with get, set
  member ForeColor : Color with get, set
  member Image : Image with get, set
  member ImageLocation : string with get, set
  ...

Full name: System.Windows.Forms.PictureBox

--------------------
PictureBox() : unit
type DockStyle =
  | None = 0
  | Top = 1
  | Bottom = 2
  | Left = 3
  | Right = 4
  | Fill = 5

Full name: System.Windows.Forms.DockStyle
field DockStyle.Fill = 5
type Image =
  inherit MarshalByRefObject
  member Clone : unit -> obj
  member Dispose : unit -> unit
  member Flags : int
  member FrameDimensionsList : Guid[]
  member GetBounds : pageUnit:GraphicsUnit -> RectangleF
  member GetEncoderParameterList : encoder:Guid -> EncoderParameters
  member GetFrameCount : dimension:FrameDimension -> int
  member GetPropertyItem : propid:int -> PropertyItem
  member GetThumbnailImage : thumbWidth:int * thumbHeight:int * callback:GetThumbnailImageAbort * callbackData:nativeint -> Image
  member Height : int
  ...
  nested type GetThumbnailImageAbort

Full name: System.Drawing.Image
Image.Save(filename: string) : unit
Image.Save(stream: IO.Stream, format: Imaging.ImageFormat) : unit
Image.Save(filename: string, format: Imaging.ImageFormat) : unit
Image.Save(stream: IO.Stream, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
Image.Save(filename: string, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
namespace System.Drawing.Imaging
Multiple items
type ImageFormat =
  new : guid:Guid -> ImageFormat
  member Equals : o:obj -> bool
  member GetHashCode : unit -> int
  member Guid : Guid
  member ToString : unit -> string
  static member Bmp : ImageFormat
  static member Emf : ImageFormat
  static member Exif : ImageFormat
  static member Gif : ImageFormat
  static member Icon : ImageFormat
  ...

Full name: System.Drawing.Imaging.ImageFormat

--------------------
Imaging.ImageFormat(guid: Guid) : unit
property Imaging.ImageFormat.Png: Imaging.ImageFormat
property Control.Controls: Control.ControlCollection
Control.ControlCollection.Add(value: Control) : unit
Form.ShowDialog() : DialogResult
Form.ShowDialog(owner: IWin32Window) : DialogResult
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore

More information

Link:http://fssnip.net/oA
Posted:3 years ago
Author:Phillip Trelford
Tags: koch , snowflake