• well,

    since I could not find an existing method, I quickly hacked one myself (good enough for my own purposes):

    /**** drawRoundedRect ****/
    
      const roundedRectSines = [
        0, Math.sin(15*Math.PI/180), Math.sin(30*Math.PI/180),
        Math.sin(45*Math.PI/180), Math.sin(60*Math.PI/180),
        Math.sin(75*Math.PI/180), 1
      ];
      const roundedRectPoly = Array(56);
      
      function prepareRoundedRect (x1,y1, x2,y2, r) {
        r = Math.min(r || 0, Math.abs(x1-x2), Math.abs(y1-y2));
        
        for (let i = 0, j = 0; i <= 6; i++, j += 2) {
          roundedRectPoly[j]   = x1 + r - r*roundedRectSines[6-i];
          roundedRectPoly[j+1] = y1 + r - r*roundedRectSines[i];
        }
    
        for (let i = 0, j = 14; i <= 6; i++, j += 2) {
          roundedRectPoly[j]   = x2 - r + r*roundedRectSines[i];
          roundedRectPoly[j+1] = y1 + r - r*roundedRectSines[6-i];
        }
    
        for (let i = 0, j = 28; i <= 6; i++, j += 2) {
          roundedRectPoly[j]   = x2 - r + r*roundedRectSines[6-i];
          roundedRectPoly[j+1] = y2 - r + r*roundedRectSines[i];
        }
    
        for (let i = 0, j = 42; i <= 6; i++, j += 2) {
          roundedRectPoly[j]   = x1 + r - r*roundedRectSines[i];
          roundedRectPoly[j+1] = y2 - r + r*roundedRectSines[6-i];
        }
      }
      
      g.drawRoundedRect = function drawRoundedRect (x1,y1, x2,y2, r) {
        prepareRoundedRect(x1,y1, x2,y2, r);
        this.drawPoly(roundedRectPoly,true);
      }
      
      g.fillRoundedRect = function fillRoundedRect (x1,y1, x2,y2, r) {
        prepareRoundedRect(x1,y1, x2,y2, r);
        this.fillPoly(roundedRectPoly,true);
      }
    
    
      g.clear();
      g.setColor('#000000');
      g.drawRoundedRect(10,20, 160,100, 20);
      g.fillRoundedRect(40,10, 100,160, 10);
    

    The two functions have been designed to be added to the g variable in order to be invoked like any other of its methods - but there is certainly a better alternative.


    1 Attachment

    • roundedRectangles.png
About