10_line.t 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 35;
  6. use constant PI => 4 * atan2(1, 1);
  7. use constant EPSILON => 1E-4;
  8. my $points = [
  9. [100, 100],
  10. [200, 100],
  11. ];
  12. my $line = Slic3r::Line->new(@$points);
  13. {
  14. my $clone = $line->clone;
  15. $clone->translate(10, -5);
  16. is_deeply $clone->pp, [
  17. [110, 95],
  18. [210, 95],
  19. ], 'translate';
  20. }
  21. {
  22. ok +Slic3r::Line->new([0,0],[200,0])->parallel_to_line(Slic3r::Line->new([200,200],[0,200])), 'parallel_to';
  23. }
  24. foreach my $base_angle (0, PI/4, PI/2, PI) {
  25. my $line = Slic3r::Line->new([0,0], [100,0]);
  26. $line->rotate($base_angle, [0,0]);
  27. my $clone = $line->clone;
  28. ok $line->parallel_to_line($clone), 'line is parallel to self';
  29. $clone->reverse;
  30. ok $line->parallel_to_line($clone), 'line is parallel to self + PI';
  31. ok $line->parallel_to($line->direction), 'line is parallel to its direction';
  32. ok $line->parallel_to($line->direction + PI), 'line is parallel to its direction + PI';
  33. ok $line->parallel_to($line->direction - PI), 'line is parallel to its direction - PI';
  34. {
  35. my $line2 = $line->clone;
  36. $line2->reverse;
  37. ok $line->parallel_to_line($line2), 'line is parallel to its opposite';
  38. }
  39. {
  40. my $line2 = $line->clone;
  41. $line2->rotate(+(EPSILON)/2, [0,0]);
  42. ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
  43. }
  44. {
  45. my $line2 = $line->clone;
  46. $line2->rotate(-(EPSILON)/2, [0,0]);
  47. ok $line->parallel_to_line($line2), 'line is parallel within epsilon';
  48. }
  49. }
  50. {
  51. my $a = Slic3r::Line->new([100, 0], [200, 0]);
  52. my $b = Slic3r::Line->new([300, 300], [300, 100]);
  53. my $r = $a->intersection_infinite($b);
  54. is_deeply $r->pp, [300, 0], 'intersection_infinite';
  55. }
  56. __END__