<?php
$page_name = "2D Stock Cutting<br />1V and 2V Domes";
$menu_type = "programming";
$menu_name = "2D stock cutting calc";
$blog_num = 30;
$page_rate = 10;
$page_tags = array("erudition");
$page_date = "September 17, 2011";
include_once("./files/head.php");

echo "<p>Alrighty then, welcome back.  I've coded up the first 2 dome programs. <a href=\"http://www.cerberus.ca/~scott/musings/stock_cutting.php" . $admin_link . "\">(Huh? What's this about? Read the background here)</a></p>\n";
echo "<p>Part 3 will be back to the math.</p>\n";
echo "<p>Both these programs allow you to specify spare parts.</p>\n";


//     #####     #####     #####     V1 DOME     #####     #####     #####
echo "<form enctype=\"multipart/form-data\" id=\"one_v_calc\" action=\"./stock_cutting_2.php" . $admin_link . "#one_v_calc\" method=\"POST\">\n";
echo "<h3>1V Dome Stock Calculator</h3>\n";

//one v globals
$one_v_enable = $_POST['from_one_v'];
$one_v_L = ($one_v_enable == 0) ? 10.0 : $_POST['one_v_L'];
$one_v_as = ($one_v_enable == 0) ? 1 : $_POST['one_v_as'];
$one_v_a = 25 + $one_v_as;
$one_v_al = ($one_v_enable == 0) ? 1 : $_POST['one_v_al'];
$one_v_n = floor($one_v_L / $one_v_al);
$one_v_X = ceil($one_v_a / $one_v_n);
$one_v_w = round($one_v_L - ($one_v_n * $one_v_al), 3);
$one_v_W = $one_v_w * $one_v_X;

$one_v_unit = ($one_v_enable == 0) ? "none" : $_POST['one_v_unit'];
if ($one_v_unit != "none") {
  $one_v_disp_u = ($one_v_unit == "feet") ? "ft" : "m";
} else {
  $one_v_disp_u = null;
}

echo "<input type=\"hidden\" name=\"from_one_v\" id=\"from_one_v\" value=\"1\" />\n";
echo "<table>";
  echo "<tr>\n";
    echo "<td>Length of stock: <input type=\"text\" name=\"one_v_L\" id=\"one_v_L\" value=\"" . $one_v_L . "\" size=\"6\"/> " . $one_v_disp_u . "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Length of pole A: <input type=\"text\" name=\"one_v_al\" id=\"one_v_al\" value=\"" . $one_v_al . "\" size=\"6\"/> " . $one_v_disp_u . "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Spares of pole A: <input type=\"text\" name=\"one_v_as\" id=\"one_v_as\" value=\"" . $one_v_as . "\" size=\"3\"/></td>\n";
  echo "</tr>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Units*: <select name=\"one_v_unit\" id=\"one_v_unit\" >\n";
	  echo "<option value=\"" . $one_v_unit . "\" >" . $one_v_unit . "</option>\n";
	  echo "<option value=\"none\">none</option>\n";
	  echo "<option value=\"feet\">feet</option>\n";
	  echo "<option value=\"meters\">meters</option>\n";
	  echo "</select>\n";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Submit\" /><br />*Not needed, but nicer to read.</td>\n";
  echo "</tr>\n";
echo "</table>";
echo "</form>\n";

$one_v_al_feet = ($one_v_unit == "feet") ? "  (" . make_inches($one_v_al) . ")" : null;
$one_v_w_feet = ($one_v_unit == "feet") ? "  (" . make_inches($one_v_w) . ")" : null;
$one_v_W_feet = ($one_v_unit == "feet") ? "  (" . make_inches($one_v_W) . ")" : null;

