0 people like it.

Complex 9 - Draw Sets

  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: 
148: 
149: 
150: 
151: 
152: 
153: 
154: 
155: 
156: 
157: 
158: 
159: 
160: 
161: 
162: 
163: 
164: 
165: 
166: 
167: 
168: 
169: 
170: 
171: 
172: 
173: 
174: 
175: 
176: 
177: 
178: 
179: 
180: 
181: 
182: 
183: 
184: 
185: 
186: 
187: 
188: 
189: 
190: 
191: 
192: 
193: 
194: 
195: 
196: 
197: 
198: 
199: 
200: 
201: 
202: 
203: 
204: 
205: 
206: 
207: 
208: 
209: 
210: 
211: 
212: 
213: 
214: 
215: 
216: 
217: 
218: 
219: 
220: 
221: 
222: 
223: 
224: 
225: 
226: 
227: 
228: 
229: 
230: 
231: 
232: 
233: 
234: 
235: 
236: 
237: 
238: 
239: 
240: 
241: 
242: 
243: 
244: 
245: 
246: 
247: 
248: 
249: 
250: 
251: 
252: 
253: 
254: 
255: 
256: 
257: 
258: 
259: 
260: 
261: 
262: 
// Part 1. - Complex Basics
// Part 2. - Graphics Basics
// Part 3. - Drawing Complex Sets

// Import Try F# compatibility (enable for Visual Studio)
// #load "Offline.fsx"

////////////////////////////////////////
// Part 1. - Complex Basics

// Define complex type with some operators
type Complex =
    { Re : float;
      Im : float }
    static member (+) (z1, z2) = 
        { Re = z1.Re + z2.Re; 
          Im = z1.Im + z2.Im }
    static member (-) (z1, z2) = 
        { Re = z1.Re - z2.Re; 
          Im = z1.Im - z2.Im }
    static member (*) (z1, z2) = 
        { Re = ((z1.Re * z2.Re) - (z1.Im * z2.Im));
          Im = ((z1.Re * z2.Im) + (z1.Im * z2.Re)) }
    static member (/) (z1, z2) = 
        let z2_conj = {Re = z2.Re; Im = -z2.Im}
        let den = (z2 * z2_conj).Re
        let num = z1 * z2_conj
        { Re = num.Re / den;
          Im = num.Im / den }
    static member (~-) z = 
        { Re = -z.Re; 
          Im = -z.Im };;

// .. and printing
let print z = printfn "%.3f%+.3fi" z.Re z.Im;;
let sprint z = sprintf "%.3f%+.3fi" z.Re z.Im;;

// .. and the conjugate
let conj z = 
    { Re = z.Re; 
      Im = -z.Im };;

// ... and the modulus (absolute value)
let abs z =
    sqrt (z.Re * z.Re + z.Im * z.Im);;

// ... and the argument (actually this is the principal value of the argument (Arg)
let arg z = 
    atan2 z.Im z.Re;;

// Polar form of complex number
type ComplexPolar = 
    { Mag : float;
      Arg : float };;

// ... with conversion to and from the polar form
let toPolar z = 
    { Mag = abs z;
      Arg = arg z };;

let fromPolar zp = 
    { Re = zp.Mag * (cos zp.Arg);
      Im = zp.Mag * (sin zp.Arg) };;

// ... and define printing of the polar form
let printp zp = 
    printfn "%.1f(cos %.3f + i sin %.3f)" zp.Mag zp.Arg zp.Arg;;

// Get list of angles used for roots
let rootAngles theta n =
    let pi = atan2 0.0 -1.0
    let kList = [0 .. (n-1)]
    let angles = List.map (fun k -> (theta + 2.0 * (float k) * pi) / (float n)) kList
    let anglesModPi = List.map (fun angle -> angle % (2.0 * pi)) angles
    let anglesSorted = List.sort anglesModPi
    anglesSorted;;

// Find roots
let nthRootsPolar n z = 
    let zp = toPolar z
    let angles = rootAngles zp.Arg n
    let mag = System.Math.Pow(zp.Mag, (1.0 / (float n)))
    List.map (fun angle -> {Mag = mag; Arg = angle}) angles;;

// ... one way to convert a list from polar
let fromPolarList polars = 
    List.map fromPolar polars;;

