Advertisement
ptrelford

Loewensberg

Oct 31st, 2014
582
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #r "System.Drawing.dll"
  2. #r "System.Windows.Forms.dll"
  3.  
  4. open System
  5. open System.Drawing
  6. open System.Windows.Forms
  7.  
  8. let rand = Random()
  9. let width, height = 512, 512
  10.  
  11. let createCircles () =
  12.    let toCircles k length =
  13.       Seq.unfold (fun distance ->
  14.          let d = length/32 + rand.Next(length/k)  
  15.          if (distance + d) < length then Some((d,distance), distance + d)
  16.          else None
  17.       ) 0
  18.       |> Seq.toArray
  19.    let toRect k length w h =
  20.       let lines = [        
  21.          w/2,h/2, -1, 0, 0      // b   r to l        
  22.          -w/2, h/2, 0, -1, 90   // l   b to t
  23.          -w/2, -h/2, 1, 0, 180  // t   l to r
  24.          w/2,-h/2, 0, 1, 270    // r   t to b
  25.       ]
  26.       [for (x,y,dx,dy,a) in lines do
  27.          let rs = toCircles k length
  28.          yield!
  29.             [for (d,k) in rs ->
  30.                let n = k + d/2
  31.                d, x + n*dx, y + n*dy, a]
  32.       ]
  33.    let rects = [75, 5; 53, 4; 35, 4; 22, 3; 13, 2] |> List.rev
  34.    [for pc,k in rects do
  35.       let w = width * pc / 100
  36.       let h = height * pc / 100
  37.       let circles = toRect k w w h
  38.       for (d,x,y,a) in circles do      
  39.          let x = width/2 + x
  40.          let y = height/2 + y
  41.          yield (x,y,d,a)
  42.    ]
  43.  
  44. let draw circles =
  45.    let image = new Bitmap(width, height)
  46.    use graphics = Graphics.FromImage(image)
  47.    graphics.SmoothingMode <- System.Drawing.Drawing2D.SmoothingMode.AntiAlias
  48.    let color = Color.FromArgb(255,234,236,232)
  49.    let brush = new SolidBrush(color)
  50.    graphics.FillRectangle(brush, 0, 0, width, height)
  51.    (*
  52.    let rects = [75; 53; 35; 22; 13] |> List.rev
  53.    rects |> Seq.iter (fun pc ->
  54.       let w = width * pc / 100
  55.       let h = height * pc / 100      
  56.       graphics.DrawRectangle(Pens.Black, width/2-w/2, height/2-h/2, w, h)            
  57.    )
  58.    *)
  59.    circles |> List.fold (fun xs circle ->
  60.       let x,y,d,a = circle
  61.       let isTouching =
  62.          xs |> List.exists (fun (x1,y1,d1,_) ->
  63.                let k = ((x1-x)*(x1-x))+((y1-y)*(y1-y))
  64.                let n = sqrt(float k)
  65.                n < ((float (d1-1)/2.0) + (float (d-1)/2.0))
  66.          )
  67.       let x, y = x - d/2, y - d/2
  68.       let color = Color.FromArgb(255,247,171,27)
  69.       let brush = new SolidBrush(color)
  70.       if isTouching        
  71.       then graphics.FillPie(brush, x, y, d, d, a, 180)
  72.       else graphics.FillEllipse(brush, x, y, d, d)
  73.       circle::xs
  74.    ) []
  75.    |> ignore
  76.    image
  77.  
  78. (*
  79. let show () =
  80.    let image = draw ()
  81.    let form = new Form (Text="Circles", Width=320+16, Height=320+36)  
  82.    let picture = new PictureBox(Dock=DockStyle.Fill, Image=image)
  83.    do  form.Controls.Add(picture)
  84.    form.ShowDialog() |> ignore
  85.  
  86. show()
  87. *)
  88.  
  89. #r @"Gif.Components.dll"
  90.  
  91. open Gif.Components
  92.  
  93. let circles = createCircles ()
  94. let encoder = AnimatedGifEncoder()
  95.  
  96. if encoder.Start(@"c:\temp\Loewensberg step-by-step.gif") then
  97.    encoder.SetFrameRate(10.0f)
  98.    encoder.SetRepeat 0
  99.    for i = 1 to circles.Length do
  100.       let circles = circles |> Seq.take i |> Seq.toList
  101.       encoder.AddFrame(draw circles) |> ignore
  102.    for i = 1 to 10 do
  103.       encoder.AddFrame(draw circles) |> ignore  
  104.    encoder.Finish() |> ignore
Advertisement
RAW Paste Data Copied
Advertisement