|
| 1 | +/* |
| 2 | + +----------------------------------------------------------------------+ |
| 3 | + | PHP | |
| 4 | + +----------------------------------------------------------------------+ |
| 5 | + | Copyright (c) 1997-2024 The PHP Group | |
| 6 | + +----------------------------------------------------------------------+ |
| 7 | + | This source file is subject to version 3.01 of the PHP license, | |
| 8 | + | that is bundled with this package in the file LICENSE, and is | |
| 9 | + | available through the world-wide-web at the following url: | |
| 10 | + | http://www.php.net/license/3_01.txt | |
| 11 | + | If you did not receive a copy of the PHP license and are unable to | |
| 12 | + | obtain it through the world-wide-web, please send a note to | |
| 13 | + | license@php.net so we can mail you a copy immediately. | |
| 14 | + +----------------------------------------------------------------------+ |
| 15 | + | Authors: Derick Rethans <github@derickrethans.nl> | |
| 16 | + +----------------------------------------------------------------------+ |
| 17 | +*/ |
| 18 | +#include <math.h> |
| 19 | + |
| 20 | +#include "geo_array.h" |
| 21 | + |
| 22 | +static double rdp_find_perpendicular_distable(double pX, double pY, double p1X, double p1Y, double p2X, double p2Y) |
| 23 | +{ |
| 24 | + double slope, intercept, result; |
| 25 | + |
| 26 | + if (p1X == p2X) { |
| 27 | + return fabs(pX - p1X); |
| 28 | + } else { |
| 29 | + slope = (p2Y - p1Y) / (p2X - p1X); |
| 30 | + intercept = p1Y - (slope * p1X); |
| 31 | + result = fabs(slope * pX - pY + intercept) / sqrt(pow(slope, 2) + 1); |
| 32 | + return result; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +void rdp_simplify(geo_array *points, double epsilon, int start, int end) |
| 37 | +{ |
| 38 | + double firstX = points->x[start]; |
| 39 | + double firstY = points->y[start]; |
| 40 | + double lastX = points->x[end]; |
| 41 | + double lastY = points->y[end]; |
| 42 | + int index = -1; |
| 43 | + double dist = 0.0, current_dist; |
| 44 | + int i; |
| 45 | + |
| 46 | + if (end - start < 2) { |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + for (i = start + 1; i < end; i++) { |
| 51 | + if (!points->status[i]) { |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + current_dist = rdp_find_perpendicular_distable(points->x[i], points->y[i], firstX, firstY, lastX, lastY); |
| 56 | + |
| 57 | + if (current_dist > dist) { |
| 58 | + dist = current_dist; |
| 59 | + index = i; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + if (dist > epsilon) { |
| 64 | + rdp_simplify(points, epsilon, start, index); |
| 65 | + rdp_simplify(points, epsilon, index, end); |
| 66 | + |
| 67 | + return; |
| 68 | + } else { |
| 69 | + for (i = start + 1; i < end; i++) { |
| 70 | + points->status[i] = 0; |
| 71 | + } |
| 72 | + return; |
| 73 | + } |
| 74 | +} |
| 75 | + |
0 commit comments