3 Replies Latest reply on Jan 13, 2009 10:06 AM by kglad

    Draw ellipse

    hoppe11
      I have found a script to draw a ellipse, but I cant get to work. I have made a few changes so it's easy to paste in but nothing that should influence the funcion!?

      It just draws a straight line. (the diagonal I think?)

      I found the script here:
      http://e-articles.info/e/a/title/Drawing-an-Ellipse-using-ActionScript/

      - and here is my version with a few moderations:

      function drawEllipse(mc, xRadius, yRadius, x, y)
      {
      var angleDelta = Math.PI / 4;

      var xCtrlDist = xRadius/Math.cos(angleDelta/2);
      var yCtrlDist = yRadius/Math.cos(angleDelta/2);
      var rx, ry, ax, ay;
      mc.moveTo(x + xRadius, y);
      for (var i = 0; i < 8; i++)
      {
      angle += angleDelta;
      rx = x + Math.cos(angle-(angleDelta/2))*(xCtrlDist);
      ry = y + Math.sin(angle-(angleDelta/2))*(yCtrlDist);
      ax = x + Math.cos(angle)*xRadius;
      ay = y + Math.sin(angle)*yRadius;
      mc.curveTo(rx, ry, ax, ay);
      }
      }

      var tst = _root.createEmptyMovieClip("ellipse", 1);
      tst.lineStyle(1, 0x000000, 100);
      drawEllipse(tst, 100, 200, 300, 300);
        • 1. Re: Draw ellipse
          kglad Adobe Community Professional & MVP
          try:

          • 2. Re: Draw ellipse
            hoppe11 Level 1
            it works, but I think the lineTo() is a bit "distorted" compared to curveTo()

            function drawEllipse(mc, xRadius, yRadius, x, y)
            {
            var smoothness:Number = 360;
            var twoPI:Number = Math.PI*2/smoothness;
            mc.moveTo(x+xRadius,y);
            for(var t=0;t<=smoothness;t++)
            {
            ang = t*twoPI;
            mc.lineTo(x+Math.cos(ang)*xRadius,y+Math.sin(ang)*yRadius);
            }
            }

            var tst = _root.createEmptyMovieClip("ellipse", 1);
            tst.lineStyle(1,0x0000FF,100);
            drawEllipse(tst,100,100,120,120);

            MovieClip.prototype.drawCircle = function(center, rad, fillColor, fillAlpha, lineColor, lineAlpha, lineWidth)
            {
            controlOffset = Math.sin(24*Math.PI/180)*rad;
            if (!fillColor) {
            // if fillColor is not specified black is assigned @ alpha 100%
            fillColor = 0x000000;
            fillAlpha = 100;
            }
            if (lineColor) {
            // if lineColor is specified a line is drawn, if not not
            // if lineWidth is not specified width is set @ 1 pixel
            // if lineAlpha is not specified alpha is set @ 100%
            lineStyle(lineWidth+1*(lineWidth == undefined), lineColor, lineAlpha+100*(lineAlpha == undefined));
            }
            beginFill(fillColor, fillAlpha);
            moveTo(center[0], center[1]-rad);
            curveTo(center[0]+controlOffset, center[1]-rad, center[0]+Math.cos(45*Math.PI/180)*rad, center[1]-Math.sin(45*Math.PI/180)*rad);
            curveTo(center[0]+rad, center[1]-controlOffset, center[0]+rad, center[1]);
            curveTo(center[0]+rad, center[1]+controlOffset, center[0]+Math.cos(45*Math.PI/180)*rad, center[1]+Math.sin(45*Math.PI/180)*rad);
            curveTo(center[0]+controlOffset, center[1]+rad, center[0], center[1]+rad);
            curveTo(center[0]-controlOffset, center[1]+rad, center[0]-Math.cos(45*Math.PI/180)*rad, center[1]+Math.sin(45*Math.PI/180)*rad);
            curveTo(center[0]-rad, center[1]+controlOffset, center[0]-rad, center[1]);
            curveTo(center[0]-rad, center[1]-controlOffset, center[0]-Math.cos(45*Math.PI/180)*rad, center[1]-Math.sin(45*Math.PI/180)*rad);
            curveTo(center[0]-controlOffset, center[1]-rad, center[0], center[1]-rad);
            endFill();
            };
            _root.drawCircle([140, 140], 100, 0xFFFFFF, 100, 0x0000FF, 100, 1);
            • 3. Re: Draw ellipse
              kglad Adobe Community Professional & MVP
              my method is actually less distorted. but use whichever you want.