Advertisement
ptrelford

Loewensberg Square

Nov 2nd, 2014
443
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 width, height = 512, 512
  9.  
  10. let black = 0,0,0
  11. let red = 183,51,32
  12. let green = 45, 140, 108
  13. let blue = 68, 116, 178
  14.  
  15. let toBrush (r,g,b) =
  16.    let color = Color.FromArgb(255,r,g,b)
  17.    new SolidBrush(color)
  18.  
  19. let draw (a:int) =
  20.    let a = (float a * Math.PI) / 180.0
  21.    let image = new Bitmap(width, height)
  22.    use graphics = Graphics.FromImage(image)  
  23.    let brush = toBrush black
  24.    graphics.FillRectangle(brush, 0, 0, width, height)  
  25.  
  26.    let n = 50
  27.    let points =
  28.       [|n, -n;
  29.        width+n, n;
  30.        width-n, height+n;
  31.        -n,height-n|]
  32.       |> Array.map (fun (x,y) -> Point(x,y))
  33.  
  34.    let r = float width / 2.0
  35.    let transform (x,y) = x*cos(a)-y*sin(a), x*sin(a)+y*cos(a)
  36.    let translate (x,y) = Point(int (x*r) + width/2,int (y*r)+height/2)
  37.  
  38.    let rectangle points color =
  39.       let points =
  40.          points
  41.          |> Array.map transform            
  42.          |> Array.map translate
  43.       let brush = toBrush color
  44.       graphics.FillPolygon(brush,points)
  45.  
  46.    let points =
  47.       [|-1.0,-1.0;
  48.          1.0,-1.0;
  49.          1.0, 1.0;
  50.         -1.0, 1.0|]
  51.    rectangle points red
  52.  
  53.    let points =
  54.       [|-1.0,-2.0;
  55.          1.0,-2.0;
  56.          1.0,-1.0;
  57.         -1.0,-1.0|]
  58.    rectangle points blue
  59.  
  60.    let points =
  61.       [| 1.0,-1.0;
  62.          2.0,-1.0;
  63.          2.0, 1.0;
  64.          1.0, 1.0|]
  65.    rectangle points blue
  66.  
  67.    let points =
  68.       [| -1.0,-1.0;
  69.          -2.0,-1.0;
  70.          -2.0, 1.0;
  71.          -1.0, 1.0|]
  72.    rectangle points green
  73.  
  74.    let points =
  75.       [|-1.0, 2.0;
  76.          1.0, 2.0;
  77.          1.0, 1.0;
  78.         -1.0, 1.0|]
  79.    rectangle points green
  80.  
  81.    image
  82.  
  83. let show () =
  84.    let image = draw 45
  85.    let form = new Form (Text="Loewensberg Lines", Width=width+16, Height=height+36)  
  86.    let picture = new PictureBox(Dock=DockStyle.Fill, Image=image)
  87.    image.Save(@"C:\app\Loewensberg Square.png", Imaging.ImageFormat.Png)
  88.    do  form.Controls.Add(picture)
  89.    form.ShowDialog() |> ignore
  90.  
  91. show()
  92.  
  93.  
  94. #r @"Gif.Components.dll"
  95.  
  96. open Gif.Components
  97.  
  98. let encoder = AnimatedGifEncoder()
  99.  
  100. if encoder.Start(@"c:\temp\Loewensberg square.gif") then
  101.    encoder.SetFrameRate(10.0f)
  102.    encoder.SetRepeat 0
  103.    for angle in 0..5..360 do
  104.       encoder.AddFrame(draw angle) |> ignore
  105.    encoder.Finish() |> ignore
Advertisement
RAW Paste Data Copied
Advertisement