Deprecated: Function get_magic_quotes_runtime() is deprecated in /home/public/forums/include/common.php on line 79

Deprecated: Function get_magic_quotes_gpc() is deprecated in /home/public/forums/include/common.php on line 83
cubic_bezier - Draw a curved line / Script Submission / GMLscripts.com

# 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: 25
Website

### cubic_bezier - Draw a curved line

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

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)
];
}

Example code

ExpandCREATE:
points = [[x,y], [x+50,y+100], [x+100, y-100], [x+150, y]];
precision = 20; // smoothness of the line

DRAW:
var prev_cb = points[0]; // start point
for(var i = 0; i < precision; i++)
{
var cb = cubic_bezier(points[0], points[1], points[2], points[3], i / precision);
draw_line(cb[0], cb[1], prev_cb[0], prev_cb[1]); // connect points between
prev_cb = cb;
}
draw_line(prev_cb[0], prev_cb[1], points[3][0], points[3][1]); // connect end point

Last edited by maras (2021-06-08 07:24:41)

I'm on the official GM discord > maras#5104

Offline