TYPENAME RegPoly
PARENT Polygon
PARTS
{ sides:Integer
}
TYPEMETHODS
{ new
    { rp -> me:^ new.
      rp polyStyle #closedline.
      rp's sides -> 5.
      rp resizeWidth rp's width resizeHeight rp's width.
      REPLY rp.
    }
}
INSTANCEMETHODS
{ drawHandlesIn aWidget:Widget with aMode:ShapeMode
    { h -> aMode's handle.
      h movex (origin's x - 20) movey (origin's y - 20).
      h drawIn aWidget.
      h movex (origin's x + width) movey (origin's y + height).
      h drawIn aWidget.
    }
  computeExtent
    { !! ignore computeExtent since width is correct for
      !! given points
    }
  resizeWidth w:Integer resizeHeight h:Integer
    { val -> w min h; width -> val; height -> val.
      endpoints -> [].
      n1 -> sides asReal.
      r1 -> width asReal.
      r -> r1.
      r2 -> height asReal.
      r1 -> (r1 * 3.0) / n1.
      r2 -> (r2 * 3.0) / n1.
      pi -> Real pi.
      q -> ((n1 - 2.0) * 180.0) / n1.
      xx -> origin's x.
      yy -> origin's y.
      yy -> yy + height.
      x -> xx asReal.
      x -> x + ((r / 2.0) - (r1 / 2.0)).
      y -> yy asReal.
      1 to sides do 
      { | i:integer |
        u -> (i - 1) asReal.
        uu -> 180.0 - q.
        rad -> ((q - (u * uu)) * pi) / 180.0.
        x -> x + ((rad cos) * r1).
        y -> y - ((rad sin) * r2).
        x1 -> (x asinteger) - origin's x.
        y1 -> (y asinteger) - origin's y.
        endpoints -> endpoints append (x1 @ y1).
      }.
    }
  getValues db:Dialogbox
    { (db's name <> "regpolydb") then 
      { me:^ getValues db.
      } else 
      { te -> db at "originte":Texteditor.
        (te defined) then 
        { te setStringTo (origin asString).
        }.
        te -> db at "widthte":Texteditor.
        (te defined) then 
        { te setStringTo (width asString).
        }.
        te -> db at "sideste":Texteditor.
        (te defined) then 
        { te setStringTo (sides asString).
        }.
      }.
    }
  setValues db:Dialogbox
    { (db's name <> "regpolydb") then 
      { me:^ setValues db.
        me polyStyle #closedline.
      } else 
      { te -> db at "originte":Texteditor.
        org -> te getString.
        (org <> "") then 
        { origin -> org asPoint.
        }.
        te -> db at "widthte":Texteditor.
        w -> te getString.
        (w <> "") then 
        { width -> (w asInteger).
        }.
        te -> db at "sideste":Texteditor.
        si -> te getString.
        (si <> "") then 
        { sides -> (si asInteger).
        } else 
        { sides -> width.
        }.
        me resizeWidth width resizeHeight width.
      }.
    }
}