Pixel-Composer/scripts/__path/__path.gml

119 lines
3.0 KiB
Plaintext
Raw Normal View History

2023-03-19 09:17:39 +01:00
function Path() constructor {
lengthTotal = 0;
lengths = [];
lengthAccs = [];
boundary = new BoundingBox();
loop = false;
static getBoundary = function() { return boundary; }
static getAccuLength = function() { return lengthAccs; }
static getLength = function() { return lengthTotal; }
static getSegmentCount = function() { return 1; }
static getLineCount = function() { return 1; }
static getTangentRatio = function(_rat) { return 0; }
2024-01-22 10:26:25 +01:00
static getPointDistance = function(_seg, _ind = 0, out = undefined) { return new __vec2(0, 0); }
static getPointRatio = function(_rat, _ind = 0, out = undefined) {
var pix = frac(_rat) * lengthTotal;
return getPointDistance(pix, _ind, out);
}
2023-03-19 09:17:39 +01:00
}
function PathSegment() : Path() constructor {
segments = [];
static getBoundary = function() { #region
2023-03-19 09:17:39 +01:00
if(getSegmentCount() == 0) return new BoundingBox( 0, 0, 0, 0 );
var minx = segments[0].x, maxx = segments[0].x;
var miny = segments[0].y, maxy = segments[0].y;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(segments); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
var s = segments[i];
minx = min(minx, s.x);
maxx = max(maxx, s.x);
miny = min(miny, s.y);
maxy = max(maxy, s.y);
}
return new BoundingBox( minx, miny, maxx, maxy );
} #endregion
2023-03-19 09:17:39 +01:00
static getSegmentCount = function() { return array_length(segments); }
static getLineCount = function() { return 1; }
static setSegment = function(segment) { #region
2023-03-19 09:17:39 +01:00
self.segments = segment;
lengths = [];
lengthAccs = [];
lengthTotal = 0;
2023-03-19 09:17:39 +01:00
var op, np;
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(segment); i < n; i++ ) {
2023-03-19 09:17:39 +01:00
np = segment[i];
if(i) {
lengths[i - 1] = point_distance(op.x, op.y, np.x, np.y);
lengthTotal += lengths[i - 1];
lengthAccs[i - 1] = lengthTotal;
2023-03-19 09:17:39 +01:00
}
op = np;
}
} #endregion
2023-03-19 09:17:39 +01:00
static getPointDistance = function(_dist, _ind = 0, out = undefined) { #region
if(out == undefined) out = new __vec2(); else { out.x = 0; out.y = 0; }
for( var i = 0; i < array_length(lengths); i++ ) {
2023-03-19 09:17:39 +01:00
if(_dist <= lengths[i]) {
var rat = _dist / lengths[i];
out.x = lerp(segments[i].x, segments[i + 1].x, rat);
out.y = lerp(segments[i].y, segments[i + 1].y, rat);
return out;
2023-03-19 09:17:39 +01:00
}
_dist -= lengths[i];
}
out.x = segments[i].x;
out.y = segments[i].y;
return out;
} #endregion
2023-03-19 09:17:39 +01:00
static getPointRatio = function(_rat, _ind = 0, out = undefined) { return getPointDistance(frac(_rat) * lengthTotal, _ind, out); }
2024-01-23 11:01:19 +01:00
static getTangentRatio = function(_rat) { #region
2023-03-19 09:17:39 +01:00
_rat = frac(_rat);
var l = _rat * lengthTotal;
for( var i = 1; i < array_length(lengths); i += 1 ) {
if(l <= lengths[i]) {
var rat = l / lengths[i];
return segments[i - 1].directionTo(segments[i]);
}
l -= lengths[i];
}
return 0;
} #endregion
static draw = function(_x, _y, _s) { #region
var ox, oy, nx, ny;
for( var i = 0, n = array_length(segments); i < n; i++ ) {
nx = _x + segments[i].x * _s;
ny = _y + segments[i].y * _s;
if(i) draw_line(ox, oy, nx, ny);
ox = nx;
oy = ny;
}
} #endregion
2023-03-19 09:17:39 +01:00
}