为什么这个圆圈没有正确地绘制所有点?

时间:2011-12-28 19:31:14

标签: flash actionscript-3 drawing geometry

我在绘制圆圈时遇到问题。代码似乎适用于绘制除了一个所需的所有段,但跟踪绘图的点看起来是正确的。

这是生成细分的类:

package views
{
    import flash.display.Sprite;
    import flash.geom.Point;

    import views.segments.AS_Segment;

    public class AS_ColorContainer extends Sprite
    {       
        private var currX       :int;
        private var currY       :int;
        private var diameter    :int;
        private var angle       :int;

        private var segments:Vector.<AS_Segment>;

        public function AS_ColorContainer()
        {
            init();
        }

        private function init():void
        {
            diameter = 400;
            angle = 0;
            currX = 0;
            currY = 0 - radius;
            segments = new Vector.<AS_Segment>(24);

            for(var i:int = 0; i < 6; i++) { 
                drawSegment("greenLarge",0x00FF00,30);
                drawSegment("red",0xFF0000,10);
                drawSegment("greenSmall",0x00FF00,10);
                drawSegment("blue",0x0000FF,10);
            }
        }

        private function drawSegment($name:String,$color:uint,$size:int):void
        {
            var seg:AS_Segment = new AS_Segment($name);
            seg.graphics.beginFill($color);
            seg.graphics.moveTo(0,0);
            seg.graphics.lineTo(currX,currY);
            trace("Old: "+currX+":"+currY);
            angle += $size;
            currX = radius * Math.cos(degToRad(angle));
            currY = radius * Math.sin(degToRad(angle));
            trace("New: "+currX+":"+currY);
            seg.graphics.lineTo(currX,currY);
            seg.graphics.lineTo(0,0);
            seg.graphics.endFill();
            addChild(seg);
            segments.push(seg);
        }

        private function degToRad($deg:Number):Number
        {
            return $deg * (Math.PI / 180);
        }

        private function get radius():Number
        {
            return diameter / 2;
        }
    }
}

AS_Segment类是形状的简单扩展,只存储一个id:

package views.segments
{
    import flash.display.Shape;

    public class AS_Segment extends Shape
    {
        public var id       :String;

        public function AS_Segment($id:String)
        {
            id = $id;
        }
    }
}

输出不符合预期:

enter image description here

但痕迹看起来是正确的:

Old: 0:-200
New: 173:99
Old: 173:99
New: 153:128
Old: 153:128
New: 128:153
Old: 128:153
New: 100:173
Old: 100:173
New: 0:200
Old: 0:200
New: -34:196
Old: -34:196
New: -68:187
Old: -68:187
New: -99:173
Old: -99:173
New: -173:99
Old: -173:99
New: -187:68
Old: -187:68
New: -196:34
Old: -196:34
New: -200:0
Old: -200:0
New: -173:-100
Old: -173:-100
New: -153:-128
Old: -153:-128
New: -128:-153
Old: -128:-153
New: -100:-173
Old: -100:-173
New: 0:-200
Old: 0:-200
New: 34:-196
Old: 34:-196
New: 68:-187
Old: 68:-187
New: 100:-173
Old: 100:-173
New: 173:-100
Old: 173:-100
New: 187:-68
Old: 187:-68
New: 196:-34
Old: 196:-34
New: 200:0

1 个答案:

答案 0 :(得分:4)

你没有以正确的方式初始化你的咖喱 (以及currX)

  currX = 0;
  currY = 0 - radius;

这些行设置第一个要绘制到(0,-200)的点,它应该是(200,0)。结果有点奇怪,因为人们会期望x值是错误的。黑色边框显示第一个圆弧片段的实际外观,如果它没有被其他片段隐藏:

circle segments