echo "<br />\n";
echo "<p>Building a 1v dome from " . $one_v_L . $one_v_disp_u . " stock with " . $one_v_al . $one_v_disp_u . " poles will:</p>\n";
echo "<ul>\n";
  echo "<li>Your A poles will be " . $one_v_al . $one_v_disp_u . " long." . $one_v_al_feet . "</li>\n";
  echo "<li>You need " . $one_v_X . " pieces of stock.</li>\n";
  echo "<li>You will get " . $one_v_n . " poles per piece of stock and " . $one_v_w . $one_v_disp_u . " of waste." . $one_v_w_feet . "</li>\n";
  echo "<li>Your total waste is " . $one_v_W . $one_v_disp_u . "." . $one_v_W_feet . "</li>\n";
echo "</ul>\n";


//     #####     #####     #####     V2 DOME     #####     #####     #####
echo "<form enctype=\"multipart/form-data\" id=\"two_v_calc\" action=\"./stock_cutting_2.php" . $admin_link . "#two_v_calc\" method=\"POST\">\n";
echo "<h3>2V Dome Stock Calculator</h3>\n";

//two v globals
//0 = name, 1 = # of A, 2 = # of B, 3 = max, 4 = waste, 5 = waste of combo1, 6 = waste of combo2
$two_v_cuts = array(array("A", 1, 0, 0, 0, 0, 0),
                    array("B", 0, 1, 0, 0, 0, 0),
                    array("AA", 2, 0, 0, 0, 0, 0),
                    array("AB", 1, 1, 0, 0, 0, 0),
                    array("BB", 0, 2, 0, 0, 0, 0),
                    array("AAA", 3, 0, 0, 0, 0, 0),
                    array("AAB", 2, 1, 0, 0, 0, 0),
                    array("ABB", 1, 2, 0, 0, 0, 0),
                    array("BBB", 0, 3, 0, 0, 0, 0),
                    array("AAAA", 4, 0, 0, 0, 0, 0),
                    array("AAAB", 3, 1, 0, 0, 0, 0),
                    array("AABB", 2, 2, 0, 0, 0, 0),
                    array("ABBB", 1, 3, 0, 0, 0, 0),
                    array("BBBB", 0, 4, 0, 0, 0, 0),
                    array("AAAAA", 5, 0, 0, 0, 0, 0),
                    array("AAAAB", 4, 1, 0, 0, 0, 0),
                    array("AAABB", 3, 2, 0, 0, 0, 0),
                    array("AABBB", 2, 3, 0, 0, 0, 0),
                    array("ABBBB", 1, 4, 0, 0, 0, 0),
                    array("BBBBB", 0, 5, 0, 0, 0, 0),
					array("AAAAAA", 6, 0, 0, 0, 0, 0),
                    array("AAAAAB", 5, 1, 0, 0, 0, 0),
                    array("AAAABB", 4, 2, 0, 0, 0, 0),
                    array("AAABBB", 3, 3, 0, 0, 0, 0),
                    array("AABBBB", 2, 4, 0, 0, 0, 0),
                    array("ABBBBB", 1, 5, 0, 0, 0, 0),
                    array("BBBBBB", 0, 6, 0, 0, 0, 0));
$two_v_cuts_size = count($two_v_cuts);
$two_v_enable = $_POST['from_two_v'];
$two_v_L = ($two_v_enable == 0) ? 10.0 : $_POST['two_v_L'];
$two_v_as = ($two_v_enable == 0) ? 1 : $_POST['two_v_as'];
$two_v_a = 35 + $two_v_as;
$two_v_al = ($two_v_enable == 0) ? 1 : $_POST['two_v_al'];
$two_v_bs = ($two_v_enable == 0) ? 1 : $_POST['two_v_bs'];
$two_v_b = 30 + $two_v_bs;
$two_v_bl = 0.885 * $two_v_al;

//calculate waste and if cuts are possible
for ($i=0; $i < $two_v_cuts_size; $i++) {
  $temp = $two_v_cuts[$i][1] * $two_v_al + $two_v_cuts[$i][2] * $two_v_bl;
  $two_v_cuts[$i][4] = round($two_v_L - $temp, 3);
  $temp_keep = floor($two_v_L / $temp);
  if ($temp_keep == 0) { $two_v_cuts[$i] = null; }
  if ($two_v_cuts[$i][4] > $temp) { $two_v_cuts[$i] = null; }
}
sort($two_v_cuts);
//remove impossible cuts and excessive waste
for ($i=0; $i < $two_v_cuts_size; $i++) {
  if ($two_v_cuts[0] == null) { array_shift($two_v_cuts); }
}
$two_v_cuts_size = count($two_v_cuts);

