Commits

Trammell Hudson committed b4fcb94 Draft

clipping works

Comments (0)

Files changed (1)

 use Math::Geometry::Voronoi;
 use Math::Clipper;
 
-my $size = 300;
-my $num = 200;
-my $line_width = -2;
+my $size = 200;
+my $num = 150;
+my $line_width = 2;
 
-my @points = map { [ rand $size, rand $size ] } 0..$num;
+my @points = map { [
+	(rand $size*1.1) - $size*.05,
+	(rand $size*1.1) - $size*.05,
+] } 0..$num;
 
 my $geo = Math::Geometry::Voronoi->new(points => \@points);
 $geo->compute;
 	# Remove index value
 	shift @$poly;
 
-	my $n = Math::Clipper::offset([$poly], $line_width);
+	# Inset the polygon by the line width (negative == towards center)
+	my $n = Math::Clipper::offset([$poly], -$line_width);
 	$poly = $n->[0];
 }
 
 
+# Clip it by the bounding box
+my $bounding = [
+	[0,0],
+	[$size,0],
+	[$size,$size],
+	[0,$size],
+];
+
+my $clipper = Math::Clipper->new;
+my $scale = Math::Clipper::integerize_coordinate_sets($bounding, @polys);
+$clipper->add_subject_polygons(\@polys);
+$clipper->add_clip_polygon($bounding);
+
+my $n = $clipper->execute(Math::Clipper::CT_INTERSECTION);
+Math::Clipper::unscale_coordinate_sets($scale, $n);
+@polys = @$n;
+
 #print Dumper(\@polys);
 #__END__