# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #1 2021-06-01 05:53:58

maras
Member
Registered: 2021-04-25
Posts: 28
Website

### draw_curve_cb (cubic bezier)

Expand/// draw_curve_cb(p1, p2, p3, p4, prec)
//
//    p1    start point, array [x, y]
//    p2    curvature point 1, array [x, y]
//    p3    curvature point 2, array [x, y]
//    p4    end point, array [x, y]
//    prec     precision, number of segments, real
//

function draw_curve_cb(p1, p2, p3, p4, prec) {

prec = round(prec);
var prev_cb = p1;
for(var i = 0; i < prec; i++) {
var cb = cubic_bezier(p1, p2, p3, p4, i / prec);
draw_line(cb[0], cb[1], prev_cb[0], prev_cb[1]);
prev_cb = cb;
}
draw_line(prev_cb[0], prev_cb[1], p4[0], p4[1]);
}

/// cubic_bezier(p1, p2, p3, p4, t)
//
// returns array [x, y], a single point on the curve depending on t
//
//    p1    start point, array [x, y]
//    p2    curvature point 1, array [x, y]
//    p3    curvature point 2, array [x, y]
//    p4    end point, array [x, y]
//    t     curve position, real
//

function cubic_bezier(p1, p2, p3, p4, t) {
t = clamp(t, 0, 1);
return [
p1[0] * power(1-t, 3) + 3 * p2[0] * power(1-t, 2) * t + 3 * p3[0] * (1-t) * power(t, 2) + p4[0] * power(t, 3),
p1[1] * power(1-t, 3) + 3 * p2[1] * power(1-t, 2) * t + 3 * p3[1] * (1-t) * power(t, 2) + p4[1] * power(t, 3)
];
}

Last edited by maras (2022-12-27 07:42:01)

I'm on the official GM discord > maras_cz

Offline