+
+our @distances;
+push @distances, 0;
+foreach my $i (1..$N) {
+ my $dist = $distances[ $i-1 ];
+ $dist += abs($ellipse[$i % $N] - $ellipse[$i-1]);
+ $distances[$i] = $dist;
+}
+
+sub infodistprop ($) {
+ my ($distprop) = @_;
+ # returns
+ # ( $ellipse_centreline_point,
+ # $along_vector )
+ my $dist = $distprop * $distances[$N];
+ foreach my $i (0..$N-1) {
+ my $prorata =
+ ($dist - $distances[$i]) /
+ ($distances[$i+1] - $distances[$i]);
+ next unless 0 <= $prorata && $prorata <= 1;
+ print "// infodistprop $distprop => #$i=$ellipse[$i] $prorata $ellipse[$i+1]\n";
+ return (
+ (1-$prorata) * $ellipse[$i] + ($prorata) * $ellipse[$i+1],
+ $alongs[$i],
+ );
+ }
+ die "$distprop ?";
+}
+
+sub channels(){
+ print "module Channels(){\n";
+
+ foreach my $cdp (
+ (map { 0.5 * $_ } @channeldistprops),
+ (map { 0.5 * ($_+1) } @channeldistprops),
+ ) {
+ my ($ctr, $along) = infodistprop($cdp);
+ my $angle = atan2(-$along->[0], $along->[1]);
+ print " translate(",scadvec($ctr),")\n";
+ print " rotate([0,0,$angle*360/",tau,"])\n";
+ print " rotate([0,90,0])\n";
+ print " translate([0,0, -2*$circle])\n";
+ print " scale([1, $channelw/$channelh/2, 1])\n";
+ print " rotate([0,0,360/8/2])\n";
+ print " cylinder(r=$channelh, h=4*$circle, \$fn=8);\n";
+ }
+ print "}\n";
+}
+
+channels();
+