
-----------------------------------
Catalyst
Tue Mar 02, 2004 9:17 pm

[source] Bezier Curve
-----------------------------------
Implementation of a 4-point Bezier Curve
Translated (and simplified) from my c++ implementation


View.Set ("offscreenonly,nobuttonbar,graphics:512;512")

type Point2D :
    record
        x, y : real
    end record

var controlPoints : array 1 .. 4 of Point2D
var hold1, hold2, hold3 : real

controlPoints (1).x := 100
controlPoints (1).y := maxy div 2

controlPoints (2).x := 200
controlPoints (2).y := maxy div 2 + 150


controlPoints (3).x := 300
controlPoints (3).y := maxy div 2 - 150

controlPoints (4).x := 400
controlPoints (4).y := maxy div 2

var holdPoint : Point2D

fcn PointOnCurve (t : real) : Point2D%% Percentage along Curve : 0 to 1 

    hold1 := 1 - t
    hold2 := hold1 * hold1 * hold1
    hold3 := t * t * t

    holdPoint.x := controlPoints (1).x * hold2
    holdPoint.y := controlPoints (1).y * hold2

    holdPoint.x += controlPoints (2).x * (3 * t * hold1 * hold1)
    holdPoint.y += controlPoints (2).y * (3 * t * hold1 * hold1)

    holdPoint.x += controlPoints (3).x * (3 * t * t * hold1)
    holdPoint.y += controlPoints (3).y * (3 * t * t * hold1)

    holdPoint.x += controlPoints (4).x * hold3
    holdPoint.y += controlPoints (4).y * hold3

    result holdPoint

end PointOnCurve

var hold, holdNext : Point2D := PointOnCurve (0)
proc DrawCurve
    holdNext := PointOnCurve (0)

    for i : 1 .. 3
        drawline (round (controlPoints (i).x), round (controlPoints (i).y), round (controlPoints (i + 1).x), round (controlPoints (i + 1).y), 22)
    end for

    for i : 1 .. 100
        hold := holdNext
        holdNext := PointOnCurve ((i + 1) / 100)
        drawline (round (hold.x), round (hold.y), round (holdNext.x), round (holdNext.y), 42)
    end for

    for i : 1 .. 4
        drawoval (round (controlPoints (i).x), round (controlPoints (i).y), 5, 5, 103)

    end for
end DrawCurve

var x, y, z : int

loop
    mousewhere (x, y, z)
    DrawCurve
    for i : 1 .. 4
        if sqrt ((controlPoints (i).x - x) * (controlPoints (i).x - x) + (controlPoints (i).y - y) * (controlPoints (i).y - y)) 