// ... another way...
// send (pipe) the output from the nthRootsPolar 
// to a list conversion
let nthRoots n z = 
    nthRootsPolar n z
    |> List.map fromPolar;;


// Define some standard complex numbers
let zero = {Re = 0.0; Im = 0.0}
let one = {Re=1.0; Im = 0.0}
let i = {Re=0.0; Im = 1.0}

////////////////////////////////////////
// Part 2. - Graphics Basics
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Media.Imaging
open Microsoft.TryFSharp

type Graphic = 
    { Canvas : Canvas;
      Size : float;
      Scale : float}
 
let makeCanvas (canvas : Canvas) size =
    let initSize = 250.0
    canvas.HorizontalAlignment <- HorizontalAlignment.Center
    canvas.VerticalAlignment <- VerticalAlignment.Center
    let scale = ScaleTransform()
    scale.ScaleX <- initSize / size
    scale.ScaleY <- -initSize / size
    canvas.RenderTransform <- scale
    { Canvas = canvas; Size = size; Scale = 2.0 * size / initSize}

let drawLine canvas brush (x1, y1) (x2, y2) = 
    let line = Shapes.Line()
    line.X1 <- x1
    line.Y1 <- y1
    line.X2 <- x2
    line.Y2 <- y2
    line.Stroke <- brush
    line.StrokeThickness <- 1.0 * canvas.Scale
    canvas.Canvas.Children.Add line |> ignore

let drawDot canvas fill (x, y) = 
    let dot = Shapes.Ellipse()
    dot.HorizontalAlignment <- HorizontalAlignment.Center
    dot.VerticalAlignment <- VerticalAlignment.Center
    dot.Height <- 2.0 * canvas.Scale
    dot.Width <- dot.Height
    dot.Fill <- fill
    dot.StrokeThickness <- 0.0
    Canvas.SetLeft(dot, x - dot.Width / 2.0)
    Canvas.SetTop(dot, y - dot.Width / 2.0)
    canvas.Canvas.Children.Add dot |> ignore

let drawDots canvas fill positions = 
    let grp = GeometryGroup()
    grp.FillRule <- FillRule.Nonzero
    let addDot (x,y) = 
        let dot = EllipseGeometry()
        dot.Center <- Point(x,y)
        dot.RadiusX <- 0.1
        dot.RadiusY <- dot.RadiusX
        grp.Children.Add dot
    List.iter addDot positions
    let shape = Shapes.Path()
    shape.Data <- grp
    shape.Fill <- fill
    shape.HorizontalAlignment <- HorizontalAlignment.Center
    shape.VerticalAlignment <- VerticalAlignment.Center
    //Canvas.SetLeft(dot, x - dot.Width / 2.0)
    //Canvas.SetTop(dot, y - dot.Width / 2.0)
    canvas.Canvas.Children.Add shape |> ignore

let drawText canvas (x, y) text = 
    let tb = TextBlock()
    //tb.HorizontalAlignment <- HorizontalAlignment.Center
    //tb.VerticalAlignment <- VerticalAlignment.Center
    tb.Text <- text
    let scale = ScaleTransform()
    scale.ScaleX <- 1.0
    scale.ScaleY <- -1.0
    tb.RenderTransform <- scale
    tb.FontSize <- 10.0 * canvas.Scale
    Canvas.SetLeft(tb, x)
    Canvas.SetTop(tb, y)
    canvas.Canvas.Children.Add tb |> ignore

let drawAxes canvas tickUnit =
    let darkGray = SolidColorBrush(Colors.DarkGray)
    drawLine canvas darkGray (-canvas.Size, 0.0) (canvas.Size, 0.0)
    drawLine canvas darkGray (0.0, -canvas.Size) (0.0, canvas.Size)
    let ticks = [0.0 .. tickUnit .. canvas.Size] @ [-tickUnit .. -tickUnit .. -canvas.Size]
    let tickSize = tickUnit / 10.0
    List.iter (fun x -> drawLine canvas darkGray (x, -tickSize) (x, tickSize)) ticks
    List.iter (fun y -> drawLine canvas darkGray (-tickSize, y) (tickSize, y)) ticks
    
let drawBitmap canvas colorFunc =
    let getCoordinates =
        let step = canvas.Size * 2.0 / 100.0
        [ for x in -canvas.Size .. step .. canvas.Size do
            for y in -canvas.Size .. step .. canvas.Size do
                yield (x, y) ]
    List.iter (fun p -> drawDot canvas (colorFunc p) p) getCoordinates

