12 people like it.

Sierpinski triangle, WPF

Draws a Sierpinski triangle using WPF

 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: 
// Requires reference to 
// PresentationCore, PresentationFramework, 
// System.Windows.Presentation, System.Xaml, WindowsBase

open System
open System.Windows
open System.Windows.Media
open System.Windows.Shapes
open System.Windows.Controls

type Vector = 
   { dX:float; dY:float }
   static member (+) (v1, v2) = { dX = v1.dX + v2.dX; dY = v1.dY + v2.dY }
   static member (*) (sc, v) = { dX = sc * v.dX; dY = sc * v.dY }

type Point = 
   { X:float; Y:float }
   static member (+) (p, v) = { X = p.X + v.dX; Y = p.Y + v.dY }
   static member (-) (p2, p1) = { dX = p2.X - p1.X; dY = p2.Y - p1.Y }

type Triangle = { A:Point; B:Point; C:Point }

let transform (p1:Point, p2, p3) =
   let a = p1 + 0.5 * (p2 - p1) + 0.5 * (p3 - p1)
   let b = p1 + 1.0 * (p2 - p1) + 0.5 * (p3 - p1)
   let c = p1 + 0.5 * (p2 - p1) + 1.0 * (p3 - p1)
   { A = a; B = b; C = c }

let generateFrom triangle = seq {
      yield transform (triangle.A, triangle.B, triangle.C)
      yield transform (triangle.B, triangle.C, triangle.A)
      yield transform (triangle.C, triangle.A, triangle.B)
   }

let nextGeneration triangles =
   Seq.collect generateFrom triangles 
      
let render (target:Canvas) (brush:Brush) triangle =
   let points = new PointCollection()
   points.Add(new System.Windows.Point(triangle.A.X, triangle.A.Y))
   points.Add(new System.Windows.Point(triangle.B.X, triangle.B.Y))
   points.Add(new System.Windows.Point(triangle.C.X, triangle.C.Y))
   let polygon = new Polygon()
   polygon.Points <- points
   polygon.Fill <- brush
   target.Children.Add(polygon) |> ignore
   
let win = new Window()
let canvas = new Canvas()
canvas.Background <- Brushes.White
let brush = new SolidColorBrush(Colors.Black)
brush.Opacity <- 1.0
let renderTriangle = render canvas brush

let triangle = 
    let p1 = { X = 190.0; Y = 170.0 }
    let p2 = { X = 410.0; Y = 210.0}
    let p3 = { X = 220.0; Y = 360.0}
    { A = p1; B = p2; C = p3 }

let root = seq { yield triangle }
let generations = 
   Seq.unfold (fun state -> Some(state, (nextGeneration state))) root
   |> Seq.take 7
Seq.iter (fun gen -> Seq.iter renderTriangle gen) generations

win.Content <- canvas
win.Show()

[<STAThread()>]
do 
   let app =  new Application() in
   app.Run() |> ignore
namespace System
namespace System.Windows
namespace System.Media
type Vector =
  {dX: float;
   dY: float;}
  static member ( + ) : v1:Vector * v2:Vector -> Vector
  static member ( * ) : sc:float * v:Vector -> Vector

Full name: Script.Vector
Vector.dX: 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<_>
Vector.dY: float
val v1 : Vector
val v2 : Vector
val sc : float
val v : Vector
type Point =
  {X: float;
   Y: float;}
  static member ( + ) : p:Point * v:Vector -> Point
  static member ( - ) : p2:Point * p1:Point -> Vector

Full name: Script.Point
Point.X: float
Point.Y: float
val p : Point
val p2 : Point
val p1 : Point
type Triangle =
  {A: Point;
   B: Point;
   C: Point;}

Full name: Script.Triangle
Triangle.A: Point
Triangle.B: Point
Triangle.C: Point
val transform : p1:Point * p2:Point * p3:Point -> Triangle

Full name: Script.transform
val p3 : Point
val a : Point
val b : Point
val c : Point
val generateFrom : triangle:Triangle -> seq<Triangle>

Full name: Script.generateFrom
val triangle : Triangle
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val nextGeneration : triangles:seq<Triangle> -> seq<Triangle>

Full name: Script.nextGeneration
val triangles : seq<Triangle>
module Seq

from Microsoft.FSharp.Collections
val collect : mapping:('T -> #seq<'U>) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.collect
val render : target:'a -> brush:'b -> triangle:'c -> unit

Full name: Script.render
val target : 'a
val brush : 'b
val triangle : 'c
val points : obj
val polygon : obj
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val win : obj

Full name: Script.win
val canvas : obj

Full name: Script.canvas
val brush : obj

Full name: Script.brush
val renderTriangle : (Triangle -> unit)

Full name: Script.renderTriangle
val triangle : Triangle

Full name: Script.triangle
val root : seq<Triangle>

Full name: Script.root
val generations : seq<seq<Triangle>>

Full name: Script.generations
val unfold : generator:('State -> ('T * 'State) option) -> state:'State -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.unfold
val state : seq<Triangle>
union case Option.Some: Value: 'T -> Option<'T>
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val iter : action:('T -> unit) -> source:seq<'T> -> unit

Full name: Microsoft.FSharp.Collections.Seq.iter
val gen : seq<Triangle>
Multiple items
type STAThreadAttribute =
  inherit Attribute
  new : unit -> STAThreadAttribute

Full name: System.STAThreadAttribute

--------------------
STAThreadAttribute() : unit
val app : obj

More information

Link:http://fssnip.net/ba
Posted:12 years ago
Author:Mathias Brandewinder
Tags: wpf , sierpinski , sequences , geometry , fun