Geometry.pm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. package Slic3r::Geometry;
  2. use strict;
  3. use warnings;
  4. require Exporter;
  5. our @ISA = qw(Exporter);
  6. our @EXPORT_OK = qw(
  7. PI X Y Z A B X1 Y1 X2 Y2 Z1 Z2 MIN MAX epsilon slope
  8. line_point_belongs_to_segment points_coincide distance_between_points
  9. normalize tan move_points_3D
  10. point_in_polygon point_in_segment segment_in_segment
  11. polyline_lines polygon_lines
  12. point_along_segment polygon_segment_having_point polygon_has_subsegment
  13. deg2rad rad2deg
  14. rotate_points move_points
  15. dot perp
  16. line_intersection bounding_box bounding_box_intersect
  17. angle3points
  18. chained_path chained_path_from collinear scale unscale
  19. rad2deg_dir bounding_box_center line_intersects_any douglas_peucker
  20. polyline_remove_short_segments normal triangle_normal polygon_is_convex
  21. scaled_epsilon bounding_box_3D size_3D size_2D
  22. convex_hull directions_parallel directions_parallel_within
  23. );
  24. use constant PI => 4 * atan2(1, 1);
  25. use constant A => 0;
  26. use constant B => 1;
  27. use constant X1 => 0;
  28. use constant Y1 => 1;
  29. use constant X2 => 2;
  30. use constant Y2 => 3;
  31. use constant Z1 => 4;
  32. use constant Z2 => 5;
  33. use constant MIN => 0;
  34. use constant MAX => 1;
  35. our $parallel_degrees_limit = abs(deg2rad(0.1));
  36. sub epsilon () { 1E-4 }
  37. sub scaled_epsilon () { epsilon / &Slic3r::SCALING_FACTOR }
  38. sub scale ($) { $_[0] / &Slic3r::SCALING_FACTOR }
  39. sub unscale ($) { $_[0] * &Slic3r::SCALING_FACTOR }
  40. sub tan {
  41. my ($angle) = @_;
  42. return (sin $angle) / (cos $angle);
  43. }
  44. sub slope {
  45. my ($line) = @_;
  46. return undef if abs($line->[B][X] - $line->[A][X]) < epsilon; # line is vertical
  47. return ($line->[B][Y] - $line->[A][Y]) / ($line->[B][X] - $line->[A][X]);
  48. }
  49. # this subroutine checks whether a given point may belong to a given
  50. # segment given the hypothesis that it belongs to the line containing
  51. # the segment
  52. sub line_point_belongs_to_segment {
  53. my ($point, $segment) = @_;
  54. #printf " checking whether %f,%f may belong to segment %f,%f - %f,%f\n",
  55. # @$point, map @$_, @$segment;
  56. my @segment_extents = (
  57. [ sort { $a <=> $b } map $_->[X], @$segment ],
  58. [ sort { $a <=> $b } map $_->[Y], @$segment ],
  59. );
  60. return 0 if $point->[X] < ($segment_extents[X][0] - epsilon) || $point->[X] > ($segment_extents[X][1] + epsilon);
  61. return 0 if $point->[Y] < ($segment_extents[Y][0] - epsilon) || $point->[Y] > ($segment_extents[Y][1] + epsilon);
  62. return 1;
  63. }
  64. sub points_coincide {
  65. my ($p1, $p2) = @_;
  66. return 1 if abs($p2->[X] - $p1->[X]) < epsilon && abs($p2->[Y] - $p1->[Y]) < epsilon;
  67. return 0;
  68. }
  69. sub distance_between_points {
  70. my ($p1, $p2) = @_;
  71. return sqrt((($p1->[X] - $p2->[X])**2) + ($p1->[Y] - $p2->[Y])**2);
  72. }
  73. # this will check whether a point is in a polygon regardless of polygon orientation
  74. sub point_in_polygon {
  75. my ($point, $polygon) = @_;
  76. my ($x, $y) = @$point;
  77. my $n = @$polygon;
  78. my @x = map $_->[X], @$polygon;
  79. my @y = map $_->[Y], @$polygon;
  80. # Derived from the comp.graphics.algorithms FAQ,
  81. # courtesy of Wm. Randolph Franklin
  82. my ($i, $j);
  83. my $side = 0; # 0 = outside; 1 = inside
  84. for ($i = 0, $j = $n - 1; $i < $n; $j = $i++) {
  85. if (
  86. # If the y is between the (y-) borders...
  87. ($y[$i] <= $y && $y < $y[$j]) || ($y[$j] <= $y && $y < $y[$i])
  88. and
  89. # ...the (x,y) to infinity line crosses the edge
  90. # from the ith point to the jth point...
  91. ($x < ($x[$j] - $x[$i]) * ($y - $y[$i]) / ($y[$j] - $y[$i]) + $x[$i])
  92. ) {
  93. $side = not $side; # Jump the fence
  94. }
  95. }
  96. # if point is not in polygon, let's check whether it belongs to the contour
  97. if (!$side && 0) {
  98. return 1 if polygon_segment_having_point($polygon, $point);
  99. }
  100. return $side;
  101. }
  102. sub point_in_segment {
  103. my ($point, $line) = @_;
  104. my ($x, $y) = @$point;
  105. my $line_p = $line->pp;
  106. my @line_x = sort { $a <=> $b } $line_p->[A][X], $line_p->[B][X];
  107. my @line_y = sort { $a <=> $b } $line_p->[A][Y], $line_p->[B][Y];
  108. # check whether the point is in the segment bounding box
  109. return 0 unless $x >= ($line_x[0] - epsilon) && $x <= ($line_x[1] + epsilon)
  110. && $y >= ($line_y[0] - epsilon) && $y <= ($line_y[1] + epsilon);
  111. # if line is vertical, check whether point's X is the same as the line
  112. if ($line_p->[A][X] == $line_p->[B][X]) {
  113. return abs($x - $line_p->[A][X]) < epsilon ? 1 : 0;
  114. }
  115. # calculate the Y in line at X of the point
  116. my $y3 = $line_p->[A][Y] + ($line_p->[B][Y] - $line_p->[A][Y])
  117. * ($x - $line_p->[A][X]) / ($line_p->[B][X] - $line_p->[A][X]);
  118. return abs($y3 - $y) < epsilon ? 1 : 0;
  119. }
  120. sub segment_in_segment {
  121. my ($needle, $haystack) = @_;
  122. # a segment is contained in another segment if its endpoints are contained
  123. return point_in_segment($needle->[A], $haystack) && point_in_segment($needle->[B], $haystack);
  124. }
  125. sub polyline_lines {
  126. my ($polyline) = @_;
  127. my @points = @$polyline;
  128. return map Slic3r::Line->new(@points[$_, $_+1]), 0 .. $#points-1;
  129. }
  130. sub polygon_lines {
  131. my ($polygon) = @_;
  132. return polyline_lines([ @$polygon, $polygon->[0] ]);
  133. }
  134. # given a segment $p1-$p2, get the point at $distance from $p1 along segment
  135. sub point_along_segment {
  136. my ($p1, $p2, $distance) = @_;
  137. my $point = [ @$p1 ];
  138. my $line_length = sqrt( (($p2->[X] - $p1->[X])**2) + (($p2->[Y] - $p1->[Y])**2) );
  139. for (X, Y) {
  140. if ($p1->[$_] != $p2->[$_]) {
  141. $point->[$_] = $p1->[$_] + ($p2->[$_] - $p1->[$_]) * $distance / $line_length;
  142. }
  143. }
  144. return Slic3r::Point->new(@$point);
  145. }
  146. # given a $polygon, return the (first) segment having $point
  147. sub polygon_segment_having_point {
  148. my ($polygon, $point) = @_;
  149. foreach my $line (@{ $polygon->lines }) {
  150. return $line if point_in_segment($point, $line);
  151. }
  152. return undef;
  153. }
  154. # return true if the given segment is contained in any edge of the polygon
  155. sub polygon_has_subsegment {
  156. my ($polygon, $segment) = @_;
  157. foreach my $line (polygon_lines($polygon)) {
  158. return 1 if segment_in_segment($segment, $line);
  159. }
  160. return 0;
  161. }
  162. # polygon must be simple (non complex) and ccw
  163. sub polygon_is_convex {
  164. my ($points) = @_;
  165. for (my $i = 0; $i <= $#$points; $i++) {
  166. my $angle = angle3points($points->[$i-1], $points->[$i-2], $points->[$i]);
  167. return 0 if $angle < PI;
  168. }
  169. return 1;
  170. }
  171. sub rotate_points {
  172. my ($radians, $center, @points) = @_;
  173. $center //= [0,0];
  174. return map {
  175. [
  176. $center->[X] + cos($radians) * ($_->[X] - $center->[X]) - sin($radians) * ($_->[Y] - $center->[Y]),
  177. $center->[Y] + cos($radians) * ($_->[Y] - $center->[Y]) + sin($radians) * ($_->[X] - $center->[X]),
  178. ]
  179. } @points;
  180. }
  181. sub move_points {
  182. my ($shift, @points) = @_;
  183. return map {
  184. my @p = @$_;
  185. Slic3r::Point->new($shift->[X] + $p[X], $shift->[Y] + $p[Y]);
  186. } @points;
  187. }
  188. sub move_points_3D {
  189. my ($shift, @points) = @_;
  190. return map [
  191. $shift->[X] + $_->[X],
  192. $shift->[Y] + $_->[Y],
  193. $shift->[Z] + $_->[Z],
  194. ], @points;
  195. }
  196. sub normal {
  197. my ($line1, $line2) = @_;
  198. return [
  199. ($line1->[Y] * $line2->[Z]) - ($line1->[Z] * $line2->[Y]),
  200. -($line2->[Z] * $line1->[X]) + ($line2->[X] * $line1->[Z]),
  201. ($line1->[X] * $line2->[Y]) - ($line1->[Y] * $line2->[X]),
  202. ];
  203. }
  204. sub triangle_normal {
  205. my ($v1, $v2, $v3) = @_;
  206. my $u = [ map +($v2->[$_] - $v1->[$_]), (X,Y,Z) ];
  207. my $v = [ map +($v3->[$_] - $v1->[$_]), (X,Y,Z) ];
  208. return normal($u, $v);
  209. }
  210. sub normalize {
  211. my ($line) = @_;
  212. my $len = sqrt( ($line->[X]**2) + ($line->[Y]**2) + ($line->[Z]**2) )
  213. or return [0, 0, 0]; # to avoid illegal division by zero
  214. return [ map $_ / $len, @$line ];
  215. }
  216. # 2D dot product
  217. sub dot {
  218. my ($u, $v) = @_;
  219. return $u->[X] * $v->[X] + $u->[Y] * $v->[Y];
  220. }
  221. # 2D perp product
  222. sub perp {
  223. my ($u, $v) = @_;
  224. return $u->[X] * $v->[Y] - $u->[Y] * $v->[X];
  225. }
  226. sub line_intersects_any {
  227. my ($line, $lines) = @_;
  228. for (@$lines) {
  229. return 1 if line_intersection($line, $_, 1);
  230. }
  231. return 0;
  232. }
  233. sub line_intersection {
  234. my ($line1, $line2, $require_crossing) = @_;
  235. $require_crossing ||= 0;
  236. my $intersection = _line_intersection(map @$_, @$line1, @$line2);
  237. return (ref $intersection && $intersection->[1] == $require_crossing)
  238. ? $intersection->[0]
  239. : undef;
  240. }
  241. sub collinear {
  242. my ($line1, $line2, $require_overlapping) = @_;
  243. my $intersection = _line_intersection(map @$_, @$line1, @$line2);
  244. return 0 unless !ref($intersection)
  245. && ($intersection eq 'parallel collinear'
  246. || ($intersection eq 'parallel vertical' && abs($line1->[A][X] - $line2->[A][X]) < epsilon));
  247. if ($require_overlapping) {
  248. my @box_a = bounding_box([ $line1->[0], $line1->[1] ]);
  249. my @box_b = bounding_box([ $line2->[0], $line2->[1] ]);
  250. return 0 unless bounding_box_intersect( 2, @box_a, @box_b );
  251. }
  252. return 1;
  253. }
  254. sub _line_intersection {
  255. my ( $x0, $y0, $x1, $y1, $x2, $y2, $x3, $y3 ) = @_;
  256. my ($x, $y); # The as-yet-undetermined intersection point.
  257. my $dy10 = $y1 - $y0; # dyPQ, dxPQ are the coordinate differences
  258. my $dx10 = $x1 - $x0; # between the points P and Q.
  259. my $dy32 = $y3 - $y2;
  260. my $dx32 = $x3 - $x2;
  261. my $dy10z = abs( $dy10 ) < epsilon; # Is the difference $dy10 "zero"?
  262. my $dx10z = abs( $dx10 ) < epsilon;
  263. my $dy32z = abs( $dy32 ) < epsilon;
  264. my $dx32z = abs( $dx32 ) < epsilon;
  265. my $dyx10; # The slopes.
  266. my $dyx32;
  267. $dyx10 = $dy10 / $dx10 unless $dx10z;
  268. $dyx32 = $dy32 / $dx32 unless $dx32z;
  269. # Now we know all differences and the slopes;
  270. # we can detect horizontal/vertical special cases.
  271. # E.g., slope = 0 means a horizontal line.
  272. unless ( defined $dyx10 or defined $dyx32 ) {
  273. return "parallel vertical";
  274. }
  275. elsif ( $dy10z and not $dy32z ) { # First line horizontal.
  276. $y = $y0;
  277. $x = $x2 + ( $y - $y2 ) * $dx32 / $dy32;
  278. }
  279. elsif ( not $dy10z and $dy32z ) { # Second line horizontal.
  280. $y = $y2;
  281. $x = $x0 + ( $y - $y0 ) * $dx10 / $dy10;
  282. }
  283. elsif ( $dx10z and not $dx32z ) { # First line vertical.
  284. $x = $x0;
  285. $y = $y2 + $dyx32 * ( $x - $x2 );
  286. }
  287. elsif ( not $dx10z and $dx32z ) { # Second line vertical.
  288. $x = $x2;
  289. $y = $y0 + $dyx10 * ( $x - $x0 );
  290. }
  291. elsif ( abs( $dyx10 - $dyx32 ) < epsilon ) {
  292. # The slopes are suspiciously close to each other.
  293. # Either we have parallel collinear or just parallel lines.
  294. # The bounding box checks have already weeded the cases
  295. # "parallel horizontal" and "parallel vertical" away.
  296. my $ya = $y0 - $dyx10 * $x0;
  297. my $yb = $y2 - $dyx32 * $x2;
  298. return "parallel collinear" if abs( $ya - $yb ) < epsilon;
  299. return "parallel";
  300. }
  301. else {
  302. # None of the special cases matched.
  303. # We have a "honest" line intersection.
  304. $x = ($y2 - $y0 + $dyx10*$x0 - $dyx32*$x2)/($dyx10 - $dyx32);
  305. $y = $y0 + $dyx10 * ($x - $x0);
  306. }
  307. my $h10 = $dx10 ? ($x - $x0) / $dx10 : ($dy10 ? ($y - $y0) / $dy10 : 1);
  308. my $h32 = $dx32 ? ($x - $x2) / $dx32 : ($dy32 ? ($y - $y2) / $dy32 : 1);
  309. return [Slic3r::Point->new($x, $y), $h10 >= 0 && $h10 <= 1 && $h32 >= 0 && $h32 <= 1];
  310. }
  311. # http://paulbourke.net/geometry/lineline2d/
  312. sub _line_intersection2 {
  313. my ($line1, $line2) = @_;
  314. my $denom = ($line2->[B][Y] - $line2->[A][Y]) * ($line1->[B][X] - $line1->[A][X])
  315. - ($line2->[B][X] - $line2->[A][X]) * ($line1->[B][Y] - $line1->[A][Y]);
  316. my $numerA = ($line2->[B][X] - $line2->[A][X]) * ($line1->[A][Y] - $line2->[A][Y])
  317. - ($line2->[B][Y] - $line2->[A][Y]) * ($line1->[A][X] - $line2->[A][X]);
  318. my $numerB = ($line1->[B][X] - $line1->[A][X]) * ($line1->[A][Y] - $line2->[A][Y])
  319. - ($line1->[B][Y] - $line1->[A][Y]) * ($line1->[A][X] - $line2->[A][X]);
  320. # are the lines coincident?
  321. if (abs($numerA) < epsilon && abs($numerB) < epsilon && abs($denom) < epsilon) {
  322. return Slic3r::Point->new(
  323. ($line1->[A][X] + $line1->[B][X]) / 2,
  324. ($line1->[A][Y] + $line1->[B][Y]) / 2,
  325. );
  326. }
  327. # are the lines parallel?
  328. if (abs($denom) < epsilon) {
  329. return undef;
  330. }
  331. # is the intersection along the segments?
  332. my $muA = $numerA / $denom;
  333. my $muB = $numerB / $denom;
  334. if ($muA < 0 || $muA > 1 || $muB < 0 || $muB > 1) {
  335. return undef;
  336. }
  337. return Slic3r::Point->new(
  338. $line1->[A][X] + $muA * ($line1->[B][X] - $line1->[A][X]),
  339. $line1->[A][Y] + $muA * ($line1->[B][Y] - $line1->[A][Y]),
  340. );
  341. }
  342. # 2D
  343. sub bounding_box {
  344. my ($points) = @_;
  345. my @x = map $_->x, @$points;
  346. my @y = map $_->y, @$points; #,,
  347. my @bb = (undef, undef, undef, undef);
  348. for (0..$#x) {
  349. $bb[X1] = $x[$_] if !defined $bb[X1] || $x[$_] < $bb[X1];
  350. $bb[X2] = $x[$_] if !defined $bb[X2] || $x[$_] > $bb[X2];
  351. $bb[Y1] = $y[$_] if !defined $bb[Y1] || $y[$_] < $bb[Y1];
  352. $bb[Y2] = $y[$_] if !defined $bb[Y2] || $y[$_] > $bb[Y2];
  353. }
  354. return @bb[X1,Y1,X2,Y2];
  355. }
  356. sub bounding_box_center {
  357. my ($bounding_box) = @_;
  358. return Slic3r::Point->new(
  359. ($bounding_box->[X2] + $bounding_box->[X1]) / 2,
  360. ($bounding_box->[Y2] + $bounding_box->[Y1]) / 2,
  361. );
  362. }
  363. sub size_2D {
  364. my @bounding_box = bounding_box(@_);
  365. return (
  366. ($bounding_box[X2] - $bounding_box[X1]),
  367. ($bounding_box[Y2] - $bounding_box[Y1]),
  368. );
  369. }
  370. # bounding_box_intersect($d, @a, @b)
  371. # Return true if the given bounding boxes @a and @b intersect
  372. # in $d dimensions. Used by line_intersection().
  373. sub bounding_box_intersect {
  374. my ( $d, @bb ) = @_; # Number of dimensions and box coordinates.
  375. my @aa = splice( @bb, 0, 2 * $d ); # The first box.
  376. # (@bb is the second one.)
  377. # Must intersect in all dimensions.
  378. for ( my $i_min = 0; $i_min < $d; $i_min++ ) {
  379. my $i_max = $i_min + $d; # The index for the maximum.
  380. return 0 if ( $aa[ $i_max ] + epsilon ) < $bb[ $i_min ];
  381. return 0 if ( $bb[ $i_max ] + epsilon ) < $aa[ $i_min ];
  382. }
  383. return 1;
  384. }
  385. # 3D
  386. sub bounding_box_3D {
  387. my ($points) = @_;
  388. my @extents = (map [undef, undef], X,Y,Z);
  389. foreach my $point (@$points) {
  390. for (X,Y,Z) {
  391. $extents[$_][MIN] = $point->[$_] if !defined $extents[$_][MIN] || $point->[$_] < $extents[$_][MIN];
  392. $extents[$_][MAX] = $point->[$_] if !defined $extents[$_][MAX] || $point->[$_] > $extents[$_][MAX];
  393. }
  394. }
  395. return @extents;
  396. }
  397. sub size_3D {
  398. my ($points) = @_;
  399. my @extents = bounding_box_3D($points);
  400. return map $extents[$_][MAX] - $extents[$_][MIN], (X,Y,Z);
  401. }
  402. # this assumes a CCW rotation from $p2 to $p3 around $p1
  403. sub angle3points {
  404. my ($p1, $p2, $p3) = @_;
  405. # p1 is the center
  406. my $angle = atan2($p2->[X] - $p1->[X], $p2->[Y] - $p1->[Y])
  407. - atan2($p3->[X] - $p1->[X], $p3->[Y] - $p1->[Y]);
  408. # we only want to return only positive angles
  409. return $angle <= 0 ? $angle + 2*PI() : $angle;
  410. }
  411. sub polyline_remove_short_segments {
  412. my ($points, $min_length, $isPolygon) = @_;
  413. for (my $i = $isPolygon ? 0 : 1; $i < $#$points; $i++) {
  414. if (distance_between_points($points->[$i-1], $points->[$i]) < $min_length) {
  415. # we can remove $points->[$i]
  416. splice @$points, $i, 1;
  417. $i--;
  418. }
  419. }
  420. }
  421. sub douglas_peucker {
  422. my ($points, $tolerance) = @_;
  423. no warnings "recursion";
  424. my $results = [];
  425. my $dmax = 0;
  426. my $index = 0;
  427. for my $i (1..$#$points) {
  428. my $d = $points->[$i]->distance_to(Slic3r::Line->new($points->[0], $points->[-1]));
  429. if ($d > $dmax) {
  430. $index = $i;
  431. $dmax = $d;
  432. }
  433. }
  434. if ($dmax >= $tolerance) {
  435. my $dp1 = douglas_peucker([ @$points[0..$index] ], $tolerance);
  436. $results = [
  437. @$dp1[0..($#$dp1-1)],
  438. @{douglas_peucker([ @$points[$index..$#$points] ], $tolerance)},
  439. ];
  440. } else {
  441. $results = [ $points->[0], $points->[-1] ];
  442. }
  443. return $results;
  444. }
  445. sub douglas_peucker2 {
  446. my ($points, $tolerance) = @_;
  447. my $anchor = 0;
  448. my $floater = $#$points;
  449. my @stack = ();
  450. my %keep = ();
  451. push @stack, [$anchor, $floater];
  452. while (@stack) {
  453. ($anchor, $floater) = @{pop @stack};
  454. # initialize line segment
  455. my ($anchor_x, $anchor_y, $seg_len);
  456. if (grep $points->[$floater][$_] != $points->[$anchor][$_], X, Y) {
  457. $anchor_x = $points->[$floater][X] - $points->[$anchor][X];
  458. $anchor_y = $points->[$floater][Y] - $points->[$anchor][Y];
  459. $seg_len = sqrt(($anchor_x ** 2) + ($anchor_y ** 2));
  460. # get the unit vector
  461. $anchor_x /= $seg_len;
  462. $anchor_y /= $seg_len;
  463. } else {
  464. $anchor_x = $anchor_y = $seg_len = 0;
  465. }
  466. # inner loop:
  467. my $max_dist = 0;
  468. my $farthest = $anchor + 1;
  469. for my $i (($anchor + 1) .. $floater) {
  470. my $dist_to_seg = 0;
  471. # compare to anchor
  472. my $vecX = $points->[$i][X] - $points->[$anchor][X];
  473. my $vecY = $points->[$i][Y] - $points->[$anchor][Y];
  474. $seg_len = sqrt(($vecX ** 2) + ($vecY ** 2));
  475. # dot product:
  476. my $proj = $vecX * $anchor_x + $vecY * $anchor_y;
  477. if ($proj < 0) {
  478. $dist_to_seg = $seg_len;
  479. } else {
  480. # compare to floater
  481. $vecX = $points->[$i][X] - $points->[$floater][X];
  482. $vecY = $points->[$i][Y] - $points->[$floater][Y];
  483. $seg_len = sqrt(($vecX ** 2) + ($vecY ** 2));
  484. # dot product:
  485. $proj = $vecX * (-$anchor_x) + $vecY * (-$anchor_y);
  486. if ($proj < 0) {
  487. $dist_to_seg = $seg_len
  488. } else { # calculate perpendicular distance to line (pythagorean theorem):
  489. $dist_to_seg = sqrt(abs(($seg_len ** 2) - ($proj ** 2)));
  490. }
  491. if ($max_dist < $dist_to_seg) {
  492. $max_dist = $dist_to_seg;
  493. $farthest = $i;
  494. }
  495. }
  496. }
  497. if ($max_dist <= $tolerance) { # use line segment
  498. $keep{$_} = 1 for $anchor, $floater;
  499. } else {
  500. push @stack, [$anchor, $farthest];
  501. push @stack, [$farthest, $floater];
  502. }
  503. }
  504. return [ map $points->[$_], sort keys %keep ];
  505. }
  506. 1;