This script draws bezier curves given an x, y, ds_list, and precision. The ds_list should be structured like [x0, y0, x1, y1, ...] and the script will draw multiple curves if there are more than four coordinates specified.
//bezier_curve_draw(x,y,list,prec);
//by jsorgeagames
var i, c, t, x0, y0, x1, y1, x2, y2, x3, y3, xx, yy;
draw_primitive_begin(pr_linestrip);
i = 0;
c = 0;
while(i < ds_list_size(argument2))
{
c = i-2;
if i == 0
c = i;
x0 = ds_list_find_value(argument2,c);
y0 = ds_list_find_value(argument2,c+1);
x1 = ds_list_find_value(argument2,c+2);
y1 = ds_list_find_value(argument2,c+3);
x2 = ds_list_find_value(argument2,c+4);
y2 = ds_list_find_value(argument2,c+5);
x3 = ds_list_find_value(argument2,c+6);
y3 = ds_list_find_value(argument2,c+7);
//cubic
if c+7 < ds_list_size(argument2)
{
cx = 3*(x1-x0);
cy = 3*(y1-y0);
bx = 3*(x2-x1)-cx;
by = 3*(y2-y1)-cy;
ax = x3-x0-cx-bx;
ay = y3-y0-cy-by;
t = 0;
for(t = 0; t <= 1; t += 1/argument3)
{
xx = argument0+ax*power(t,3)+bx*sqr(t)+cx*t+x0;
yy = argument1+ay*power(t,3)+by*sqr(t)+cy*t+y0;
draw_vertex(xx,yy);
}
}
//quadratic
if c+6 == ds_list_size(argument2)
{
t = 0;
for(t = 0; t <= 1; t += 1/argument3)
{
ax = sqr(1-t);
bx = 2*(1-t)*t;
cx = sqr(t);
xx = ax*x0+bx*x1+cx*x2;
yy = ax*y0+bx*y1+cx*y2;
draw_vertex(xx,yy);
}
}
//linear
if c+4 == ds_list_size(argument2)
{
draw_vertex(x0,y0);
draw_vertex(x1,y1);
}
if i == 0
i += 8;
else
i += 6;
}
draw_primitive_end();
bezier_curve_find_pointThis script finds the point along a bezier curve given a ds_list, a curve id, and the position along the curve from 0 to 1 (t). The curve id is the index in the ds_list of the first coordinate of the curve.
//bezier_curve_find_point(list,listpos,t);
//by jsorgeagames
var c, t, x0, y0, x1, y1, x2, y2, x3, y3;
c = argument1;
t = argument2;
x0 = ds_list_find_value(argument0,c);
y0 = ds_list_find_value(argument0,c+1);
x1 = ds_list_find_value(argument0,c+2);
y1 = ds_list_find_value(argument0,c+3);
x2 = ds_list_find_value(argument0,c+4);
y2 = ds_list_find_value(argument0,c+5);
x3 = ds_list_find_value(argument0,c+6);
y3 = ds_list_find_value(argument0,c+7);
//cubic
if c+7 < ds_list_size(argument0)
{
cx = 3*(x1-x0);
cy = 3*(y1-y0);
bx = 3*(x2-x1)-cx;
by = 3*(y2-y1)-cy;
ax = x3-x0-cx-bx;
ay = y3-y0-cy-by;
bcurve_x = ax*power(t,3)+bx*sqr(t)+cx*t+x0;
bcurve_y = ay*power(t,3)+by*sqr(t)+cy*t+y0;
exit;
}
//quadratic
if c+6 == ds_list_size(argument0)
{
ax = sqr(1-t);
bx = 2*(1-t)*t;
cx = sqr(t);
bcurve_x = ax*x0+bx*x1+cx*x2;
bcurve_y = ax*y0+bx*y1+cx*y2;
exit;
}
//linear
bcurve_x = x0+t*(x1-x0);
bcurve_y = y0+t*(y1-y0);
Screenshots:
[ 1 ] [ 2 ]
Example:
beziercurves.gmk 11.5 kb
Edited by jsorgeagames, 24 March 2011 - 03:54 PM.











