3 Replies Latest reply on May 22, 2009 6:03 AM by apmyhr

    Drawing filled polygons

    apmyhr Level 1

      I wrote an alrogithm to draw a polygon using an array.  My code looks like this

       

       

       

       

      var

       

       

      points:Array = [{x: -5, y: height}, {x: 0, y: 0}, {x: 5, y: height}];

       

       

       

       

       

       

      this

       

       

       

      this.graphics.beginFill(TimeChartingTool.resourceColors[color]);

       

       

      this.graphics.lineStyle(2, 0x000000);

       

      this.graphics.moveTo(points[0].x, points[0].y);

       

      for (var i:int = 1; i < points.length; i++){

       

           this.graphics.lineTo(points[i].x, points[i].y);

       

           this.graphics.moveTo(points[i].x, points[i].y);

      }

       

       

       

      this.graphics.endFill();

       

      This draws the outline of the triangle correctly, but it wont fill the shape.  Any suggestions?

      this.graphics.lineTo(points[0].x, points[0].y);

      this.graphics.lineTo(points[0].x, points[0].y);

       

       

      this.graphics.endFill();

       

      This draws the outline of the triangle correctly, but it wont fill the shape.  Any suggestions?

      .graphics.clear();

       

        • 1. Re: Drawing filled polygons
          apmyhr Level 1

          Sorry for the messed up format of that comment.  It looks like Adobe forum has issues with copying/pasting code to the comment.

          • 2. Re: Drawing filled polygons
            Gregory Lafrance Level 6

            This code may help you. Compare with your code:

             

            var triangleHeight:uint = 100;
            var triangle:Shape = new Shape();
            
            // red triangle, starting at point 0, 0
            triangle.graphics.beginFill(0xFF0000);
            triangle.graphics.moveTo(triangleHeight/2, 0);
            triangle.graphics.lineTo(triangleHeight, triangleHeight);
            triangle.graphics.lineTo(0, triangleHeight);
            triangle.graphics.lineTo(triangleHeight/2, 0);
            
            // green triangle, starting at point 200, 0
            triangle.graphics.beginFill(0x00FF00);
            triangle.graphics.moveTo(200 + triangleHeight/2, 0);
            triangle.graphics.lineTo(200 + triangleHeight, triangleHeight);
            triangle.graphics.lineTo(200, triangleHeight);
            triangle.graphics.lineTo(200 + triangleHeight/2, 0);
            
            this.addChild(triangle);
            
            

             

            If this post answers your question of helps, please mark it as such.

            1 person found this helpful
            • 3. Re: Drawing filled polygons
              apmyhr Level 1

              After reading the API better I realized what I did wrong.  I didn't know that lineTo(x,y) not only draws to x,y but it also sets the current position to x,y.  So all I had to do was remove the moveTo(x,y).