//find out maximum number of each combo
for ($i=0; $i < $two_v_cuts_size; $i++) {
  $tempa = ceil($two_v_a / $two_v_cuts[$i][1]);
  $tempb = ceil($two_v_b / $two_v_cuts[$i][2]);
  if ($tempa > $tempb) {
    $two_v_cuts[$i][3] = ($tempb != 0) ? $tempb : $tempa;
  } else {
    $two_v_cuts[$i][3] = ($tempa != 0) ? $tempa : $tempb;
  }
}

//make up combos of cuts to find optimum
$two_v_combos = array();
for ($i=0; $i < $two_v_cuts_size; $i++){
  $tempa = $two_v_cuts[$i][1] * $two_v_cuts[$i][3];
  $tempb = $two_v_cuts[$i][2] * $two_v_cuts[$i][3];
  $temp_a_max = ($tempa >= $two_v_a) ? 1 : 0;
  $temp_b_max = ($tempb >= $two_v_b) ? 1 : 0;
  for ($j=0; $j < $two_v_cuts_size; $j++){
    if ($i != $j) {
	  if ($temp_a_max == 1 && $two_v_cuts[$j][1] == 0) {
	    $temp_combo1 = $two_v_cuts[$i][0];
	    $temp_combo2 = $two_v_cuts[$j][0];
		$temp_needed = $two_v_b - $tempb;
		$temp_filler = ceil($temp_needed / $two_v_cuts[$j][2]);
		$temp_waste1 = $two_v_cuts[$i][4] * $two_v_cuts[$i][3];
		$temp_waste2 = $two_v_cuts[$j][4] * $temp_filler;
		$temp_waste3 = $temp_waste1 + $temp_waste2;
	    $two_v_combos[] = array($temp_combo1, $two_v_cuts[$i][3], $temp_combo2, $temp_filler, $temp_waste3, $two_v_cuts[$i][4], $two_v_cuts[$j][4]);
	  }
	  if ($temp_b_max == 1 && $two_v_cuts[$j][2] == 0) {
	    $temp_combo1 = $two_v_cuts[$i][0];
	    $temp_combo2 = $two_v_cuts[$j][0];
		$temp_needed = $two_v_a - $tempa;
		$temp_filler = ceil($temp_needed / $two_v_cuts[$j][1]);
		$temp_waste1 = $two_v_cuts[$i][4] * $two_v_cuts[$i][3];
		$temp_waste2 = $two_v_cuts[$j][4] * $temp_filler;
		$temp_waste3 = $temp_waste1 + $temp_waste2;
	    $two_v_combos[] = array($temp_combo1, $two_v_cuts[$i][3], $temp_combo2, $temp_filler, $temp_waste3, $two_v_cuts[$i][4], $two_v_cuts[$j][4]);
	  }
    }
  }
}
$two_v_combos_size = count($two_v_combos);
$sort_num = 4;
usort($two_v_combos, "sort_sub_array");

//eliminate duplicates
for ($i=0; $i < $two_v_combos_size; $i++){
  if ($two_v_combos[$i][0] == $two_v_combos[$i+1][2] && $two_v_combos[$i][2] == $two_v_combos[$i+1][0] && $two_v_combos[$i][4] == $two_v_combos[$i+1][4]) {
    $two_v_combos[$i] = null;
  }
}
sort($two_v_combos);

for ($i=0; $i < $two_v_combos_size; $i++) {
  if ($two_v_combos[0] == null) { array_shift($two_v_combos); }
}
$two_v_combos_size = count($two_v_combos);
$sort_num = 4;
usort($two_v_combos, "sort_sub_array");

