Pixel-Composer/scripts/__polygon/__polygon.gml

205 lines
4.8 KiB
Plaintext
Raw Normal View History

2023-02-14 02:48:33 +01:00
function polygon_simplify(points, tolerance = 4) {
var remSt = ds_stack_create();
var len = array_length(points);
for( var i = 0; i < len; i++ ) {
2023-03-19 09:17:39 +01:00
var _px0 = points[i].x;
var _py0 = points[i].y;
var _px1 = points[safe_mod(i + 1, len)].x;
var _py1 = points[safe_mod(i + 1, len)].y;
var _px2 = points[safe_mod(i + 2, len)].x;
var _py2 = points[safe_mod(i + 2, len)].y;
2023-02-14 02:48:33 +01:00
var dir0 = point_direction(_px0, _py0, _px1, _py1);
var dir1 = point_direction(_px1, _py1, _px2, _py2);
if(abs(dir0 - dir1) <= tolerance)
ds_stack_push(remSt, safe_mod(i + 1, len));
}
while(!ds_stack_empty(remSt)) {
var ind = ds_stack_pop(remSt);
array_delete(points, ind, 1);
}
ds_stack_destroy(remSt);
return points;
}
function polygon_points_classify(points) {
var len = array_length(points);
var maxx = -99999;
var maxindex = 0;
for( var i = 0; i < len; i++ ) {
2023-03-19 09:17:39 +01:00
var _x = points[i].x;
2023-02-14 02:48:33 +01:00
if(_x > maxx) {
maxx = _x;
maxindex = i;
}
}
var side, _side = 0;
2023-03-19 09:17:39 +01:00
var convexs = [];
var reflects = [];
var startindex = safe_mod(maxindex - 1 + len, len);
2023-02-14 02:48:33 +01:00
for( var i = 0; i < len; i++ ) {
2023-02-20 10:16:31 +01:00
var index = safe_mod(startindex + i, len);
2023-03-19 09:17:39 +01:00
var _px0 = points[index].x;
var _py0 = points[index].y;
var _px1 = points[safe_mod(index + 1, len)].x;
var _py1 = points[safe_mod(index + 1, len)].y;
var _px2 = points[safe_mod(index + 2, len)].x;
var _py2 = points[safe_mod(index + 2, len)].y;
2023-02-14 02:48:33 +01:00
var side = cross_product(_px0, _py0, _px1, _py1, _px2, _py2);
if(_side != 0 && sign(_side) != sign(side))
array_push(reflects, safe_mod(index + 1, len));
else {
array_push(convexs, safe_mod(index + 1, len));
_side = sign(side);
}
}
return [ convexs, reflects, _side ];
}
function polygon_triangulate_convex(points) {
var triangles = [];
var len = array_length(points);
var c0 = points[0];
for( var i = 0; i < len - 2; i++ ) {
var c1 = points[safe_mod(i + 1, len)];
var c2 = points[safe_mod(i + 2, len)];
array_push(triangles, [c0, c1, c2]);
}
return triangles;
}
function polygon_triangulate(points, tolerance = 4) {
points = polygon_simplify(points, tolerance);
var classes = polygon_points_classify(points);
var convexes = classes[0];
var reflected = classes[1];
var checkSide = classes[2];
var pointInd = [];
if(array_length(reflected) == 0)
return polygon_triangulate_convex(points);
2023-07-25 20:12:40 +02:00
for( var i = 0, n = array_length(points); i < n; i++ )
2023-02-14 02:48:33 +01:00
pointInd[i] = i;
var triangles = [];
var repeated = 0;
while(array_length(pointInd) > 3) {
if(array_length(convexes) == 0) return triangles;
var len = array_length(pointInd);
var c0 = convexes[0];
var c0i = array_find(pointInd, c0);
2023-02-20 10:16:31 +01:00
var c1 = pointInd[safe_mod(c0i - 1 + len, len)];
var c2 = pointInd[safe_mod(c0i + 1, len)];
2023-02-14 02:48:33 +01:00
var p0 = points[c0];
var p1 = points[c1];
var p2 = points[c2];
//check if point is ear
var isEar = true;
for( var i = 0; i < len; i++ ) {
var ind = pointInd[i];
if(ind == c0) continue;
if(ind == c1) continue;
if(ind == c2) continue;
var p = points[ind];
2023-03-19 09:17:39 +01:00
if(point_in_triangle(p.x, p.y, p0.x, p0.y, p1.x, p1.y, p2.x, p2.y)) {
2023-02-14 02:48:33 +01:00
isEar = false;
break;
}
}
if(isEar) {
array_remove(convexes, c0);
array_remove(pointInd, c0);
array_push(triangles, [p0, p1, p2]);
len--;
if(array_exists(reflected, c1)) {
var c1i = array_find(pointInd, c1);
2023-02-20 10:16:31 +01:00
var c1b = safe_mod(c1i - 1 + len, len);
var c1a = safe_mod(c1i + 1, len);
2023-02-14 02:48:33 +01:00
var p1b = points[pointInd[c1b]];
var p1a = points[pointInd[c1a]];
2023-03-19 09:17:39 +01:00
var side = cross_product(p1b.x, p1b.y, p1.x, p1.y, p1a.x, p1a.y);
2023-02-14 02:48:33 +01:00
if(sign(side) == checkSide) {
array_remove(reflected, c1);
array_push(convexes, c1);
}
}
if(array_exists(reflected, c2)) {
var c2i = array_find(pointInd, c2);
2023-02-20 10:16:31 +01:00
var c2b = safe_mod(c2i - 1 + len, len);
var c2a = safe_mod(c2i + 1, len);
2023-02-14 02:48:33 +01:00
var p2b = points[pointInd[c2b]];
var p2a = points[pointInd[c2a]];
2023-03-19 09:17:39 +01:00
var side = cross_product(p2b.x, p2b.y, p2.x, p2.y, p2a.x, p2a.y);
2023-02-14 02:48:33 +01:00
if(sign(side) == checkSide) {
array_remove(reflected, c2);
array_push(convexes, c2);
}
}
repeated = 0;
} else {
array_remove(convexes, c0);
array_push(convexes, c0);
if(repeated++ > len) {
print("mesh error")
break;
}
}
}
if(array_length(pointInd) == 3)
array_push(triangles, [points[pointInd[0]], points[pointInd[1]], points[pointInd[2]]]);
return triangles;
}
2023-03-19 09:17:39 +01:00
function polygon_triangulate_convex_fan(points) {
var triangles = [];
var amo = array_length(points);
var cx = 0;
var cy = 0;
for( var i = 0; i < amo; i++ ) {
cx += points[i].x;
cy += points[i].y;
}
cx /= amo;
cy /= amo;
var pc = new Point(cx, cy);
for( var i = 0; i < amo; i++ ) {
array_push(triangles, [ points[i], points[(i + 1) % amo], pc ]);
}
return triangles;
}