Bitmap/Bézier curves/Cubic: Difference between revisions

Content added Content deleted
(Added PicoLisp)
(Add F# version)
Line 183: Line 183:
#undef plot
#undef plot
#undef line</lang>
#undef line</lang>

=={{header|F#}}==
<lang f#>
/// Uses Vector<float> from Microsoft.FSharp.Math (in F# PowerPack)
module CubicBezier

/// Create bezier curve from p1 to p4, using the control points p2, p3
/// Returns the requested number of segments
let cubic_bezier (p1:vector) (p2:vector) (p3:vector) (p4:vector) segments =
[0 .. segments - 1]
|> List.map(fun i ->
let t = float i / float segments
let a = (1. - t) ** 3.
let b = 3. * t * ((1. - t) ** 2.)
let c = 3. * (t ** 2.) * (1. - t)
let d = t ** 3.
let x = a * p1.[0] + b * p2.[0] + c * p3.[0] + d * p4.[0]
let y = a * p1.[1] + b * p2.[1] + c * p3.[1] + d * p4.[1]
vector [x; y])

</lang>
<lang f#>
// For rendering..
let drawPoints points (canvas:System.Windows.Controls.Canvas) =
let addLineToScreen (v1:vector) (v2:vector) =
canvas.Children.Add(new System.Windows.Shapes.Line(X1 = v1.[0],
Y1 = -v1.[1],
X2 = v2.[0],
Y2 = -v2.[1],
StrokeThickness = 2.)) |> ignore
let renderPoint (previous:vector) (current:vector) =
addLineToScreen previous current
current

points |> List.fold renderPoint points.Head
</lang>

=={{header|Factor}}==
=={{header|Factor}}==
The points should probably be in a sequence...
The points should probably be in a sequence...