let drawTestDot canvas fill =
    drawDot canvas fill (-canvas.Size, canvas.Size)

//
//let drawBitmap canvas colorFunc =
//    let setPixel (bm:WriteableBitmap) row col (alpha : byte) (r : byte) (g : byte) (b : byte) = 
//        let idx = row * bm.PixelWidth + col
//        let r1 = byte ((uint16 r) * (uint16 alpha) / (uint16 255))
//        let g1 = byte ((uint16 g) * (uint16 alpha) / (uint16 255))
//        let b1 = byte ((uint16 b) * (uint16 alpha) / (uint16 255))
//        Array.set bm.Pixels idx (((((int alpha) <<< 24) ||| ( int r1 <<< 16)) ||| (int g1 <<< 8)) ||| int b1)
//    let draw (bm:WriteableBitmap) x =
//        for i = 0 to 499 do
//            for j = 0 to 499 do
//                setPixel bm i j 255uy (byte i) (byte j) (200uy + x)
//        bm.Invalidate()
//    let bm = new WriteableBitmap(500, 500, 96.0, 96.0, PixelFormats.Bgr32, null )
//    let image = new Image()
//    image.Source <- bm
//    canvas.Canvas.Children.Add image


////////////////////////////////////////
// Part 3. - Drawing Complex Sets

let drawSet canvas fill pred =
    let dotStep = 0.05
    let dots = 
        [for x in -canvas.Size .. dotStep .. canvas.Size do
            for y in -canvas.Size .. dotStep .. canvas.Size do
                if pred {Re = x; Im = y} then yield (x,y) ]
    drawDots canvas fill dots

////////////////////////////
let set1 z =
    z.Re > 0.0  

let set2 z =
    (z.Im < 0.0) && (z.Re >= 0.0) && (abs z < 2.0)

// This is the exercise we did in class
// returns true or false, whether z s in the set or not
let set3 z =
    let four = {Re = 4.0; Im = 0.0}
    let z1 = z - one + (four * i) 
    let r1 = abs z1
    (z.Im < 0.0) && (1.0 < r1) && (r1 < 5.0) 

let main () =
    // Set up the canvas and define some colors
    let canvas = makeCanvas App.Console.Canvas 10.0
    let black = SolidColorBrush(Colors.Black)
    let red = SolidColorBrush(Colors.Red)
    let green = SolidColorBrush(Colors.Green)
    let blue = SolidColorBrush(Colors.Blue)
    let brown = SolidColorBrush(Colors.Brown)
    
    // drawSet canvas red set1
    // drawSet canvas blue set2
    drawSet canvas blue set3
    
    drawAxes canvas 1.0
    App.Console.CanvasPosition <- CanvasPosition.Alone

App.Dispatch (fun() -> main())
Complex.Re: float
Multiple items
val float : value:'T -> float (requires member op_Explicit)

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

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

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

Full name: Microsoft.FSharp.Core.float<_>
Complex.Im: float
val z1 : Complex
val z2 : Complex
val z2_conj : Complex
val den : float
val num : Complex
val z : Complex
val print : z:Complex -> unit

Full name: Script.print
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val sprint : z:Complex -> string

Full name: Script.sprint
val sprintf : format:Printf.StringFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val conj : z:Complex -> Complex

Full name: Script.conj
val abs : z:Complex -> float

Full name: Script.abs
val sqrt : value:'T -> 'U (requires member Sqrt)

Full name: Microsoft.FSharp.Core.Operators.sqrt
val arg : z:Complex -> float

Full name: Script.arg
val atan2 : y:'T1 -> x:'T1 -> 'T2 (requires member Atan2)

Full name: Microsoft.FSharp.Core.Operators.atan2
type ComplexPolar =
  {Mag: float;
   Arg: float;}

Full name: Script.ComplexPolar
ComplexPolar.Mag: float
ComplexPolar.Arg: float
val toPolar : z:Complex -> ComplexPolar

Full name: Script.toPolar
val fromPolar : zp:ComplexPolar -> Complex

Full name: Script.fromPolar
val zp : ComplexPolar
val cos : value:'T -> 'T (requires member Cos)

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

