Source

boxer / lace-maker

Diff from to

lace-maker

 use Data::Dumper;
 use Math::Geometry::Voronoi;
 use Math::Clipper;
+use Getopt::Long qw(:config no_ignore_case);
 
 my $xsize = 100;
 my $ysize = 100;
 my $num = 32;
 my $line_width = 2;
 
+my $usage = <<__END_HELP__;
+Usage: lace-maker [options] > lace.svg
+Options:
+    -h | -? | --help           This help
+    -x N                       X dimension in mm
+    -y N                       Y dimension in mm
+    -n N                       Number of points
+    -w N                       Line width in mm
+ 
+Note that if the point density is too high the Voronoi generation might
+fail, causing an empty SVG output.
+__END_HELP__
+
+GetOptions(
+	'h|?|help'		=> sub { print $usage; exit 0 },
+	'x=f'			=> \$xsize,
+	'y=f'			=> \$ysize,
+	'n=i'			=> \$num,
+	'w=f'			=> \$line_width,
+) or die $usage;
+
+
 my @points = map { [
 	(rand $xsize*1.1) - $xsize*.05,
 	(rand $ysize*1.1) - $ysize*.05,
 
 my $clipper = Math::Clipper->new;
 my $scale = Math::Clipper::integerize_coordinate_sets($bounding, @polys);
-$clipper->add_subject_polygons(\@polys);
+
+eval {
+	$clipper->add_subject_polygons(\@polys);
+	1;;
+} or die "Bad polygons?  Try reducing number of points or line width.\n";
+
 $clipper->add_clip_polygon($bounding);
 
 my $n = $clipper->execute(Math::Clipper::CT_INTERSECTION);