03_point.t 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Slic3r::XS;
  5. use Test::More tests => 24;
  6. my $point = Slic3r::Point->new(10, 15);
  7. is_deeply [ @$point ], [10, 15], 'point roundtrip';
  8. my $point2 = $point->clone;
  9. $point2->scale(2);
  10. is_deeply [ @$point2 ], [20, 30], 'scale';
  11. $point2->translate(10, -15);
  12. is_deeply [ @$point2 ], [30, 15], 'translate';
  13. ok $point->coincides_with($point->clone), 'coincides_with';
  14. ok !$point->coincides_with($point2), 'coincides_with';
  15. {
  16. my $point3 = Slic3r::Point->new(4300000, -9880845);
  17. is $point->[0], $point->x, 'x accessor';
  18. is $point->[1], $point->y, 'y accessor'; #,,
  19. }
  20. {
  21. my $nearest = $point->nearest_point([ $point2, Slic3r::Point->new(100, 200) ]);
  22. ok $nearest->coincides_with($point2), 'nearest_point';
  23. }
  24. {
  25. my $line = Slic3r::Line->new([0,0], [100,0]);
  26. is +Slic3r::Point->new(0,0) ->distance_to_line($line), 0, 'distance_to_line()';
  27. is +Slic3r::Point->new(100,0)->distance_to_line($line), 0, 'distance_to_line()';
  28. is +Slic3r::Point->new(50,0) ->distance_to_line($line), 0, 'distance_to_line()';
  29. is +Slic3r::Point->new(150,0)->distance_to_line($line), 50, 'distance_to_line()';
  30. is +Slic3r::Point->new(0,50) ->distance_to_line($line), 50, 'distance_to_line()';
  31. is +Slic3r::Point->new(50,50)->distance_to_line($line), 50, 'distance_to_line()';
  32. is +Slic3r::Point->new(50,50) ->perp_distance_to_line($line), 50, 'perp_distance_to_line()';
  33. is +Slic3r::Point->new(150,50)->perp_distance_to_line($line), 50, 'perp_distance_to_line()';
  34. }
  35. {
  36. my $line = Slic3r::Line->new([50,50], [125,-25]);
  37. cmp_ok(abs(Slic3r::Point->new(100,0)->distance_to_line($line)), '<=', 4e-15, 'distance_to_line()');
  38. }
  39. {
  40. my $line = Slic3r::Line->new(
  41. [18335846,18335845],
  42. [18335846,1664160],
  43. );
  44. $point = Slic3r::Point->new(1664161,18335848);
  45. is $point->perp_distance_to_line($line), 16671685, 'perp_distance_to_line() does not overflow';
  46. }
  47. {
  48. my $p0 = Slic3r::Point->new(76975850,89989996);
  49. my $p1 = Slic3r::Point->new(76989990,109989991);
  50. my $p2 = Slic3r::Point->new(76989987,89989994);
  51. ok $p0->ccw($p1, $p2) < 0, 'ccw() does not overflow';
  52. }
  53. {
  54. my $point = Slic3r::Point->new(15,15);
  55. my $line = Slic3r::Line->new([10,10], [20,10]);
  56. is_deeply $point->projection_onto_line($line)->pp, [15,10], 'project_onto_line';
  57. $point = Slic3r::Point->new(0, 15);
  58. is_deeply $point->projection_onto_line($line)->pp, [10,10], 'project_onto_line';
  59. $point = Slic3r::Point->new(25, 15);
  60. is_deeply $point->projection_onto_line($line)->pp, [20,10], 'project_onto_line';
  61. $point = Slic3r::Point->new(10,10);
  62. is_deeply $point->projection_onto_line($line)->pp, [10,10], 'project_onto_line';
  63. $point = Slic3r::Point->new(12, 10);
  64. is_deeply $point->projection_onto_line($line)->pp, [12,10], 'project_onto_line';
  65. }
  66. __END__