Skip to content

Instantly share code, notes, and snippets.

@katsaii
Last active November 1, 2020 18:05
Show Gist options
  • Save katsaii/874ef503cc5a476c73b1a2ac7fc60d3b to your computer and use it in GitHub Desktop.
Save katsaii/874ef503cc5a476c73b1a2ac7fc60d3b to your computer and use it in GitHub Desktop.
A function which lets you draw bezier cuves in GameMaker.
/// @desc Draws a bezier curve with 3 control points.
/// @param x1 {Real} The X coordinate of the first control point.
/// @param y1 {Real} The Y coordinate of the first control point.
/// @param x2 {Real} The X coordinate of the second control point.
/// @param y2 {Real} The Y coordinate of the second control point.
/// @param x3 {Real} The X coordinate of the third control point.
/// @param y3 {Real} The Y coordinate of the third control point.
/// @author Kat @katsaii
var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var x3 = argument4;
var y3 = argument5;
var step = 0.1;
draw_primitive_begin(pr_linestrip);
draw_vertex(x1, y1);
for (var i = 0; i <= 1; i += step) {
// get intermediate coordinates
var ix = lerp(x1, x2, i);
var iy = lerp(y1, y2, i);
var jx = lerp(x2, x3, i);
var jy = lerp(y2, y3, i);
// get final curve point
var bx = lerp(ix, jx, i);
var by = lerp(iy, jy, i);
draw_vertex(bx, by);
}
draw_vertex(x3, y3);
draw_primitive_end();
@net8floz
Copy link

net8floz commented Nov 1, 2020

excellent.

@katsaii
Copy link
Author

katsaii commented Nov 1, 2020

For 4 control points:

/// @desc Draws a bezier curve with 4 control points.
/// @param x1 {Real} The X coordinate of the first control point.
/// @param y1 {Real} The Y coordinate of the first control point.
/// @param x2 {Real} The X coordinate of the second control point.
/// @param y2 {Real} The Y coordinate of the second control point.
/// @param x3 {Real} The X coordinate of the third control point.
/// @param y3 {Real} The Y coordinate of the third control point.
/// @param x4 {Real} The X coordinate of the fourth control point.
/// @param y4 {Real} The Y coordinate of the fourth control point.
/// @author Kat @katsaii
var x1 = argument0;
var y1 = argument1;
var x2 = argument2;
var y2 = argument3;
var x3 = argument4;
var y3 = argument5;
var x4 = argument6;
var y4 = argument7;
var step = 0.1;
draw_primitive_begin(pr_linestrip);
draw_vertex(x1, y1);
for (var i = 0; i <= 1; i += step) {
    // get intermediate coordinates
    var ix = lerp(x1, x2, i);
    var iy = lerp(y1, y2, i);
    var jx = lerp(x2, x3, i);
    var jy = lerp(y2, y3, i);
    var kx = lerp(x3, x4, i);
    var ky = lerp(y3, y4, i);
    // get further intermediate coordinates
    var iix = lerp(ix, jx, i);
    var iiy = lerp(iy, jy, i);
    var jjx = lerp(jx, kx, i);
    var jjy = lerp(jy, ky, i);
    // get final curve point
    var bx = lerp(iix, jjx, i);
    var by = lerp(iiy, jjy, i);
    draw_vertex(bx, by);
}
draw_vertex(x4, y4);
draw_primitive_end();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment