Joe Bartok's Bow Window Calculator





Perl solve_theta function


#=============================================================================#

sub solve_theta{
    my ($projection,$span,$bays) = @_;
    
    my $deg = (180/$PIE);    
    my $half_span = 0.5 * $span;
    my $theta = 0.01;
    my $limit = 0.5 * ($bays - 1);
    my $ratio = 10.0;
    my $difference = 10;
    my $numerator = 0;
    my $denominator = 0;
    my $odd_bay_add = 0.5;
    my $odd_even  = $bays  %2;
    if($odd_even == 0){
        $odd_bay_add = 0.0;
        $limit = 0.5  * $bays;
        
    }    
   
    
    my $i = 0;
    print "
Results From Perl Code\nloop results\n";
    print "\n\n span = $span";
    print "\n projection = $projection";
    print "\n bays = $bays\n\n";
          
   OUTTER: for($si=0; $si<1000; $si++){
        my  $n_sum = 0;
        my  $d_sum = 0;
        my $n_calc = 0;
        my $d_calc = 0;
 
      
       do{
       
          for (my $n=1; $n<=$limit; $n++){
              if($odd_even > 0){
                    $n_calc = cos($n * $theta);
                    $d_calc = sin($n * $theta);
              }else{
                    $n_calc = cos(((($n * 2)-1)* $theta)/2);
                    $d_calc = sin(((($n * 2)-1)* $theta)/2);
              }
  
              
              $n_sum = $n_sum + $n_calc;
              $d_sum = $d_sum + $d_calc;
                            
          }
           if($d_sum == 0){last OUTTER;}
           $numerator = $half_span /($odd_bay_add + $n_sum);
           $denominator = $projection / $d_sum;
           $ratio = $numerator / $denominator;
           $difference = $denominator - $numerator;
           $i++;
           $theta = $theta / $ratio;
           
           	print "\n$i difference = " .roundAngle($difference);
           	print "\n$i ratio = " .roundAngle($ratio);
           	print "\n$i theta = " .roundAngle($theta * $deg);

           
           if($ratio < 1){next;}
        }while($difference > 0);                
      
        if(abs($difference) < 0.0000000000001){last;}
            
    }
    my $central_angle = $theta * $bays;
    $radius = $span /(2 * sin($central_angle / 2));  
    my $apothem = $radius * cos($theta / 2);  
    my $side_angle = ($PIE - $central_angle) /2;
    my $offset = $apothem - (($span/2) * tan($side_angle));
    if($odd_even == 0){$offset = $radius - (($span/2) * tan($side_angle));}
    
    $theta = 0 if(abs($projection - $offset) > $projection_precision);
    
    
my $bay_arc = $radius*$theta;
my $total_arc = $bays*$radius*$theta;

print "\n\nbest fit results\n";

print "\n\n Span derived Side = " .roundAngle($numerator);
print "\n Projection derived Side = " .roundAngle($denominator);
print "\n Ratio = " .roundAngle($ratio);
print "\n Difference = " .roundAngle($difference);
print "\n Theta = " .roundAngle($theta*$deg);
print "\n Radius = " .roundAngle($radius);
print "\n Arc Length Per Bay = " .roundAngle($bay_arc);
print "\n Total arc length = " .roundAngle($total_arc);
print "\n Calculated Projection = " .roundAngle($offset);
print "\n
"; return ($theta); }

Translated Version of - JavaScript solve_polygon function
to display results in iFrame


//Title: JavaScript for Online Bow Window Polygon Side and Central Angle Calculator
//Author: Joe Bartok
//Date: 2007

function solve_polygon()
{
var F = Math.pow(10,12)
var pi = Math.PI
var deg = 180/pi
var rad = pi/180

var display = "\nbest fit results\n";
var span = document.rafterForm.span.value
var projection = document.rafterForm.projection.value
var bays = document.rafterForm.bays.value

if(span < 12){span = 12};
//if(projection > span) {projection = span/2};
if(bays < 2) {bays = 3 };

var results = "
Results From JavaScript Code\nloop results\n";
		results = results + "\n\n" + " span = " + span + " "
		results = results + "\n" +  " projection = " + projection + " "
		results = results + "\n" +  " bays = " + bays + "\n\n "
        
var half_span = .5*span;
//var theta = .01;
var theta = 1/F;
var limit = .5*(bays - 1);
var i = 0;

var odd_bay_add = 0.5;
var odd_bays = true;

var odd_even  = bays  %2;
if(odd_even == 0){ 
	odd_bays = false;
    limit = .5*bays;
	odd_bay_add = 0.0;
}
   
for (var j=0; j<1000; j++){

	
	var n_sum = 0;
	var d_sum = 0;
	var n_calc = 0;
	var d_calc = 0;
	
	do{
		for (var n=1; n<=limit; n++){
			if(odd_bays == true){
				n_calc = Math.cos(n*theta);
				d_calc = Math.sin(n*theta);
			}else{
				n_calc = Math.cos((2*n - 1)*theta/2)
				d_calc = Math.sin((2*n - 1)*theta/2)
			}
			n_sum = n_sum + n_calc
			d_sum = d_sum + d_calc
		}
		
		
	    if(d_sum == 0){ break;}
		var numerator = half_span/(odd_bay_add + n_sum)
		var denominator = projection/d_sum
		var ratio = numerator/denominator
		var difference = denominator - numerator
		results = results + "\n" + i + " difference = " + difference + " "
		results = results + "\n" + i + " ratio = " + ratio + " "
		results = results + "\n" + i + " theta = " + deg*theta + " "
		i++;
		theta = theta/ratio
		if (ratio < 1){	break;}
	
	} while (difference > 0);
	//if(Math.abs(difference) < .0000000000001){ break;}
	if(Math.abs(difference) < 1/F){ break;}
} 

var radius = .5*((numerator + denominator)/2)/(Math.sin(.5*theta))
var bay_arc = radius*theta
var total_arc = bays*radius*theta


display = display + "\n\n" + " Span derived Side = " + numerator
display = display + "\n" + " Projection derived Side = " + denominator
display = display + "\n" + " Ratio = " + ratio
display = display + "\n" + " Difference = " + difference
display = display + "\n" + " Theta = " + theta*deg
display = display + "\n" + " Radius = " + radius
display = display + "\n" + " Arc Length Per Bay = " + bay_arc
display = display + "\n" + " Total arc length = " + total_arc
display = display + "\n" + " odd bays = " + odd_bays
results = results + display + "
"; Show_Results(results); } function Show_Results(val) { var testFrame = document.getElementById("iframe1"); var doc = testFrame.contentDocument; if (doc == undefined || doc == null) doc = testFrame.contentWindow.document; doc.open(); doc.write(val); doc.close(); }