topical media & game development

talk show tell print

actionscript-book-SpriteArranger-com-example-programmingas3-geometricshapes-RegularPolygon.ax

actionscript-book-SpriteArranger-com-example-programmingas3-geometricshapes-RegularPolygon.ax [swf] flex


  package //com.example.programmingas3.geometricshapes 
  {
      
A regular polygon is equilateral (all sides are the same length) and equiangular (all interior angles are the same).

   
          public class @ax-actionscript-book-SpriteArranger-com-example-programmingas3-geometricshapes-RegularPolygon implements actionscript_book_SpriteArranger_com_example_programmingas3_geometricshapes_IPolygon 
          { 
              public var numSides:int;
                  public var sideLength:Number;
                  
                  public function @ax-actionscript-book-SpriteArranger-com-example-programmingas3-geometricshapes-RegularPolygon(len:Number = 100, sides:int = 3):void
                  {
                          this.sideLength = len;
                          this.numSides = sides;
                  }
                  
                  public function getArea():Number
                  {
                      // this method should be overridden in subclasses
                      return 0;
                  }
                  
                  public function getPerimeter():Number
                  {
                      return sideLength * numSides;
                  }
                  
                  public function getSumOfAngles():Number
                  {
                      if (numSides >= 3)
                      {
                          return ((numSides - 2) * 180);
                      }
                      else
                      {
                          return 0;
                      }
                  }
                  
                  public function describe():String
                  {
                      var desc:String = "Each side is " + sideLength + " pixels long.\n";
              desc += "Its area is " + getArea() + " pixels square.\n";
              desc += "Its perimeter is " + getPerimeter() + " pixels long.\n"; 
              desc += "The sum of all interior angles in this shape is " + getSumOfAngles() + " degrees.\n"; 
              
              return desc;  
                  }
      }
  }


(C) Æliens 27/08/2009

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.