Source

boxer / lace-maker

Diff from to

File lace-maker

 use strict;
 use Data::Dumper;
 use Math::Geometry::Voronoi;
+use Math::Clipper;
 
 my $size = 300;
 my $num = 200;
-my $scale = 0.8;
+my $line_width = -2;
 
 my @points = map { [ rand $size, rand $size ] } 0..$num;
 
 my $geo = Math::Geometry::Voronoi->new(points => \@points);
 $geo->compute;
 
+my @polys = $geo->polygons;
+
+# Inset everything and then replace the polygons inplace
+for my $poly (@polys)
+{
+	# Remove index value
+	shift @$poly;
+
+	my $n = Math::Clipper::offset([$poly], $line_width);
+	$poly = $n->[0];
+}
+
+
+#print Dumper(\@polys);
+#__END__
+
 print <<"";
 <!-- Created with lace-maker (http://trmm.net/) -->
 <svg xmlns="http://www.w3.org/2000/svg">
 <g transform="scale(3.543307)"><!-- scale to mm -->
 
-POLY:
-for my $poly ($geo->polygons)
+
+for my $poly (@polys)
 {
-	my $index = shift @$poly;
-	my $count = @$poly;
-	my $cx = 0;
-	my $cy = 0;
-
-	# Find the center point of the polygon.
-	# Throw away any polygons that are outside of the box.
-	for (my $i = 0 ; $i < $count ; $i++)
-	{
-		my ($x0,$y0) = @{ $poly->[$i+0] };
-		$cx += $x0;
-		$cy += $y0;
-
-	}
-
-	$cx /= $count;
-	$cy /= $count;
-	#printf STDERR "%.5f, %.5f\n", $cx, $cy;
-
 	print <<"";
-		<g transform	= "translate($cx,$cy)" >
 		<path
-			transform	= "scale($scale)"
 			stroke		= "#ff0000"
 			fill		= "none"
 			stroke-width	= "0.1px"
 	for my $pt (@$poly)
 	{
 		my ($x,$y) = @$pt;
-		$x -= $cx;
-		$y -= $cy;
 		print "$x,$y\n";
 	}
 
 	# Close the path
 	print <<"";
-		Z"/></g>
+		Z"/>
 
 	#print Dumper($poly);
 }