$two_v_X = $two_v_combos[0][1] + $two_v_combos[0][3];
$two_v_W = $two_v_combos[0][4];

$two_v_unit = ($two_v_enable == 0) ? "none" : $_POST['two_v_unit'];
if ($two_v_unit != "none") {
  $two_v_disp_u = ($two_v_unit == "feet") ? "ft" : "m";
} else {
  $two_v_disp_u = null;
}

  echo "<input type=\"hidden\" name=\"from_two_v\" id=\"from_two_v\" value=\"1\" />\n";
echo "<table>";
  echo "<tr>\n";
    echo "<td>Length of Stock: <input type=\"text\" name=\"two_v_L\" id=\"two_v_L\" value=\"" . $two_v_L . "\" size=\"6\"/> " . $two_v_disp_u . "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Length of pole A: <input type=\"text\" name=\"two_v_al\" id=\"two_v_al\" value=\"" . $two_v_al . "\" size=\"6\"/> " . $two_v_disp_u . "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Length of pole B: " . $two_v_bl .  $two_v_disp_u . "</td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Spares of pole A: <input type=\"text\" name=\"two_v_as\" id=\"two_v_as\" value=\"" . $two_v_as . "\" size=\"3\"/></td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Spares of pole B: <input type=\"text\" name=\"two_v_bs\" id=\"two_v_bs\" value=\"" . $two_v_bs . "\" size=\"3\"/></td>\n";
  echo "</tr>\n";
  echo "<tr>\n";
    echo "<td>Units*: <select name=\"two_v_unit\" id=\"two_v_unit\" >\n";
	  echo "<option value=\"" . $two_v_unit . "\" >" . $two_v_unit . "</option>\n";
	  echo "<option value=\"none\">none</option>\n";
	  echo "<option value=\"feet\">feet</option>\n";
	  echo "<option value=\"meters\">meters</option>\n";
	  echo "</select>\n";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type=\"submit\" value=\"Submit\" /><br />*Not needed, but nicer to read.</td>\n";
  echo "</tr>\n";
echo "</table>";
echo "</form>\n";

$two_v_al_feet = ($two_v_unit == "feet") ? "  (" . make_inches($two_v_al) . ")" : null;
$two_v_bl_feet = ($two_v_unit == "feet") ? "  (" . make_inches($two_v_bl) . ")" : null;
$two_v_aw_feet = ($two_v_unit == "feet") ? "  (" . make_inches($two_v_combos[0][5]) . ")" : null;
$two_v_bw_feet = ($two_v_unit == "feet") ? "  (" . make_inches($two_v_combos[0][6]) . ")" : null;
$two_v_W_feet = ($two_v_unit == "feet") ? "  (" . make_inches($two_v_W) . ")" : null;

echo "<br />\n";
echo "<p>Building a 2v dome from " . $one_v_L . $two_v_disp_u . " stock with " . $two_v_al . $two_v_disp_u . " A poles will:</p>\n";
echo "<ul>\n";
  echo "<li>Your A poles will be " . $two_v_al . $two_v_disp_u . " long." . $two_v_al_feet . "</li>\n";
  echo "<li>Your B poles will be " . $two_v_bl . $two_v_disp_u . " long." . $two_v_bl_feet . "</li>\n";
  echo "<li>You need " . $two_v_X . " pieces of stock.</li>\n";
  echo "<li>You will need to cut " . $two_v_combos[0][1] . " pieces of stock into " . $two_v_combos[0][0] . ", which will generate " . $two_v_combos[0][5] . $two_v_disp_u . " of waste each." . $two_v_aw_feet . "</li>\n";
  echo "<li>You will need to cut " . $two_v_combos[0][3] . " pieces of stock into " . $two_v_combos[0][2] . ", which will generate " . $two_v_combos[0][6] . $two_v_disp_u . " of waste each." . $two_v_bw_feet . "</li>\n";
  echo "<li>Your total waste is " . $two_v_W . $two_v_disp_u . "." . $two_v_W_feet . "</li>\n";
echo "</ul>\n";

include_once("./files/close.php");
?>