From 1ab0a85453e2503a6dd72513e4543424b06b973f Mon Sep 17 00:00:00 2001
From: Adrian Mariano <avm4@cornell.edu>
Date: Fri, 9 Aug 2024 21:52:51 -0400
Subject: [PATCH] doc fixes and screws fix

---
 drawing.scad |  2 +-
 screws.scad  | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/drawing.scad b/drawing.scad
index 1771188..ed63276 100644
--- a/drawing.scad
+++ b/drawing.scad
@@ -1024,7 +1024,7 @@ function _normal_segment(p1,p2) =
 // Usage:
 //   path = turtle(commands, [state], [full_state=], [repeat=])
 // Description:
-//   Use a sequence of [turtle graphics]{https://en.wikipedia.org/wiki/Turtle_graphics} commands to generate a path.  The parameter `commands` is a list of
+//   Use a sequence of [turtle graphics](https://en.wikipedia.org/wiki/Turtle_graphics) commands to generate a path.  The parameter `commands` is a list of
 //   turtle commands and optional parameters for each command.  The turtle state has a position, movement direction,
 //   movement distance, and default turn angle.  If you do not give `state` as input then the turtle starts at the
 //   origin, pointed along the positive x axis with a movement distance of 1.  By default, `turtle` returns just
diff --git a/screws.scad b/screws.scad
index 8fee1d7..6e33a94 100644
--- a/screws.scad
+++ b/screws.scad
@@ -592,6 +592,8 @@ module screw(spec, head, drive, thread, drive_size,
             assert(is_finite(_shoulder_diam) && _shoulder_diam>=0, "Must specify nonnegative shoulder diameter")
             assert(is_undef(user_thread_len) || (is_finite(user_thread_len) && user_thread_len>=0), "Must specify nonnegative thread length");
    sides = max(pitch==0 ? 3 : 12, segs(nominal_diam/2));
+   rad_scale = _internal? (1/cos(180/sides)) : 1;
+   islop = _internal ? 4*get_slop() : 0;
    head_height = headless || flathead ? 0 
                : counterbore==true || is_undef(counterbore) || counterbore==0 ? struct_val(spec, "head_height")
                : counterbore;
@@ -599,7 +601,8 @@ module screw(spec, head, drive, thread, drive_size,
    flat_height = !flathead ? 0 
                : let( given_height = struct_val(spec, "head_height"))
                  all_positive(given_height) ? given_height
-               : (struct_val(spec,"head_size_sharp")+struct_val(spec,"head_oversize",0)-d_major)/2/tan(struct_val(spec,"head_angle")/2);
+               : (struct_val(spec,"head_size_sharp")+struct_val(spec,"head_oversize",0)-d_major*rad_scale-islop)/2/tan(struct_val(spec,"head_angle")/2);
+
    flat_cbore_height = flathead && is_num(counterbore) ? counterbore : 0;
 
    blunt_start1 = first_defined([blunt_start1,blunt_start,true]);
@@ -650,8 +653,6 @@ module screw(spec, head, drive, thread, drive_size,
           named_anchor("threads_bot", [0,0,-length-shoulder_full+offset]),
           named_anchor("threads_center", [0,0,(-shank_len-length-_shoulder_len-shoulder_full-flat_height)/2+offset])
    ];
-   rad_scale = _internal? (1/cos(180/sides)) : 1;
-   islop = _internal ? 4*get_slop() : 0;
    vnf = head=="hex" && atype=="head" && counterbore==0 ? linear_sweep(hexagon(id=head_diam*rad_scale),height=head_height,center=true) : undef;
    head_diam_full = head=="hex" ? 2*head_diam/sqrt(3) : head_diam;
    attach_d = in_list(atype,["threads","shank","shaft"]) ? d_major 
@@ -1422,8 +1423,8 @@ function _parse_drive(drive=undef, drive_size=undef) =
 //    ---
 //    details = true for more detailed model.  Default: false
 //    counterbore = counterbore height.  Default: no counterbore
-//    flat_height = height of flat head
-//    teardrop = if true make flathead and counterbores teardrop shaped with the flat 5% away from the edge of the screw.  If numeric, specify the fraction of extra to add.  Set to "max" for a pointed teardrop.  Default: false
+//    flat_height = height of flat head (required for flat heads)
+//    teardrop = if true make flatheads and counterbores teardrop shaped with the flat 5% away from the edge of the screw.  If numeric, specify the fraction of extra to add.  Set to "max" for a pointed teardrop.  Default: false
 //    slop = enlarge diameter by this extra amount (beyond that specified in the screw specification).  Default: 0
 function screw_head(screw_info,details=false, counterbore=0,flat_height,teardrop=false,slop=0) = no_function("screw_head");
 module screw_head(screw_info,details=false, counterbore=0,flat_height,teardrop=false,slop=0) {
@@ -1456,6 +1457,7 @@ module screw_head(screw_info,details=false, counterbore=0,flat_height,teardrop=f
              cyl(d=d, l=counterbore, anchor=BOTTOM);
          }  
          if (head=="flat") {   // For flat head, counterbore is integrated
+           dummy = assert(all_positive([flat_height]), "flat_height must be given for flat heads");
            angle = struct_val(screw_info, "head_angle")/2;
            sharpsize = struct_val(screw_info, "head_size_sharp")+head_oversize;
            sidewall_height = (sharpsize - head_size)/2 / tan(angle);