Announcement

Collapse
No announcement yet.

Determine the Internal Angles of a Polygon Programmatically

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Determine the Internal Angles of a Polygon Programmatically

    As the title states, I'm trying (with limited success) to determine the internal angels of any polygon given the vertices; however, I'm not getting back the correct the numbers and I'm posting here to see if anyone can see something wrong that I don't.

    I'm using a method in my line class called getTheta() to find the angle of the line to the positive x-axis. I've posted the code that determines the theta of a line, below, as well as the updateAngles() function for the polygon class.

    Theta Code
    Code:
    tProt.theta = math.deg(math.atan2(oEnd.y, oEnd.x));
    --make sure the value is positive
    tProt.theta = tProt.theta >= 0 and tProt.theta or 360 + tProt.theta;
    updateAngles()
    Code:
    local function updateAngles(tProt)
      tProt.interiorAngles = {};
      tProt.exteriorAngles = {};
      local tEdges = tProt.edges;
    
      for nLine = 1, tProt.edgesCount do --use the number of vertices since it's the same as the number of edges
        local bIsFirstLine = nLine == 1;
        --local bIsLastLine = nLine == tProt.edgesCount;
    
        --determine the lines between which the angle will be
        local oLine1 = tEdges[nLine];
        local nLine2Index = bIsFirstLine and (tProt.edgesCount) or nLine - 1;
        local oLine2 = tEdges[nLine2Index];
    
        ---LEFT OFF HERE
    
        --get the lines' theta value
        local nTheta1 = oLine1:getTheta();
        local nTheta2 = oLine2:getTheta();
        print("(before) "..nLine.."-> "..nTheta1.." | "..nLine2Index.."-> "..nTheta2);
    
        --if this is the last line, determine the second theta by flipping it so it's oriented correctly
        if (bIsFirstLine) then
          nTheta2 = (nTheta2 >= 180) and nTheta2 - 180 or nTheta2 + 180;
        end
    
        print("(after) "..nLine.."-> "..nTheta1.." | "..nLine2Index.."-> "..nTheta2);
        --get the interior angle
      
        --tProt.interiorAngles[nLine] = math.max(nTheta1, nTheta2) - math.min(nTheta1, nTheta2);
        local nSmall = math.min(nTheta1, nTheta2);
        local nLarge = math.max(nTheta1, nTheta2);
    
        tProt.interiorAngles[nLine] = nLarge;
        print(tProt.interiorAngles[nLine])
      
         --get the exterior angle: this allows for negative interior angles so all extangles == 360 even on concave polygons
        tProt.exteriorAngles[nLine] = 180 - tProt.interiorAngles[nLine];
      end
    
    end
    Any help would be most appreciated
    Last edited by Centauri Soldier; 1 week ago.
    https://github.com/CentauriSoldier
Working...
X