Pixel-Composer/scripts/draw_line_curve/draw_line_curve.gml

202 lines
5.0 KiB
Plaintext
Raw Normal View History

2022-01-29 14:25:18 +01:00
enum LINE_STYLE {
solid,
dashed
}
2022-01-13 05:24:03 +01:00
function draw_line_curve(x0, y0, x1, y1, thick = 1) {
var xc = (x0 + x1) / 2;
var sample = max(8, ceil((abs(x0 - x1) + abs(y0 - y1)) / 4));
//var buff = vertex_create_buffer();
//vertex_begin(buff, global.format_pc);
var c = draw_get_color();
var ox, oy, nx, ny, t, it;
for( var i = 0; i <= sample; i++ ) {
t = i / sample;
it = 1 - t;
nx = x0 * t * t * t + 3 * xc * it * t * t + 3 * xc * it * it * t + x1 * it * it * it;
ny = y0 * t * t * t + 3 * y0 * it * t * t + 3 * y1 * it * it * t + y1 * it * it * it;
if(i) {
draw_line_width(ox, oy, nx, ny, thick);
//vertex_position(buff, ox, oy); vertex_color(buff, c, 1);
//vertex_position(buff, nx, ny); vertex_color(buff, c, 1);
}
ox = nx;
oy = ny;
}
//vertex_end(buff);
//vertex_submit(buff, pr_linelist, -1);
//buffer_delete(buff);
}
2023-03-05 07:16:44 +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) {
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);
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;
2022-01-29 14:25:18 +01:00
var dash_distance = 2;
2022-01-13 05:24:03 +01:00
2023-07-23 20:21:35 +02:00
var line = new LineDrawer(thick);
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 :
if(floor(i / dash_distance) % 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-07-23 20:21:35 +02:00
line.finish();
2022-01-13 05:24:03 +01:00
}
2023-03-28 06:58:28 +02:00
function draw_line_curve_corner(x0, y0, x1, y1, _s = 1, thick = 1, col1 = c_white, col2 = c_white) {
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-03-02 07:59:14 +01:00
function distance_to_curve(mx, my, x0, y0, x1, y1, xc, yc, _s) {
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;
}
function distance_to_curve_corner(mx, my, x0, y0, x1, y1, _s) {
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;
}