As Tomas said you can use scaling transformations. If you want to draw a circle using small curves you can use multiple DrawCurve
calls :)
I've changed a bit jon's code for that purpose:
- used
System.Drawing.Point
type instead of your record
- modified
unit_circle
so that it returns a tuple representing coordinates x and y
- turned your list of angles into a sequence of sequence of angles. This will be useful since we can have a variable number of knots for our curve (a cardinal spline), represented by the constant
N
implemented a splitRepeatEvery
method, e.g:
Seq.splitRepeatEvery 3 { 1 .. 10 }
returns
seq [seq [1; 2; 3]; seq [3; 4; 5]; seq [5; 6; 7]; seq [7; 8; 9]; seq [9; 10]]
Here's the code:
module Seq =
/// Split a sequence into pieces, each n items long
/// repeating elements between start and end of subsequences.
let splitRepeatEvery (count : int) (source : seq<'T>) =
if not (count > 1) then failwith "count must be superior to 1"
seq { use e = source.GetEnumerator()
let hasNext = ref (e.MoveNext())
while !hasNext do
let (block:option<'T>[]) = Array.create count None
for i in 0 .. count - 1 do
do block.[i] <- if !hasNext then Some(e.Current) else None
if (i <> count - 1) then do hasNext := e.MoveNext()
yield seq { yield! block }
|> Seq.filter (fun x -> x.IsSome)
|> Seq.map (function Some(e) -> e | _ -> failwith "" ) }
let unit_circle (angle : float) =
(sin <| 2.0 * Math.PI * angle), (cos <| 2.0 * Math.PI * angle)
let draw_connected curve radius (seqOfAngles : float seq seq) knotsCount =
let form = new Form(Text = "Curve")
let computeBoundingBox points =
let search f acc array =
Array.fold (fun (x,y) (p:Point) -> f p.X x, f p.Y y) acc array
let minX, minY = search min (form.ClientSize.Width, form.ClientSize.Height) points
let maxX, maxY = search max (0,0) points
new Rectangle(minX, minY, abs(minX-maxX), abs(minY-maxY))
let drawCurves (gfx : Graphics) =
// Create a buffer for storing our knots
let buffer = Array.create knotsCount (new Point())
let mutable i = 0
for angles in seqOfAngles do
for angle in angles do
let x, y = curve angle
let X = int(x * radius + (float)form.ClientSize.Width / 2.0)
let Y = int(y * radius + (float)form.ClientSize.Height / 2.0)
let P = new Point(X, Y)
buffer.[i] <- P
i <- i + 1
let knots = buffer.[0..(i-1)]
// Draw spline only if we have one or more knots
if knots.Length <> 1 then
gfx.DrawCurve(Pens.Red, knots)
// For debug: compute BBox of an array of points and draw it
let debugRect = computeBoundingBox knots
gfx.DrawRectangle(Pens.Black, debugRect)
// Don't forget to reset position in buffer between each spline draw call
i <- 0
form.Paint.Add (fun pntEvntArgs -> drawCurves pntEvntArgs.Graphics)
form.Show ()
form
// Define constants
let STEP = 0.050
let N = 4
// Define a new sequence of sequence of angles
let s = {0.0 .. STEP .. 1.0} |> Seq.splitRepeatEvery N
let form = draw_connected unit_circle 120.0 s N
// For debug: print sequence of sequence of angles
s |> Seq.iter (fun s -> Seq.iter (fun x -> printf "%f " x) s; printfn "")
while form.Created do
Thread.Sleep (1)
Application.DoEvents ()
done
You can play with different values of N
(number of knots for the splines) and STEP
(but beware STEP
should be choosen so that 1.0f is a multiple of STEP
or a floating point number so that last element of last sequence is close enough to 1.0f else the last spline won't connect to the first one!). And voila!