Pixel-Composer/scripts/draw_connect_curve/draw_connect_curve.gml

161 lines
4.2 KiB
Text
Raw Normal View History

2023-12-20 14:02:49 +01:00
function draw_line_curve_color(x0, y0, x1, y1, xc = noone, yc = noone, _s = 1, thick = 1, col1 = c_white, col2 = c_white, type = LINE_STYLE.solid) { #region
2023-03-05 07:16:44 +01:00
if(xc == noone) xc = (x0 + x1) / 2;
if(yc == noone) yc = (y0 + y1) / 2;
2023-10-31 05:30:42 +01:00
var sample = ceil((abs(x0 - x1) + abs(y0 - y1)) / 32 * PREFERENCES.connection_line_sample);
2023-06-17 14:30:49 +02:00
sample = clamp(sample, 2, 128);
2023-12-19 14:30:34 +01:00
if(type == LINE_STYLE.dashed) sample *= 2;
2022-01-13 05:24:03 +01:00
2023-03-02 07:59:14 +01:00
var x2 = lerp(x0, x1, 0. - sign(x1 - x0) * 0.2) - abs(y1 - y0) * 0.1;
var x3 = lerp(x0, x1, 1. + sign(x1 - x0) * 0.2) + abs(y1 - y0) * 0.1;
var y2 = y0;
var y3 = y1;
var c = draw_get_color();
2022-01-13 05:24:03 +01:00
var ox, oy, nx, ny, t, it, oc, nc;
2023-07-23 20:21:35 +02:00
2022-01-13 05:24:03 +01:00
for( var i = 0; i <= sample; i++ ) {
t = i / sample;
it = 1 - t;
2023-03-02 07:59:14 +01:00
nx = x0 * power(t, 4)
+ 4 * x2 * power(it, 1) * power(t, 3)
+ 6 * xc * power(it, 2) * power(t, 2)
+ 4 * x3 * power(it, 3) * power(t, 1)
+ x1 * power(it, 4);
ny = y0 * power(t, 4)
+ 4 * y2 * power(it, 1) * power(t, 3)
+ 6 * yc * power(it, 2) * power(t, 2)
+ 4 * y3 * power(it, 3) * power(t, 1)
+ y1 * power(it, 4);
2022-01-13 05:24:03 +01:00
nc = merge_color(col1, col2, t);
if(i) {
2022-01-29 14:25:18 +01:00
switch(type) {
case LINE_STYLE.solid :
2023-07-23 20:21:35 +02:00
draw_line_round_color(ox, oy, nx, ny, thick, oc, nc, i == 1, i == sample);
2022-01-29 14:25:18 +01:00
break;
case LINE_STYLE.dashed :
2023-12-19 14:30:34 +01:00
if(floor(i % 2))
2023-03-05 07:16:44 +01:00
draw_line_round_color(ox, oy, nx, ny, thick, oc, nc);
2022-01-29 14:25:18 +01:00
break;
}
2022-01-13 05:24:03 +01:00
}
ox = nx;
oy = ny;
oc = nc;
}
2023-12-20 14:02:49 +01:00
} #endregion
2023-12-20 14:02:49 +01:00
function draw_line_curve_corner(x0, y0, x1, y1, _s = 1, thick = 1, col1 = c_white, col2 = c_white) { #region
2023-10-31 05:30:42 +01:00
var sample = ceil((abs(x0 - x1) + abs(y0 - y1)) / 32 * PREFERENCES.connection_line_sample);
2023-06-17 14:30:49 +02:00
sample = clamp(sample, 2, 128);
2023-03-28 06:58:28 +02:00
var x2 = lerp(x0, x1, 0.9);
var x3 = x1;
var y2 = lerp(y0, y1, 0.1);
var y3 = y1;
var c = draw_get_color();
var ox, oy, nx, ny, t, it, oc, nc;
for( var i = 0; i <= sample; i++ ) {
t = i / sample;
it = 1 - t;
nx = x0 * power(t, 3)
+ 3 * x2 * power(it, 1) * power(t, 2)
+ 3 * x3 * power(it, 2) * power(t, 1)
+ x1 * power(it, 3);
ny = y0 * power(t, 3)
+ 3 * y2 * power(it, 1) * power(t, 2)
+ 3 * y3 * power(it, 2) * power(t, 1)
+ y1 * power(it, 3);
nc = merge_color(col1, col2, t);
2023-07-23 20:21:35 +02:00
if(i) draw_line_round_color(ox, oy, nx, ny, thick, oc, nc, i == 1, i == sample);
2023-03-28 06:58:28 +02:00
ox = nx;
oy = ny;
oc = nc;
}
2023-12-20 14:02:49 +01:00
} #endregion
2023-03-28 06:58:28 +02:00
2023-12-20 14:02:49 +01:00
function distance_to_curve(mx, my, x0, y0, x1, y1, xc, yc, _s) { #region
2023-10-31 05:30:42 +01:00
var sample = ceil((abs(x0 - x1) + abs(y0 - y1)) / 32 * PREFERENCES.connection_line_sample);
2023-06-17 14:30:49 +02:00
sample = clamp(sample, 2, 128);
var dist = 999999;
var ox, oy, nx, ny, t, it;
2023-03-02 07:59:14 +01:00
var x2 = lerp(x0, x1, 0. - sign(x1 - x0) * 0.2);
var x3 = lerp(x0, x1, 1. + sign(x1 - x0) * 0.2);
var y2 = y0;
var y3 = y1;
for( var i = 0; i <= sample; i++ ) {
t = i / sample;
it = 1 - t;
2023-03-02 07:59:14 +01:00
nx = x0 * power(t, 4)
+ 4 * x2 * power(it, 1) * power(t, 3)
+ 6 * xc * power(it, 2) * power(t, 2)
+ 4 * x3 * power(it, 3) * power(t, 1)
+ x1 * power(it, 4);
ny = y0 * power(t, 4)
+ 4 * y2 * power(it, 1) * power(t, 3)
+ 6 * yc * power(it, 2) * power(t, 2)
+ 4 * y3 * power(it, 3) * power(t, 1)
+ y1 * power(it, 4);
if(i)
dist = min(dist, distance_to_line(mx, my, ox, oy, nx, ny));
ox = nx;
oy = ny;
}
2023-03-28 06:58:28 +02:00
return dist;
2023-12-20 14:02:49 +01:00
} #endregion
2023-03-28 06:58:28 +02:00
2023-12-20 14:02:49 +01:00
function distance_to_curve_corner(mx, my, x0, y0, x1, y1, _s) { #region
2023-10-31 05:30:42 +01:00
var sample = ceil((abs(x0 - x1) + abs(y0 - y1)) / 32 * PREFERENCES.connection_line_sample);
2023-06-17 14:30:49 +02:00
sample = clamp(sample, 2, 128);
2023-03-28 06:58:28 +02:00
var dist = 999999;
var ox, oy, nx, ny, t, it;
var x2 = lerp(x0, x1, 0.9);
var x3 = x1;
var y2 = lerp(y0, y1, 0.1);
var y3 = y1;
for( var i = 0; i <= sample; i++ ) {
t = i / sample;
it = 1 - t;
nx = x0 * power(t, 3)
+ 3 * x2 * power(it, 1) * power(t, 2)
+ 3 * x3 * power(it, 2) * power(t, 1)
+ x1 * power(it, 3);
ny = y0 * power(t, 3)
+ 3 * y2 * power(it, 1) * power(t, 2)
+ 3 * y3 * power(it, 2) * power(t, 1)
+ y1 * power(it, 3);
if(i)
dist = min(dist, distance_to_line(mx, my, ox, oy, nx, ny));
ox = nx;
oy = ny;
}
return dist;
2023-12-20 14:02:49 +01:00
} #endregion