Full name: Microsoft.FSharp.Core.Operators.sin
val printp : zp:ComplexPolar -> unit

Full name: Script.printp
val rootAngles : theta:float -> n:int -> float list

Full name: Script.rootAngles
val theta : float
val n : int
val pi : float
val kList : int list
val angles : float list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
val k : int
val anglesModPi : float list
val angle : float
val anglesSorted : float list
val sort : list:'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sort
val nthRootsPolar : n:int -> z:Complex -> ComplexPolar list

Full name: Script.nthRootsPolar
val mag : float
namespace System
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
System.Math.Pow(x: float, y: float) : float
val fromPolarList : polars:ComplexPolar list -> Complex list

Full name: Script.fromPolarList
val polars : ComplexPolar list
val nthRoots : n:int -> z:Complex -> Complex list

Full name: Script.nthRoots
val zero : Complex

Full name: Script.zero
val one : Complex

Full name: Script.one
val i : Complex

Full name: Script.i
namespace System.Windows
namespace System.Media
namespace Microsoft
type Graphic =
  {Canvas: obj;
   Size: float;
   Scale: float;}

Full name: Script.Graphic
Graphic.Canvas: obj
Graphic.Size: 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<_>
Graphic.Scale: float
val makeCanvas : canvas:'a -> size:float -> Graphic

Full name: Script.makeCanvas
val canvas : 'a
val size : float
val initSize : float
val scale : obj
val drawLine : canvas:Graphic -> brush:'a -> x1:'b * y1:'c -> x2:'d * y2:'e -> unit

Full name: Script.drawLine
val canvas : Graphic
val brush : 'a
val x1 : 'b
val y1 : 'c
val x2 : 'd
val y2 : 'e
val line : obj
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val drawDot : canvas:Graphic -> fill:'a -> x:'b * y:'c -> unit

Full name: Script.drawDot
val fill : 'a
val x : 'b
val y : 'c
val dot : obj
val drawDots : canvas:Graphic -> fill:'a -> positions:('b * 'c) list -> unit

Full name: Script.drawDots
val positions : ('b * 'c) list
val grp : obj
val addDot : ('d * 'e -> 'f)
val x : 'd
val y : 'e
val iter : action:('T -> unit) -> list:'T list -> unit

Full name: Microsoft.FSharp.Collections.List.iter
val shape : obj
Multiple items
namespace System.Data

--------------------
namespace Microsoft.FSharp.Data
val drawText : canvas:Graphic -> x:'a * y:'b -> text:'c -> unit

Full name: Script.drawText
val x : 'a
val y : 'b
val text : 'c
val tb : obj
namespace System.Text
val drawAxes : canvas:Graphic -> tickUnit:float -> unit

Full name: Script.drawAxes
val tickUnit : float
val darkGray : obj
val ticks : float list
val tickSize : float
val x : float
val y : float
val drawBitmap : canvas:Graphic -> colorFunc:(float * float -> 'a) -> unit

Full name: Script.drawBitmap
val colorFunc : (float * float -> 'a)
val getCoordinates : (float * float) list
val step : float
val p : float * float
val drawTestDot : canvas:Graphic -> fill:'a -> unit

Full name: Script.drawTestDot
val drawSet : canvas:Graphic -> fill:'a -> pred:(Complex -> bool) -> unit

Full name: Script.drawSet
val pred : (Complex -> bool)
val dotStep : float
val dots : (float * float) list
val set1 : z:Complex -> bool

Full name: Script.set1
val set2 : z:Complex -> bool

Full name: Script.set2
val set3 : z:Complex -> bool

Full name: Script.set3
val four : Complex
val r1 : float
val main : unit -> 'a

Full name: Script.main
type Console =
  static member BackgroundColor : ConsoleColor with get, set
  static member Beep : unit -> unit + 1 overload
  static member BufferHeight : int with get, set
  static member BufferWidth : int with get, set
  static member CapsLock : bool
  static member Clear : unit -> unit
  static member CursorLeft : int with get, set
  static member CursorSize : int with get, set
  static member CursorTop : int with get, set
  static member CursorVisible : bool with get, set
  ...

Full name: System.Console
val black : obj
val red : obj
val green : obj
val blue : obj
val brown : obj
Raw view Test code New version

More information

Link:http://fssnip.net/6T
Posted:14 years ago
Author:
Tags: