3 people like it.
Like the snippet!
Random Art
Drops 1000 randomly generated pictures in C:\temp. Based on Andrej Bauer's Random Art implementation: http://www.random-art.org/about/
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:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
|
type expr =
| VariableX
| VariableY
| Constant
| Sum of expr * expr
| Product of expr * expr
| Mod of expr * expr
| Well of expr
| Tent of expr
| Sin of expr
| Level of expr * expr * expr
| Mix of expr * expr * expr
let random = System.Random()
let next () = random.NextDouble()
let average (c1,c2,w) =
let r1,g1,b1 = c1
let r2,g2,b2 = c2
let r = w * r1 + (1.0 - w) * r2
let g = w * g1 + (1.0 - w) * g2
let b = w * b1 + (1.0 - w) * b2
r,g,b
let well x = 1.0 - 2.0 / (1.0 + x * x) ** 8.0
let tent x = 1.0 - 2.0 * abs x
let rec eval = function
| VariableX -> fun (x,y) -> (x,x,x)
| VariableY -> fun (x,y) -> (y,y,y)
| Constant ->
let r,g,b = next(),next(),next()
fun (x,y) -> (r,g,b)
| Sum(e1,e2) ->
let f1,f2 = eval e1, eval e2
fun (x,y) ->
average(f1(x,y),f2(x,y),0.5)
| Product(e1,e2) ->
let f1,f2 = eval e1, eval e2
fun (x,y) ->
let r1,g1,b1 = f1(x,y)
let r2,g2,b2 = f2(x,y)
r1*r2, g1*g2, b1*b2
| Mod(e1,e2) ->
let f1,f2 = eval e1, eval e2
fun (x,y) ->
let r1,g1,b1 = f1(x,y)
let r2,g2,b2 = f2(x,y)
r1 % r2, g1 % g2, b1 % b2
| Well(e) ->
let f = eval e
fun (x,y) ->
let r,g,b = f(x,y)
well r, well g, well b
| Tent(e) ->
let f = eval e
fun (x,y) ->
let r,g,b = f(x,y)
tent r, tent g, tent b
| Sin(e) ->
let f = eval e
let phase = next() * System.Math.PI
let freq = (next()*5.0)+1.0
fun (x,y) ->
let r,g,b = f(x,y)
sin(phase + r*freq),sin(phase+g*freq),sin (phase+b*freq)
| Level(e1,e2,e3) ->
let f1,f2,f3 = eval e1, eval e2, eval e3
let threshold = (next()*2.0) - 1.0
fun (x,y) ->
let r1,g1,b1 = f1(x,y)
let r2,g2,b2 = f2(x,y)
let r3,g3,b3 = f3(x,y)
let r = if r1 < threshold then r2 else r3
let g = if g1 < threshold then g2 else g3
let b = if b1 < threshold then b2 else b3
r,g,b
| Mix(e1,e2,e3) ->
let f1,f2,f3 = eval e1, eval e2, eval e3
let threshold = (next()*2.0) - 1.0
fun (x,y) ->
let n, _, _ = f1(x,y)
let w = 0.5 * (n + 1.0)
let c1 = f2(x,y)
let c2 = f3(x,y)
average(c1,c2,w)
let rec gen k =
if k <= 0 || next() < 0.01 then
let terminals = [VariableX; VariableY;Constant]
terminals.[random.Next(terminals.Length)]
else
let n () = random.Next(k)
let operators = [
fun () -> Sum(gen (n()), gen(n()))
fun () -> Product(gen (n()), gen(n()))
fun () -> Mod(gen (n()), gen(n()))
fun () -> Well(gen (n()))
fun () -> Tent(gen (n()))
fun () -> Sin(gen (n()))
fun () -> Level(gen (n()), gen (n()), gen(n()))
fun () -> Mix(gen (n()), gen (n()), gen(n()))
]
operators.[random.Next(operators.Length)]()
#if INTERACTIVE
#r "System.Drawing.dll"
#endif
let rgb (r,g,b) =
let r = max 0 (min 255 (int (128.0 * (r + 1.0))))
let g = max 0 (min 255 (int (128.0 * (g + 1.0))))
let b = max 0 (min 255 (int (128.0 * (b + 1.0))))
r,g,b
let width, height = 512, 384
open System
open System.Drawing
let draw f n =
let image = new Bitmap(width, height)
use graphics = Graphics.FromImage(image)
[|for y in 0..n..height-n do
for x in 0..n..width-n -> x,y
|]
|> Array.Parallel.map (fun (x,y) ->
let x' = -1.0 + (((float x+(float n/2.0))*2.0)/float width)
let y' = -1.0 + (((float y+(float n/2.0))*2.0)/float height)
let r,g,b = f(x',y')
let r,g,b = rgb(r,g,b)
x,y,r,g,b
)
|> Array.iter (fun (x,y,r,g,b) ->
use pen = new SolidBrush(Color.FromArgb(r,g,b))
graphics.FillRectangle(pen, x, y, n, n)
)
image
let show n =
let e = gen 50
let f = eval e
let image = draw f 1
image.Save(sprintf @"C:\temp\Random%04d.png" n, Imaging.ImageFormat.Png)
for i = 1 to 1000 do show i
|
union case expr.VariableX: expr
union case expr.VariableY: expr
union case expr.Constant: expr
union case expr.Sum: expr * expr -> expr
type expr =
| VariableX
| VariableY
| Constant
| Sum of expr * expr
| Product of expr * expr
| Mod of expr * expr
| Well of expr
| Tent of expr
| Sin of expr
| Level of expr * expr * expr
...
Full name: Script.expr
union case expr.Product: expr * expr -> expr
union case expr.Mod: expr * expr -> expr
union case expr.Well: expr -> expr
union case expr.Tent: expr -> expr
union case expr.Sin: expr -> expr
union case expr.Level: expr * expr * expr -> expr
union case expr.Mix: expr * expr * expr -> expr
val random : System.Random
Full name: Script.random
namespace System
Multiple items
type Random =
new : unit -> Random + 1 overload
member Next : unit -> int + 2 overloads
member NextBytes : buffer:byte[] -> unit
member NextDouble : unit -> float
Full name: System.Random
--------------------
System.Random() : unit
System.Random(Seed: int) : unit
val next : unit -> float
Full name: Script.next
System.Random.NextDouble() : float
val average : c1:(float * float * float) * c2:(float * float * float) * w:float -> float * float * float
Full name: Script.average
val c1 : float * float * float
val c2 : float * float * float
val w : float
val r1 : float
val g1 : float
val b1 : float
val r2 : float
val g2 : float
val b2 : float
val r : float
val g : float
val b : float
val well : x:float -> float
Full name: Script.well
val x : float
val tent : x:float -> float
Full name: Script.tent
val abs : value:'T -> 'T (requires member Abs)
Full name: Microsoft.FSharp.Core.Operators.abs
val eval : _arg1:expr -> (float * float -> float * float * float)
Full name: Script.eval
val y : float
val e1 : expr
val e2 : expr
val f1 : (float * float -> float * float * float)
val f2 : (float * float -> float * float * float)
val e : expr
val f : (float * float -> float * float * float)
val phase : 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 System.Math.PI = 3.14159265359
val freq : float
val sin : value:'T -> 'T (requires member Sin)
Full name: Microsoft.FSharp.Core.Operators.sin
val e3 : expr
val f3 : (float * float -> float * float * float)
val threshold : float
val r3 : float
val g3 : float
val b3 : float
val n : float
val gen : k:int -> expr
Full name: Script.gen
val k : int
val terminals : expr list
System.Random.Next() : int
System.Random.Next(maxValue: int) : int
System.Random.Next(minValue: int, maxValue: int) : int
property List.Length: int
val n : (unit -> int)
val operators : (unit -> expr) list
val rgb : r:float * g:float * b:float -> int * int * int
Full name: Script.rgb
val r : int
val max : e1:'T -> e2:'T -> 'T (requires comparison)
Full name: Microsoft.FSharp.Core.Operators.max
val min : e1:'T -> e2:'T -> 'T (requires comparison)
Full name: Microsoft.FSharp.Core.Operators.min
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val g : int
val b : int
val width : int
Full name: Script.width
val height : int
Full name: Script.height
namespace System.Drawing
val draw : f:(float * float -> float * float * float) -> n:int -> Bitmap
Full name: Script.draw
val n : int
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)
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
Graphics.FromImage(image: Image) : Graphics
val y : int
val x : int
type Array =
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...
Full name: System.Array
module Parallel
from Microsoft.FSharp.Collections.ArrayModule
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []
Full name: Microsoft.FSharp.Collections.ArrayModule.Parallel.map
val x' : float
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
val iter : action:('T -> unit) -> array:'T [] -> unit
Full name: Microsoft.FSharp.Collections.Array.iter
val pen : 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
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
Color.FromArgb(argb: int) : Color
Color.FromArgb(alpha: int, baseColor: Color) : Color
Color.FromArgb(red: int, green: int, blue: int) : Color
Color.FromArgb(alpha: int, red: int, green: int, blue: int) : 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 : n:int -> unit
Full name: Script.show
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
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
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
val i : int
More information